• spring 用户通过交互界面登录成功事件源码分析


    版本

    spring-security-web:5.6.7

    源码

    用户通过前端交互界面登录成功触发此事件
    org.springframework.security.authentication.event.InteractiveAuthenticationSuccessEvent

    事件触发过程

    • 用户名密码认证过滤器
      org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter
    public class UsernamePasswordAuthenticationFilter extends AbstractAuthenticationProcessingFilter 
    
    • 1
    • 认证处理过滤器
      org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter
    private void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
    	throws IOException, ServletException {
    	if (!requiresAuthentication(request, response)) {
    		chain.doFilter(request, response);
    		return;
    	}
    	try {
    		// 尝试对请求进行认证
    		Authentication authenticationResult = attemptAuthentication(request, response);
    		if (authenticationResult == null) {
    			return;
    		}
    		this.sessionStrategy.onAuthentication(authenticationResult, request, response);
    		// 认证成功
    		if (this.continueChainBeforeSuccessfulAuthentication) {
    			chain.doFilter(request, response);
    		}
    		successfulAuthentication(request, response, chain, authenticationResult);
    	}
    	catch (InternalAuthenticationServiceException failed) {
    		this.logger.error("An internal error occurred while trying to authenticate the user.", failed);
    		unsuccessfulAuthentication(request, response, failed);
    	}
    	catch (AuthenticationException ex) {
    		// Authentication failed
    		unsuccessfulAuthentication(request, response, ex);
    	}
    }
    // 默认的认证成功处理行为
    // 1. 将认证对象设置到安全上下文
    // 2. 通知RememberMe服务
    // 3. 发布交互认证成功事件
    // 4. 执行成功处理器
    protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain,
    	Authentication authResult) throws IOException, ServletException {
    	SecurityContext context = SecurityContextHolder.createEmptyContext();
    	context.setAuthentication(authResult);
    	SecurityContextHolder.setContext(context);
    	if (this.logger.isDebugEnabled()) {
    		this.logger.debug(LogMessage.format("Set SecurityContextHolder to %s", authResult));
    	}
    	this.rememberMeServices.loginSuccess(request, response, authResult);
    	if (this.eventPublisher != null) {
    		this.eventPublisher.publishEvent(new InteractiveAuthenticationSuccessEvent(authResult, this.getClass()));
    	}
    	this.successHandler.onAuthenticationSuccess(request, response, authResult);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
  • 相关阅读:
    背景的样式
    力扣题解8/17
    大话Redis(1)
    PHP HTTP 函数
    20232023年MBA/MPA/MEM联考考试大纲:提纲挈领,掌控全局
    【uvm】How to write uvm sequence
    操作系统原理-习题汇总
    Leetcode刷题---两数之和
    git:二、git的本地配置+工作区域和文件状态+git add/commit/log +git reset回退版本
    基于Android的个人电子相册设计与实现
  • 原文地址:https://blog.csdn.net/zhoudingding/article/details/131765852