Spring Security认证过程源码分析

Spring Security 认证

Spring Security 是一个基于过滤器链来提供认证和授权功能的框架,本文主要分析其中的认证过程

过滤器链

过滤器链如下图所示,过滤器链中主要是第二部分用于进行认证。本文分析 UsernamePasswordAuthenticationFilter,它用于处理表单提交的登录请求。SecurityContextPersistenceFilter 主要用于从请求读取或向响应装入认证信息 securityContext
oow6Jg.png

认证处理流程

oItEuT.png

认证用户名和密码

UsernamePasswordAuthenticationFilter 的 attemptAuthentication 方法处理认证

@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());
   }
   String username = obtainUsername(request);
   username = (username != null) ? username : "";
   username = username.trim();
   String password = obtainPassword(request);
   password = (password != null) ? password : "";
   UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(username, password);
   // 向token中添加一些详细信息
   setDetails(request, authRequest);
   return this.getAuthenticationManager().authenticate(authRequest);
}
  1. 当用户在登录页面输入用户名和密码后,进入 org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter 类的 attemptAuthentication 方法。这个方法会读取请求中的用户名和密码,并生成 UsernamePasswordAuthenticationToken 这个用于认证的 token
  2. 调用 setDetails 方法,向 token 中添加一些详细信息,具体就是请求的 ip、session 的信息
  3. 调用 ProviderManager 中的 authenticate 进行认证工作
ProviderManager 的 authenticate 方法进行认证工作
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
   
   Class<? extends Authentication> toTest = authentication.getClass();
   AuthenticationException lastException = null;
   AuthenticationException parentException = null;
   Authentication result = null;
   Authentication parentResult = null;
   int currentPosition = 0;
   int size = this.providers.size();
   // 遍历所有的providers
   for (AuthenticationProvider provider : getProviders()) {
   
      if (!provider.supports(toTest)) {
   
         // 如果provider不支持认证此请求,跳过
         continue;
      }
      try {
   
         // 此provider进行认证
         result = provider.authenticate(authentication
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值