Spring Security(03)登录认证源码分析

本文深入剖析Spring Security的登录认证过程,从UsernamePasswordAuthenticationFilter的doFilter逻辑开始,讲解AuthenticationManager如何通过ProviderManager和DaoAuthenticationProvider进行用户验证。密码匹配使用passwordEncoder,成功后信息存入SecurityContextHolder。此外,还探讨了SecurityContextPersistenceFilter在请求前后的角色,确保认证信息在上下文中的正确管理。
摘要由CSDN通过智能技术生成

Spring Security在内部维护一个过滤器链,其中每个过滤器都有特定的责任;

比如:

  • ChannelProcessingFilter,因为它可能需要重定向到不同的协议

  • SecurityContextPersistenceFilter,因此可以在web请求开头的SecurityContextHolder中设置SecurityContext,并且SecurityContext的任何更改都可以复制到HttpSession当web请求结束时(准备好与下一个web请求一起使用)

    等等…

登录流程解析

UsernamePasswordAuthenticationFilter

登录认证流程解析认证的是通过一个对应的过滤器UsernamePasswordAuthenticationFilter

此类是一个过滤器继承 AbstractAuthenticationProcessingFilter

既然是过滤器首先看 doFilter 逻辑

private void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		if (!requiresAuthentication(request, response)) {
			chain.doFilter(request, response);
			return;
		}
		try {
		    //核心代码获取  Authentication
			Authentication authenticationResult = attemptAuthentication(request, response);
			if (authenticationResult == null) {
				// return immediately as subclass has indicated that it hasn't completed
				return;
			}
			this.sessionStrategy.onAuthentication(authenticationResult, request, response);
			// Authentication success
			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);
		}
	}


attemptAuthentication 是一个抽象方法,由 UsernamePasswordAuthenticationFilter实现

	@Override
	public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
			throws AuthenticationException {
		if (this.postOnly && !request.getMethod().equals("POST")) {
			throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod());
		}
		//获取用户名 其实就是request.getParam 获取
		String username = obtainUsername(request);
		username = (username != null) ? username : "";
		username = username.trim();
		//获取密码 其实就是request.getParam 获取
		String password = obtainPassword(request);
		password = (password != null) ? password : "";
		//用户名和密码组装成  UsernamePasswordAuthenticationToken 
		Username
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值