Spring Security:身份验证处理AuthenticationManager介绍与Debug分析

本文主要是介绍Spring Security:身份验证处理AuthenticationManager介绍与Debug分析,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

AuthenticationManager

AuthenticationManager用于处理Authentication请求,上一篇博客已经介绍了AuthenticationSpring Security在进行身份验证时,会创建身份验证令牌,即Authentication实例,提供给AuthenticationManager接口的实现类进行处理,由实现类中的AuthenticationProvider列表进行验证。

  • Spring Security:身份验证令牌Authentication介绍与Debug分析
public interface AuthenticationManager {/*** 尝试对传递的Authentication对象进行身份验证* 如果验证成功,则返回完全填充的Authentication对象(包括授予的权限)* AuthenticationManager必须遵守以下关于异常的约定:* 如果帐户被禁用并且AuthenticationManager可以测试此状态,则必须抛出DisabledException* 如果帐户被锁定并且AuthenticationManager可以测试帐户锁定,则必须抛出LockedException* 如果提供了不正确的凭证(比如密码),则必须抛出BadCredentialsException* 参数:authentication - 身份验证请求的封装* 返回:一个完全经过身份验证的对象,包括凭证*/Authentication authenticate(Authentication authentication)throws AuthenticationException;
}

AuthenticationManager接口只有一个实用的实现类ProviderManager,其他的实现类都是内部类,并且不提供身份验证处理的具体实现。
在这里插入图片描述

ProviderManager

ProviderManager通过AuthenticationProvider列表迭代验证Authentication请求,AuthenticationProvider列表通常会按顺序尝试,直到提供非空响应,非空响应表示AuthenticationProvider有权决定身份验证请求,并且不再尝试其他AuthenticationProvider。 如果后续AuthenticationProvider成功验证了请求,则会忽略之前的验证异常并使用此次成功的验证。如果没有后续AuthenticationProvider提供非空响应或新的AuthenticationException ,则将使用最后收到的AuthenticationException

如果没有AuthenticationProvider返回非空响应,或者没有AuthenticationProvider可以处理Authentication,则ProviderManager将抛出ProviderNotFoundException。也可以设置父AuthenticationManager,如果配置的AuthenticationProvider都不能执行身份验证,也会尝试这样做。

此过程的例外情况是AuthenticationProvider抛出AccountStatusException,在这种情况下,将不会继续迭代列表中的其他AuthenticationProvider。身份验证后,则将从返回的Authentication对象中清除凭证(如果它实现了CredentialsContainer接口)。可以通过修改eraseCredentialsAfterAuthentication属性来控制此行为。

身份验证事件发布被委托给配置的AuthenticationEventPublisher,它默认为不发布事件的空实现,如果想接收事件,则必须设置AuthenticationEventPublisher,标准实现是DefaultAuthenticationEventPublisher,它将常见异常映射到事件(在身份验证失败的情况下),并在身份验证成功时发布AuthenticationSuccessEvent

public class ProviderManager implements AuthenticationManager, MessageSourceAware,InitializingBean {private static final Log logger = LogFactory.getLog(ProviderManager.class);// 用于验证事件的发布private AuthenticationEventPublisher eventPublisher = new NullEventPublisher();// 用于验证Authentication的AuthenticationProvider列表private List<AuthenticationProvider> providers = Collections.emptyList();// 用于访问来自MessageSource的消息protected MessageSourceAccessor messages = SpringSecurityMessageSource.getAccessor();// 父AuthenticationManagerprivate AuthenticationManager parent;// 验证之后是否删除凭证private boolean eraseCredentialsAfterAuthentication = true;/*** 使用给定的多个AuthenticationProvider构造一个ProviderManager*/public ProviderManager(AuthenticationProvider... providers) {this(Arrays.asList(providers), null);}/*** 使用给定的AuthenticationProvider列表构造一个ProviderManager*/public ProviderManager(List<AuthenticationProvider> providers) {this(providers, null);}/*** 使用提供的参数构造一个ProviderManager* providers - AuthenticationProvider列表* parent - 父AuthenticationManager*/public ProviderManager(List<AuthenticationProvider> providers,AuthenticationManager parent) {Assert.notNull(providers, "providers list cannot be null");this.providers = providers;this.parent = parent;checkState();}// 在Properties设置之后检查状态public void afterPropertiesSet() {checkState();}// 检查状态private void checkState() {if (parent == null && providers.isEmpty()) {throw new IllegalArgumentException("A parent AuthenticationManager or a list "+ "of AuthenticationProviders is required");} else if (providers.contains(null)) {throw new IllegalArgumentException("providers list cannot contain null values");}}/*** 尝试对传递的Authentication对象进行身份验证* 将连续尝试AuthenticationProvider列表* 直到AuthenticationProvider指示它能够验证传递的Authentication对象的类型* 然后将尝试使用该AuthenticationProvider进行身份验证* 如果多个AuthenticationProvider支持传递的Authentication对象* 则第一个能够成功验证Authentication对象的人会确定result* 成功验证后,不会尝试迭代后面的AuthenticationProvider* 如果所有支持此Authentication对象的AuthenticationProvider的身份验证都未成功* 则抛出AuthenticationException*/public Authentication authenticate(Authentication authentication)throws AuthenticationException {// Authentication实例的类型Class<? extends Authentication> toTest = authentication.getClass();// 最后要抛出的异常AuthenticationException lastException = null;// 父AuthenticationManager要抛出的异常AuthenticationException parentException = null;// 验证结果Authentication result = null;// 父AuthenticationManager的验证结果Authentication parentResult = null;boolean debug = logger.isDebugEnabled();// 遍历AuthenticationProvider列表for (AuthenticationProvider provider : getProviders()) {// 如果该AuthenticationProvider实例不支持此Authentication实例的类型,则continueif (!provider.supports(toTest)) {continue;}if (debug) {logger.debug("Authentication attempt using "+ provider.getClass().getName());}try {// 使用AuthenticationProvider实例验证此Authentication实例result = provider.authenticate(authentication);if (result != null) {// 如果验证结果不为null,则填充result// 并且退出后续的验证copyDetails(authentication, result);break;}}catch (AccountStatusException | InternalAuthenticationServiceException e) {prepareException(e, authentication);// SEC-546: 如果身份验证失败是由于无效的帐户状态引起的,避免轮询其他AuthenticationProviderthrow e;} catch (AuthenticationException e) {lastException = e;}}// 如果验证结果为null,并且父AuthenticationManager不为null// 则使用父AuthenticationManager进行验证if (result == null && parent != null) {try {// 父AuthenticationManager的验证结果赋值给result和parentResult result = parentResult = parent.authenticate(authentication);}catch (ProviderNotFoundException e) {}catch (AuthenticationException e) {lastException = parentException = e;}}// 验证结果不为nullif (result != null) {// 如果验证之后需要删除凭证,并且result instanceof CredentialsContainerif (eraseCredentialsAfterAuthentication&& (result instanceof CredentialsContainer)) {// 验证完成// 从result中删除凭证和其他秘密数据((CredentialsContainer) result).eraseCredentials();}// 如果父AuthenticationManager已尝试验证并成功// 那么它将发布一个AuthenticationSuccessEvent// 如果父AuthenticationManager已经发布了它// 此检查可防止重复的AuthenticationSuccessEventif (parentResult == null) {eventPublisher.publishAuthenticationSuccess(result);}return result;}// 没有AuthenticationProvider支持此Authentication实例的身份验证(引发异常)if (lastException == null) {lastException = new ProviderNotFoundException(messages.getMessage("ProviderManager.providerNotFound",new Object[] { toTest.getName() },"No AuthenticationProvider found for {0}"));}// 如果父AuthenticationManager已尝试验证,但失败// 则它将发布AbstractAuthenticationFailureEvent// 如果父AuthenticationManager已发布// 此检查可防止重复AbstractAuthenticationFailureEventif (parentException == null) {prepareException(lastException, authentication);}throw lastException;}// 根据AuthenticationException实例和Authentication实例准备异常@SuppressWarnings("deprecation")private void prepareException(AuthenticationException ex, Authentication auth) {eventPublisher.publishAuthenticationFailure(ex, auth);}/*** 将身份验证详细信息从源身份验证对象复制到目标身份验证对象,前提是后者还没有* 参数:* source - 源身份验证对象* dest - 目标身份验证对象*/private void copyDetails(Authentication source, Authentication dest) {if ((dest instanceof AbstractAuthenticationToken) && (dest.getDetails() == null)) {AbstractAuthenticationToken token = (AbstractAuthenticationToken) dest;token.setDetails(source.getDetails());}}// 返回AuthenticationProvider列表public List<AuthenticationProvider> getProviders() {return providers;}// 设置messages属性public void setMessageSource(MessageSource messageSource) {this.messages = new MessageSourceAccessor(messageSource);}// 设置eventPublisher 属性public void setAuthenticationEventPublisher(AuthenticationEventPublisher eventPublisher) {Assert.notNull(eventPublisher, "AuthenticationEventPublisher cannot be null");this.eventPublisher = eventPublisher;}/*** 设置eraseCredentialsAfterAuthentication属性* 如果设置为true* 实现CredentialsContainer接口的验证结果Authentication* 将在从authenticate方法返回之前调用其eraseCredentials方法*/public void setEraseCredentialsAfterAuthentication(boolean eraseSecretData) {this.eraseCredentialsAfterAuthentication = eraseSecretData;}// 返回eraseCredentialsAfterAuthenticationpublic boolean isEraseCredentialsAfterAuthentication() {return eraseCredentialsAfterAuthentication;}...
}

Debug分析

项目结构图:
在这里插入图片描述

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.kaven</groupId><artifactId>security</artifactId><version>1.0-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.6.RELEASE</version></parent><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency></dependencies></project>

application.yml

spring:security:user:name: kavenpassword: itkaven
logging:level:org:springframework:security: DEBUG

SecurityConfigSpring Security的配置类,不是必须的,因为有默认的配置):

package com.kaven.security.config;import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {// 任何请求都需要进行验证http.authorizeRequests().anyRequest().authenticated().and()// 记住身份验证.rememberMe(Customizer.withDefaults())// 基于表单登陆的身份验证方式.formLogin(Customizer.withDefaults());}
}

MessageController(定义接口):

package com.kaven.security.controller;import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class MessageController {@GetMapping("/message")public String getMessage() {return "hello spring security";}
}

启动类:

package com.kaven.security;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class);}
}

Debug方式启动应用,访问http://localhost:8080/message,请求会被重定向到表单登陆页,需要输入用户名和密码。
在这里插入图片描述
身份验证请求会被ProviderManager实例处理,如下图所示,该ProviderManager实例有两个AuthenticationProvider,分别为AnonymousAuthenticationProvider实例和RememberMeAuthenticationProvider实例,而它的父AuthenticationManager有一个AuthenticationProvider,即DaoAuthenticationProvider实例。

在这里插入图片描述
ProviderManager实例的两个AuthenticationProvider都不支持该身份验证请求,但它的父AuthenticationManagerAuthenticationProvider支持该身份验证请求。
在这里插入图片描述
AuthenticationManagerAuthenticationProvider会对身份验证请求进行验证,很显然是验证成功了。
在这里插入图片描述
在这里插入图片描述
身份验证处理AuthenticationManager介绍与Debug分析就到这里,如果博主有说错的地方或者大家有不同的见解,欢迎大家评论补充。

这篇关于Spring Security:身份验证处理AuthenticationManager介绍与Debug分析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/290977

相关文章

Java编译生成多个.class文件的原理和作用

《Java编译生成多个.class文件的原理和作用》作为一名经验丰富的开发者,在Java项目中执行编译后,可能会发现一个.java源文件有时会产生多个.class文件,从技术实现层面详细剖析这一现象... 目录一、内部类机制与.class文件生成成员内部类(常规内部类)局部内部类(方法内部类)匿名内部类二、

Go标准库常见错误分析和解决办法

《Go标准库常见错误分析和解决办法》Go语言的标准库为开发者提供了丰富且高效的工具,涵盖了从网络编程到文件操作等各个方面,然而,标准库虽好,使用不当却可能适得其反,正所谓工欲善其事,必先利其器,本文将... 目录1. 使用了错误的time.Duration2. time.After导致的内存泄漏3. jsO

SpringBoot实现数据库读写分离的3种方法小结

《SpringBoot实现数据库读写分离的3种方法小结》为了提高系统的读写性能和可用性,读写分离是一种经典的数据库架构模式,在SpringBoot应用中,有多种方式可以实现数据库读写分离,本文将介绍三... 目录一、数据库读写分离概述二、方案一:基于AbstractRoutingDataSource实现动态

Python FastAPI+Celery+RabbitMQ实现分布式图片水印处理系统

《PythonFastAPI+Celery+RabbitMQ实现分布式图片水印处理系统》这篇文章主要为大家详细介绍了PythonFastAPI如何结合Celery以及RabbitMQ实现简单的分布式... 实现思路FastAPI 服务器Celery 任务队列RabbitMQ 作为消息代理定时任务处理完整

Springboot @Autowired和@Resource的区别解析

《Springboot@Autowired和@Resource的区别解析》@Resource是JDK提供的注解,只是Spring在实现上提供了这个注解的功能支持,本文给大家介绍Springboot@... 目录【一】定义【1】@Autowired【2】@Resource【二】区别【1】包含的属性不同【2】@

springboot循环依赖问题案例代码及解决办法

《springboot循环依赖问题案例代码及解决办法》在SpringBoot中,如果两个或多个Bean之间存在循环依赖(即BeanA依赖BeanB,而BeanB又依赖BeanA),会导致Spring的... 目录1. 什么是循环依赖?2. 循环依赖的场景案例3. 解决循环依赖的常见方法方法 1:使用 @La

Java枚举类实现Key-Value映射的多种实现方式

《Java枚举类实现Key-Value映射的多种实现方式》在Java开发中,枚举(Enum)是一种特殊的类,本文将详细介绍Java枚举类实现key-value映射的多种方式,有需要的小伙伴可以根据需要... 目录前言一、基础实现方式1.1 为枚举添加属性和构造方法二、http://www.cppcns.co

Elasticsearch 在 Java 中的使用教程

《Elasticsearch在Java中的使用教程》Elasticsearch是一个分布式搜索和分析引擎,基于ApacheLucene构建,能够实现实时数据的存储、搜索、和分析,它广泛应用于全文... 目录1. Elasticsearch 简介2. 环境准备2.1 安装 Elasticsearch2.2 J

Java中的String.valueOf()和toString()方法区别小结

《Java中的String.valueOf()和toString()方法区别小结》字符串操作是开发者日常编程任务中不可或缺的一部分,转换为字符串是一种常见需求,其中最常见的就是String.value... 目录String.valueOf()方法方法定义方法实现使用示例使用场景toString()方法方法

Java中List的contains()方法的使用小结

《Java中List的contains()方法的使用小结》List的contains()方法用于检查列表中是否包含指定的元素,借助equals()方法进行判断,下面就来介绍Java中List的c... 目录详细展开1. 方法签名2. 工作原理3. 使用示例4. 注意事项总结结论:List 的 contain