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

相关文章

Spring boot整合dubbo+zookeeper的详细过程

《Springboot整合dubbo+zookeeper的详细过程》本文讲解SpringBoot整合Dubbo与Zookeeper实现API、Provider、Consumer模式,包含依赖配置、... 目录Spring boot整合dubbo+zookeeper1.创建父工程2.父工程引入依赖3.创建ap

SpringBoot结合Docker进行容器化处理指南

《SpringBoot结合Docker进行容器化处理指南》在当今快速发展的软件工程领域,SpringBoot和Docker已经成为现代Java开发者的必备工具,本文将深入讲解如何将一个SpringBo... 目录前言一、为什么选择 Spring Bootjavascript + docker1. 快速部署与

MySQL中的LENGTH()函数用法详解与实例分析

《MySQL中的LENGTH()函数用法详解与实例分析》MySQLLENGTH()函数用于计算字符串的字节长度,区别于CHAR_LENGTH()的字符长度,适用于多字节字符集(如UTF-8)的数据验证... 目录1. LENGTH()函数的基本语法2. LENGTH()函数的返回值2.1 示例1:计算字符串

Spring Boot spring-boot-maven-plugin 参数配置详解(最新推荐)

《SpringBootspring-boot-maven-plugin参数配置详解(最新推荐)》文章介绍了SpringBootMaven插件的5个核心目标(repackage、run、start... 目录一 spring-boot-maven-plugin 插件的5个Goals二 应用场景1 重新打包应用

SpringBoot+EasyExcel实现自定义复杂样式导入导出

《SpringBoot+EasyExcel实现自定义复杂样式导入导出》这篇文章主要为大家详细介绍了SpringBoot如何结果EasyExcel实现自定义复杂样式导入导出功能,文中的示例代码讲解详细,... 目录安装处理自定义导出复杂场景1、列不固定,动态列2、动态下拉3、自定义锁定行/列,添加密码4、合并

Spring Boot集成Druid实现数据源管理与监控的详细步骤

《SpringBoot集成Druid实现数据源管理与监控的详细步骤》本文介绍如何在SpringBoot项目中集成Druid数据库连接池,包括环境搭建、Maven依赖配置、SpringBoot配置文件... 目录1. 引言1.1 环境准备1.2 Druid介绍2. 配置Druid连接池3. 查看Druid监控

Java中读取YAML文件配置信息常见问题及解决方法

《Java中读取YAML文件配置信息常见问题及解决方法》:本文主要介绍Java中读取YAML文件配置信息常见问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 目录1 使用Spring Boot的@ConfigurationProperties2. 使用@Valu

创建Java keystore文件的完整指南及详细步骤

《创建Javakeystore文件的完整指南及详细步骤》本文详解Java中keystore的创建与配置,涵盖私钥管理、自签名与CA证书生成、SSL/TLS应用,强调安全存储及验证机制,确保通信加密和... 目录1. 秘密键(私钥)的理解与管理私钥的定义与重要性私钥的管理策略私钥的生成与存储2. 证书的创建与

浅析Spring如何控制Bean的加载顺序

《浅析Spring如何控制Bean的加载顺序》在大多数情况下,我们不需要手动控制Bean的加载顺序,因为Spring的IoC容器足够智能,但在某些特殊场景下,这种隐式的依赖关系可能不存在,下面我们就来... 目录核心原则:依赖驱动加载手动控制 Bean 加载顺序的方法方法 1:使用@DependsOn(最直

SpringBoot中如何使用Assert进行断言校验

《SpringBoot中如何使用Assert进行断言校验》Java提供了内置的assert机制,而Spring框架也提供了更强大的Assert工具类来帮助开发者进行参数校验和状态检查,下... 目录前言一、Java 原生assert简介1.1 使用方式1.2 示例代码1.3 优缺点分析二、Spring Fr