当前位置:网站首页>Analysis of when AuthenticationSuccessHandler is called after UsernameAuthenticationFilter is authorized successfully
Analysis of when AuthenticationSuccessHandler is called after UsernameAuthenticationFilter is authorized successfully
2022-08-09 02:02:00 【kgduu】
1. Questions
What is Authentication in AuthenticationSuccessHandler processing, is it the same as Authentication passed in AuthenticationManager#authenticate(Authentication authentication)
2. Analysis
AuthenticationManager#authenticate is called in UsernameAuthenticationFilter. In fact, the authentication of DaoAuthenticationProvider is called, and the processing template is defined in its parent class AbstractUserDetailsAuthenticationProvider.The processing code is as follows
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 is returned by retrieveUser.The retrieveUser of DaoAuthenticationProvider gets UserDetails through UserDetailsService#loadUserByUserName.
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;}}
Then create a new Authentication with UserDetails as 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 detailsUsernamePasswordAuthenticationToken result = UsernamePasswordAuthenticationToken.authenticated(principal,authentication.getCredentials(), this.authoritiesMapper.mapAuthorities(user.getAuthorities()));result.setDetails(authentication.getDetails());this.logger.debug("Authenticated user");return result;}
3. Conclusion
The authentication in AuthenticationSuccessHandler is different from that passed in AuthenticationManager#authenticate. The authentication in AuthenticationSuccessHandler is used as the principal by UserDetails, and the credentials of the authentication passed in by AuthenticationManager#authenticate are used as the credentials of the new authentication
边栏推荐
- The Best Open Source Web Application Firewall to Protect Your Web Applications
- Using ngrok on Raspberry Pi (Extra 2)
- 全文翻译:欧盟第29条数据保护工作组 数据保护官指南
- 『Another Redis DeskTop Manager』用了这款Redis可视化工具,分析效率提升12倍
- 力扣刷题记录9.1-----24. 两两交换链表中的节点
- Latex示例参考
- 如何在EasyDSS中使用ffmpeg实现点播视频的拼接与合成?
- 2022/8/8 比赛思维+状压dp
- KQL和Lucene的区别
- 保护您的 Web 应用程序的最佳开源 Web 应用程序防火墙
猜你喜欢
2022杭电多校第五场1007(生成函数+启发式合并+ntt)
Data recovery software EasyRecovery supports recovery of all types of files
New Swagger3.0 tutorial, OAS3 quick configuration guide, to automate API interface documentation!
史上最猛“员工”,疯狂吐槽亿万富翁老板小扎:那么有钱,还总穿着同样的衣服!
[C language brush questions] Application of fast and slow pointers in linked lists
数据恢复软件EasyRecovery支持恢复所有类型的文件
全文翻译:欧盟第29条数据保护工作组 数据保护官指南
LeetCode每日两题02:轮转数组 (均1200道)
进程和线程
力扣刷题记录4.1-----209. 长度最小的子数组
随机推荐
LeetCode每日两题01:有序数组的平方 (均1200道)方法:双指针
gstreamer 记录
Go-8-Gin框架
mysql连接超过八小时报错
Proe/Creo智能硬件产品结构设计要点「干货分享」
425 Can‘t open data connection for transfer of “/“
makefile file compilation
等到中心化的平台不再,衍生于这个平台的一切都将化作泡影
Codeforces Round #809 (Div. 2)A~D1
数据恢复软件EasyRecovery支持恢复所有类型的文件
2022眼康品牌加盟展,北京视力保健展,中国眼科医学技术峰会
typescript90-使用类型文件声明类型
谷歌翻译下载-免费谷歌翻译软件下载
LeetCode每日一题:搜索插入位置 (均1200道)方法:二分查找
企业里Foxmail邮箱问题解决方法汇总
力扣刷题记录2.1-----27. 移除元素
任务六 特征衍生 案例分析
论文笔记:SAITS: SELF-ATTENTION-BASED IMPUTATION FOR TIMESERIES
全文翻译:EDPB数据保护影响评估(DPIA:Data Protection Impact Assessment)指南
考研人总结的时间管理7大忌,你中了几条?