springsecurity登陆认证流程详解

 

目录

 

框架原理

登陆认证的详细执行流程 

首先,进入AbstractAuthenticationProcessingFilter:

之后,调用AuthenticationManager的authenticate方法进行验证

调用AuthenticationManager的authenticate方法进行验证的流程—

之后,

调用DaoAuthenticationProvider的authenticate方法进行验证的流程—

最后的认证流程在AbstractUserDetailsAuthenticationProvider类中完成,将拥有认证成功信息的authentication返回给 AbstractAuthenticationProcessingFilter做认证成功后的后续处理——将认证信息加入session中、执行下一个filter等

具体的认证流程图如下——

从登陆认证的过程中我们可以大致了解SpringSecurity的工作原理——


框架原理

springsecurity本质上使用的时Java中的Filter,通过Spring框架的特性将诸多的过滤器作为bean配置在Spring框架中,用户信息的获取通过认证管理器AuthenticationManager实现——authenticamanager调用相应的认证实现类provider去完成用户的认证功能;

详见文章:http://www.blogjava.net/youxia/archive/2008/12/07/244883.html

 

 

登陆认证的详细执行流程 

具体如下——

首先,进入AbstractAuthenticationProcessingFilter:

public abstract class AbstractAuthenticationProcessingFilter extends GenericFilterBean
implements ApplicationEventPublisherAware, MessageSourceAware {
    
    // 过滤器doFilter方法
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
            throws IOException, ServletException {

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

        /*
         * 判断当前filter是否可以处理当前请求,若不行,则交给下一个filter去处理。
         */
        if (!requiresAuthentication(request, response)) {
            chain.doFilter(request, response);

            return;
        }

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

        Authentication authResult;

        try {
            // 很关键!!!调用了子类(UsernamePasswordAuthenticationFilter)的方法
            authResult = attemptAuthentication(request, response);
            if (authResult == null) {
                // return immediately as subclass has indicated that it hasn't completed
                // authentication
                return;
            }
            // 最终认证成功后,会处理一些与session相关的方法(比如将认证信息存到session等操作)。
            sessionStrategy.onAuthentication(authResult, request, response);
        }
        catch (InternalAuthenticationServiceException failed) {
            logger.error(
                    "An internal error occurred while trying to authenticate the user.",
                    failed);
            // 认证失败后的一些处理。
            unsuccessfulAuthentication(request, response, failed);

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

            return;
        }

        // 认证成功
        if (continueChainBeforeSuccessfulAuthentication) {
            chain.doFilter(request, response);
        }
        /*
         * 最终认证成功后的相关回调方法,主要将当前的认证信息放到SecurityContextHolder中
         * 并调用成功处理器做相应的操作。
         */
        successfulAuthentication(request, response, chain, authResult);
    }
}

AbstractAuthenticationProcessingFilter的""authResult = attemptAuthentication(request, response)""触发UsernamePasswordAuthenticationFilter——

// 继承了AbstractAuthenticationProcessingFilter
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值