SpringSecurity认证流程

认证

SecurityContextHolder:

SecurityContextHolder是认证模块的核心所在,SecurityContextHolder存储着认证过的用户的详情。

securitycontextholder

如果想指定一个已认证用户,最简单的方式就是set SecurityContextHolder

SecurityContext context = SecurityContextHolder.createEmptyContext(); 
Authentication authentication =
    new TestingAuthenticationToken("username", "password", "ROLE_USER"); 
context.setAuthentication(authentication);

SecurityContextHolder.setContext(context); 

同时,如上图,也可以通过SecurityContextHolder获取SecurityContext,再通过SecurityContext获取用户的认证信息

SecurityContext context = SecurityContextHolder.getContext();
Authentication authentication = context.getAuthentication();
String username = authentication.getName();
Object principal = authentication.getPrincipal();
Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();

SecurityContextHolder通过ThreadLocal来存储认证详情,通过FilterChainProxy来保证当一个请求处理完成之后,对应的线程就会被清除,从而保证SecurityContext也被清除。

可以通过设置SecurityContextHolder的Mod来设置线程与context之间的关系,详情参见SecurityContextHolder的JavaDoc


SecurityContext

securityContext包含了认证对象


Authentication

Authentication在SpringSecurity中有两个主要的目的

  • An input to AuthenticationManager to provide the credentials a user has provided to authenticate. When used in this scenario, isAuthenticated() returns false.(TODO 理解)
  • 用于表示当前已经认证过的用户

Authentication包含:

  • principal - 用户标识(TODO 待补充)
  • credentials - 通常是用户的密码
  • authorities - 用户被授予的权限

GrantedAuthority

用户Role,也就是被授予的权限

(TODO 补充)

Usually the GrantedAuthority objects are application-wide permissions. They are not specific to a given domain object. Thus, you wouldn’t likely have a GrantedAuthority to represent a permission to Employee object number 54, because if there are thousands of such authorities you would quickly run out of memory (or, at the very least, cause the application to take a long time to authenticate a user). Of course, Spring Security is expressly designed to handle this common requirement, but you’d instead use the project’s domain object security capabilities for this purpose.


AuthenticationManager

AuthenticationManager定义了SpringSecurity进行认证操作的API。Authentication被Controller设置到SecurityContextHolder中。


ProviderManager

ProviderManager是AuthenticationManager的最常用的一种实现。它使用一个list来存储配置的所有AuthenticationProvider,然后将认证操作委托给这些provider。

每个Provider都有机会指明认证操作是成功、失败、或自己无法处理,需要丢给下一个Provider处理。如果所有的Provider都无法处理这次认证,那么将会抛出一个ProviderNotFoundException(特殊的AuthenticationException),这个异常表明配置的ProviderManager无法支持当前的认证类型。

providermanager

这种委托方式使得每个AuthenticationProvider都可以处理特定的认证类型,并且保证只暴露出一个ProviderManager的bean

ProviderManager还可以配置一个可选的parent(AuthenticationManager),这个parent会在ProviderManager无法处理对应类型的认证时被调用。这个parent也通常是ProviderManager。

同时,多个ProviderManager也可以配置同一个parent

providermanagers parent

通常情况下,ProviderManager会在返回一个成功的认证请求后清除Authentication对象中的敏感信息。


AuthenticationProvider

AuthenticationProvider会被注入到ProviderManager中,每一个AuthenticationProvider支持特定类型的认证请求。

例如:DaoAuthenticationProvider支持用户名/密码类型的认证请求,JwtAuthenticationProvider 支持JWT token类型的认证请求


Request Credentials with AuthenticationEntryPoint

AuthenticationEntryPoint用于处理:有从客户端发起的请求时,直接返回HTTP响应

例如以下两种场景:

  • 如果客户端在请求资源的request中已经包含了凭证,例如用户名/密码,那么AuthenticationEntryPoint不需要对这个请求做出任何响应。因为在请求中已经包含了凭证了
  • 如果请求发起一个未认证的请求,那么AuthenticationEntryPoint就需要返回对应的response。例如重定向到登录页或返回带有 WWW-Authenticate header 的response

AbstractAuthenticationProcessingFilter

在一个未认证的请求经过AuthenticationEntryPoint处理后,再次请求认证时的流程图如下:

abstractauthenticationprocessingfilter

  1. 当用户提交credentials时,AbstractAuthenticationProcessingFilter会从请求中创建一个Authentication对象
  2. 把Authentication对象传给AuthenticationManager进行
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Security认证流程可以简要描述如下: 1. 当一个请求进入系统时,会经过一个过滤器链,其中包括一个实现了AbstractAuthenticationProcessingFilter的过滤器。 2. 这个过滤器会首先判断请求的URI是否需要认证,如果需要认证,则执行attemptAuthentication方法进行认证。 3. attemptAuthentication方法会调用AuthenticationManager进行认证,AuthenticationManager是一个接口,具体的实现类是ProviderManager。 4. ProviderManager内部包含了一个List<AuthenticationProvider>对象,通过AuthenticationProvider接口的实现类来扩展不同的认证提供者。 5. 在认证过程中,AuthenticationManager会依次调用每个AuthenticationProvider的authenticate方法进行认证。 6. 如果认证成功,会将认证后的Authentication对象存放到SecurityContext中。 7. 如果认证失败,会通过认证失败处理器AuthenticationFailureHandler进行处理。 8. 认证成功后,会执行successfulAuthentication方法,将已认证的Authentication存放到SecurityContext中。 9. 这样,下一个请求进来时,系统就能知道该请求是否已经通过认证。 总结起来,Spring Security认证流程包括了过滤器链、认证管理器、认证提供者和认证失败处理器等组件,通过这些组件的协作,实现了对请求的认证和授权。\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* *2* [SpringSecurity认证流程分析](https://blog.csdn.net/chisuisi5702/article/details/126281839)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Spring Security认证过程](https://blog.csdn.net/weixin_38927257/article/details/102960752)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值