咱们上一篇介绍了UsernamePasswordAuthenticationFilter
的工做流程,留下了一个小小的伏笔,做为一个Servlet Filter应该存在一个doFilter
实现方法,而它却没有,其实它的父类AbstractAuthenticationProcessingFilter
提供了具体的实现。稍后咱们会根据这个实现引出今天的主角AuthenticationManager
,来继续介绍用户的认证过程。html
咱们来看看AbstractAuthenticationProcessingFilter
的核心方法doFilter
的实现:java
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
方法,这个咱们已经在上一文分析了是经过AuthenticationManager
的authenticate
方法进行认证逻辑的处理,接下来咱们将重点分析这个接口来帮助咱们了解Spring Seucirty的认证过程。spring
AuthenticationManager
这个接口方法很是奇特,入参和返回值的类型都是Authentication
。该接口的做用是对用户的未授信凭据进行认证,认证经过则返回授信状态的凭据,不然将抛出认证异常AuthenticationException
。session
那么AbstractAuthenticationProcessingFilter
中的 AuthenticationManager
是在哪里配置的呢? 看过Spring Security 实战干货系列应该知道WebSecurityConfigurerAdapter
中的void configure(AuthenticationManagerBuilder auth)
是配置AuthenticationManager
的地方, 我根据源码总结了一下AuthenticationManager
的初始化流程,相信能够帮助你去阅读相关的源码:ide
须要注意的是若是咱们使用自定义配置必定不能按照相似下面的错误示范:ui
@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); }
AuthenticationManager
的实现ProviderManager
管理了众多的AuthenticationProvider
。每个AuthenticationProvider
都只支持特定类型的Authentication
,若是不支持将会跳过。另外一个做用就是对适配的Authentication
进行认证,只要有一个认证成功,那么就认为认证成功,全部的都没有经过才认为是认证失败。认证成功后的Authentication
就变成授信凭据,并触发认证成功的事件。认证失败的就抛出异常触发认证失败的事件。spa
从这里咱们能够看出认证管理器AuthenticationManager
针对特定的Authentication
提供了特定的认证功能,咱们能够借此来实现多种认证并存。debug
经过本文咱们对Spring Security认证管理器AuthenticationManager
的初始化过程和认证过程进行了分析,若是你熟悉了AuthenticationManager
的逻辑能够实现多种认证方式的并存等能力,实现不少有用的逻辑,这对集成Spring Security到项目中很是重要。多多关注:码农小胖哥 获取更多的原创干货。code
关注公众号:Felordcn 获取更多资讯
htm