当前位置:网站首页>UsernameAuthenticationFilter授权成功后调用AuthenticationSuccessHandler时的解析
UsernameAuthenticationFilter授权成功后调用AuthenticationSuccessHandler时的解析
2022-08-09 01:59:00 【kgduu】
1、问题
AuthenticationSuccessHandler处理中的Authentication是什么,与传入AuthenticationManager#authenticate(Authentication authentication)中的 Authentication是一样的吗
2、分析
UsernameAuthenticationFilter中调用AuthenticationManager#authenticate,其实调用的是DaoAuthenticationProvider的authenticate,在其父类AbstractUserDetailsAuthenticationProvider中定义了处理模板。其处理代码如下
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
String username = determineUsername(authentication);
boolean cacheWasUsed = true;
UserDetails user = this.userCache.getUserFromCache(username);
if (user == null) {
cacheWasUsed = false;
user = retrieveUser(username, (UsernamePasswordAuthenticationToken) authentication);
}
this.preAuthenticationChecks.check(user);
additionalAuthenticationChecks(user, (UsernamePasswordAuthenticationToken) authentication);
this.postAuthenticationChecks.check(user);
if (!cacheWasUsed) {
this.userCache.putUserInCache(user);
}
Object principalToReturn = user;
if (this.forcePrincipalAsString) {
principalToReturn = user.getUsername();
}
return createSuccessAuthentication(principalToReturn, authentication, user);
}principalToReturn为retrieveUser返回的。DaoAuthenticationProvider的retrieveUser是通过UserDetailsService#loadUserByUserName得到UserDetails。
protected final UserDetails retrieveUser(String username, UsernamePasswordAuthenticationToken authentication)
throws AuthenticationException {
prepareTimingAttackProtection();
UserDetails loadedUser = this.getUserDetailsService().loadUserByUsername(username);
if (loadedUser == null) {
throw new InternalAuthenticationServiceException(
"UserDetailsService returned null, which is an interface contract violation");
}
return loadedUser;
}
}然后创建新的Authentication,即以UserDetails作为principal。
protected Authentication createSuccessAuthentication(Object principal, Authentication authentication,
UserDetails user) {
// Ensure we return the original credentials the user supplied,
// so subsequent attempts are successful even with encoded passwords.
// Also ensure we return the original getDetails(), so that future
// authentication events after cache expiry contain the details
UsernamePasswordAuthenticationToken result = UsernamePasswordAuthenticationToken.authenticated(principal,
authentication.getCredentials(), this.authoritiesMapper.mapAuthorities(user.getAuthorities()));
result.setDetails(authentication.getDetails());
this.logger.debug("Authenticated user");
return result;
}3、结论
AuthenticationSuccessHandler中的 authentication与AuthenticationManager#authenticate传入的不一样,AuthenticationSuccessHandler中的authentication是由UserDetails作为principal,AuthenticationManager#authenticate传入的authentication的credentials作为新authentication的credentials
边栏推荐
- 全文翻译:欧盟第29条数据保护工作组 数据保护官指南
- 方法参数
- 2022眼康品牌加盟展,北京视力保健展,中国眼科医学技术峰会
- typescript91-添加任务基本实现
- 字节输入流(InputStream)与字节输出流(OutputStream)
- [Cellular Automata] Simulation of emergency evacuation of disaster personnel under social force factors based on cellular automata with matlab code attached
- 力扣刷题记录7.1-----707. 设计链表
- typescripet92-添加任务功能优化
- Data recovery software EasyRecovery supports recovery of all types of files
- 配置文件的读取-TOML
猜你喜欢

Image denoising based on edge enhancement Diffusion 】 (cEED) and Coherence Enhancing coursing together (cCED) filter to realize image denoising matlab code

JDBC技术(二)——设置通用的sql和配置文件

基于机器学习之模型树短期负荷预测(Matlab代码实现)

【图像去噪】基于边缘增强扩散 (cEED) 和 Coherence Enhancing Diffusion (cCED) 滤波器实现图像去噪附matlab代码

在树莓派上使用cpolar(番外篇2)

spdlog日志库的封装使用

力扣刷题记录3.1-----977. 有序数组的平方

虹科技术|如何阻止供应链攻击?

mysql连接超过八小时报错

全文翻译:欧盟第29条数据保护工作组 数据保护官指南
随机推荐
ONNX是什么?怎么用?[简明解读版]
力扣刷题记录2.1-----27. 移除元素
力扣刷题记录9.1-----24. 两两交换链表中的节点
When the centralized platform is gone, everything derived from this platform will be in vain
谷歌翻译软件-免费谷歌翻译
使用百度EasyDL实现智能垃圾箱
全文翻译:欧盟第29条数据保护工作组 数据保护官指南
方法参数
保护您的 Web 应用程序的最佳开源 Web 应用程序防火墙
D. Tournament Countdown
Go-8-Gin框架
力扣刷题记录6.1-----203. 移除链表元素
Phenomenon 1 during RF debugging
The 7 taboos of time management summarized by the postgraduate students, how many have you won?
力扣刷题记录1.5-----367. 有效的完全平方数
How to install ngrok in Synology system (Synology 6.X version)
【图像增强】基于Step和Polynomial 滤波实现图像增强附matlab代码
Latex example reference
【信号去噪】基于Sage-Husa自适应卡尔曼滤波器实现海浪磁场噪声抑制及海浪磁场噪声的产生附matlab代码
进程和线程