Spring Security 实战干货:理解AuthenticationManager

栏目: IT技术 · 发布时间: 3年前

内容简介:我们我们来看看大部分逻辑这里是清晰的,关键在于

1. 前言

我们 上一篇 介绍了 UsernamePasswordAuthenticationFilter 的工作流程,留下了一个小小的伏笔,作为一个 Servlet Filter 应该存在一个 doFilter 实现方法,而它却没有,其实它的父类 AbstractAuthenticationProcessingFilter 提供了具体的实现。稍后我们会根据这个实现引出今天的主角 AuthenticationManager ,来继续介绍用户的认证过程。

2. AbstractAuthenticationProcessingFilter

我们来看看 AbstractAuthenticationProcessingFilter 的核心方法 doFilter 的实现:

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
      throws IOException, ServletException {

   HttpServletRequest request = (HttpServletRequest) req;
   HttpServletResponse response = (HttpServletResponse) res;

    // 先通过请求的uri来判断是否需要认证,比如默认的/login 
   if (!requiresAuthentication(request, response)) {
      chain.doFilter(request, response);

      return;
   }

   if (logger.isDebugEnabled()) {
      logger.debug("Request is to process authentication");
   }

   Authentication authResult;

   try {
       // 接着就是执行子类钩子方法attemptAuthentication来获取认证结果对象Authentication ,这个对象不能是空 否则直接返回
      authResult = attemptAuthentication(request, response);
      if (authResult == null) {
         // return immediately as subclass has indicated that it hasn't completed
         // authentication
         return;
      }
       // 处理session 策略,这里默认没有任何策略
      sessionStrategy.onAuthentication(authResult, request, response);
   }
   catch (InternalAuthenticationServiceException failed) {
      logger.error(
            "An internal error occurred while trying to authenticate the user.",
            failed);
       // 如果遇到异常 就会交给认证失败处理器 AuthenticationFailureHandler 来处理
      unsuccessfulAuthentication(request, response, failed);

      return;
   }
   catch (AuthenticationException failed) {
      // Authentication failed
      unsuccessfulAuthentication(request, response, failed);

      return;
   }

   //  认证成功后继续其它过滤器链 并最终交给认证成功处理器 AuthenticationSuccessHandler 处理
   if (continueChainBeforeSuccessfulAuthentication) {
      chain.doFilter(request, response);
   }

   successfulAuthentication(request, response, chain, authResult);
}

大部分逻辑这里是清晰的,关键在于 attemptAuthentication 方法,这个我们已经在上一文分析了是通过 AuthenticationManagerauthenticate 方法进行认证逻辑的处理,接下来我们将重点分析这个接口来帮助我们了解 Spring Seucirty 的认证过程。

3. AuthenticationManager

AuthenticationManager 这个接口方法非常奇特,入参和返回值的类型都是 Authentication 。该接口的作用是对用户的未授信凭据进行认证,认证通过则返回授信状态的凭据,否则将抛出认证异常 AuthenticationException

3.1 AuthenticationManager的初始化流程

那么 AbstractAuthenticationProcessingFilter 中的 AuthenticationManager 是在哪里配置的呢? 看过 Spring Security 实战干货系列 应该知道 WebSecurityConfigurerAdapter 中的 void configure(AuthenticationManagerBuilder auth) 是配置 AuthenticationManager 的地方, 我根据源码总结了一下 AuthenticationManager 的初始化流程,相信可以帮助你去阅读相关的源码:

Spring Security 实战干货:理解AuthenticationManager

需要注意的是如果我们使用自定义配置一定不能按照类似下面的错误示范:

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
    daoAuthenticationProvider.setUserDetailsService(weChatSecurityConfigProperties.getUserDetailsService());
    daoAuthenticationProvider.setPasswordEncoder(multiPasswordEncoder());
    auth.authenticationProvider(daoAuthenticationProvider);
    // 调用 super 将导致不生效 所以下面语句不要写
    super.configure(auth);
}

3.2 AuthenticationManager的认证过程

AuthenticationManager 的实现 ProviderManager 管理了众多的 AuthenticationProvider 。每一个 AuthenticationProvider 都只支持特定类型的 Authentication ,如果不支持将会跳过。另一个作用就是对适配的 Authentication 进行认证,只要有一个认证成功,那么就认为认证成功,所有的都没有通过才认为是认证失败。认证成功后的 Authentication 就变成授信凭据,并触发认证成功的事件。认证失败的就抛出异常触发认证失败的事件。

Spring Security 实战干货:理解AuthenticationManager

从这里我们可以看出认证管理器 AuthenticationManager 针对特定的 Authentication 提供了特定的认证功能,我们可以借此来实现多种认证并存。

4. 总结

通过本文我们对 Spring Security 认证管理器 AuthenticationManager 的初始化过程和认证过程进行了分析,如果你熟悉了 AuthenticationManager 的逻辑可以实现多种认证方式的并存等能力,实现很多有用的逻辑,这对集成 Spring Security 到项目中非常重要。多多关注: 码农小胖哥 获取更多的原创干货。

关注公众号:Felordcn 获取更多资讯

个人博客:https://felord.cn


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

数字化商业模式

数字化商业模式

大前研一 / 王小燕 / 中信出版社 / 2006-4 / 32.00元

《数字化商业模式》为商学院课程的第三部精华集锦,来自金融界、餐饮业、公共设施等领域的领军人物亲自讲述他们的成功案例,以及他们在思考技能、人才管理、事业构想、战略技能等方面的管理理念和战略。任何成功的企业家,不是人云亦云而是能够独立思考的人,不是依赖于他人而是执著、自立的人,不只是沿袭旧思路而是具备创新力、执行力的人。一起来看看 《数字化商业模式》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具