当前位置:网站首页>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
边栏推荐
猜你喜欢
Observer pattern
spdlog日志库的封装使用
在实际工作中如何开展性能测试?
德语翻译器在线翻译中文
Image denoising based on edge enhancement Diffusion 】 (cEED) and Coherence Enhancing coursing together (cCED) filter to realize image denoising matlab code
2022PMP项目管理认证考试报考指南(1)
谷歌翻译软件-免费谷歌翻译
《LC刷题总结》——贪心
mysql 5.7 入坑
Codeforces Round #809 (Div. 2)A~D1
随机推荐
Proe/Creo智能硬件产品结构设计要点「干货分享」
HNUMSC-C语言第一课
When the centralized platform is gone, everything derived from this platform will be in vain
线段树知识整理
JDBC technology (3) - use Druid database connection pool test
JDBC技术(三)——使用Druid数据库连接池测试
Use of torchversion.transforms
Design of Go-7-RESTful API
论文笔记:SAITS: SELF-ATTENTION-BASED IMPUTATION FOR TIMESERIES
德语翻译-德语在线批量翻译软件
2022 PMP Project Management Certification Exam Registration Guide (1)
[C language brush questions] Application of fast and slow pointers in linked lists
【HNUMSC】C语言第二讲
[Cellular Automata] Simulation of emergency evacuation of disaster personnel under social force factors based on cellular automata with matlab code attached
SEMRush如何寻找关键词用于投放广告
Image denoising based on edge enhancement Diffusion 】 (cEED) and Coherence Enhancing coursing together (cCED) filter to realize image denoising matlab code
Go-12-结构体
在树莓派上使用cpolar(番外篇2)
Observer pattern
final