zoukankan      html  css  js  c++  java
  • Spring Security 入门(3-11)Spring Security 的登录密码验证过程 UsernamePasswordAuthenticationFilter

    认证过程如下

    一、先判断请求(请求必须是post请求)地址是否为配置的 login-processing-url 值(默认/j_spring_security_check),如果不是,则放行,进入下一个过滤器,是则进行校验。

    二、验证用户密码信息并返回Authentication类,在验证过程中如果失败则捕获异常进行处理(执行unsuccessfulAuthentication方法调转到配置中的错误链接),如果验证成功,则将调用SessionAuthenticationStrategy中的方法onAuthentication()判断用户能否重复登陆,是否二次登陆——根据你的配置文件决定,如果用户登陆满足条件则再执行successfulAuthentication(配置中的验证成功链接),如果失败则还是执行unsuccessfulAuthentication方法

    详细说明:

    1、用户密码认证过程

        AbstractAuthenticationProcessingFilter.doFilter()调用UsernamePasswordAuthenticationFilter中的attemptAuthentication方法,

        

        UsernamePasswordAuthenticationFilter中的attemptAuthentication方法将表单请求的信息(用户、密码等信息)赋值给UsernamePasswordAuthenticationToken(authRequest),

        然后调用getAuthenticationManager().authenticate(authRequest)对用户密码的正确性进行验证,认证失败就抛出异常,成功就返回Authentication对象。

        

    AuthenticationManager就是认证管理器,它的方法authenticate执行逻辑如下(默认配置DaoAuthenticationProvider,以DaoAuthenticationProvider为例):

    (1)、判断是否有Authentication 对应的AuthenticationProvider,

               有就执行AuthenticationProvider的authenticate方法,

               没有就获取父类AuthenticationManager,查看父类中是否有Authentication 对应的AuthenticationProvider,

                      如果也没有则抛出ProviderNotFoundException异常

    (2)执行AuthenticationProvider的authenticate方法

            1、根据输入名,查看缓存中是否已经有有用户实体对象

                 如果有,则对密码重新验证;

                 如果没有,则进行用户的信息验证,执行DaoAuthenticationProvider中的retrieveUser方法,获取一个UserDetails对象

                        如果UserDetails对象为null或者获取时出错就抛出异常UsernameNotFoundException或AuthenticationServiceException异常,

                        然后一些属性验证之后,对用户密码进行验证。

            2、给新获取的UserDetails对象放入缓存中

            3、新建一个UsernamePasswordAuthenticationToken一个对象,将authenticated设为true(原来传入的UsernamePasswordAuthenticationToken对象authenticated为false)并返回

    2、SessionAuthenticationStrategy.onAuthentication处理过程(主要处理一个用户是否可以同时多次登陆)

        1、checkAuthenticationAllowed方法

             根据maximumSessions和exceptionIfMaximumExceeded的设置判断

                 用户是否多次登陆,

                 是否超过maximumSessions同时登陆了,

                 是否限制用户二次登陆(限制的话则第二次登陆的时候会抛出SessionAuthenticationException异常)还是第二次登陆使第一次登陆无效

        2、复制一个新的session,拥有新的sessionID

       3、更新SessionRegistry中的ConcurrentMap<Object,Set<String>> principals和Map<String, SessionInformation> sessionIds,这个是在第三个过滤器中ConcurrentSessionFilter需要使用的

  • 相关阅读:
    去除phpcms会员登录后头部登陆条的会员名称的括号
    图片左右间隔滚动Jquery特效
    JS判断字符串长度的5个方法
    HTML中&nbsp; &ensp; &emsp; &thinsp;等6种空白空格的区别
    Chrome firefox ie等浏览器空格&nbsp;宽度不一样怎么办
    jQuery延迟加载插件(Lazy Load)详解
    jquery复选框 选中事件 及其判断是否被选中
    手机网页Html代码实现(解决显示页面很小的问题)
    iOS下的按钮css去除原生样式
    用纯css改变下拉列表select框的默认样式(不兼容IE10以下)
  • 原文地址:https://www.cnblogs.com/lexiaofei/p/7018405.html
Copyright © 2011-2022 走看看