认证过程如下

一、先判断请求(请求必须是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需要使用的

2018-03-29总结



AuthenticationManager调用Provider,provider调用userDetaisService来根据username获取真实的数据库信息。
而在usernamePasswordAuthenticationFilter中来调用的是AuthenticationManager。。这个流程虽然没多么复杂,但是花费我不少时间给理解到了。。。

Spring Security 的登录密码验证过程 UsernamePasswordAuthenticationFilter的更多相关文章

  1. Spring Security 入门(3-11)Spring Security 的登录密码验证过程 UsernamePasswordAuthenticationFilter

    认证过程如下 一.先判断请求(请求必须是post请求)地址是否为配置的 login-processing-url 值(默认/j_spring_security_check),如果不是,则放行,进入下一 ...

  2. Spring Security 自定义登录认证(二)

    一.前言 本篇文章将讲述Spring Security自定义登录认证校验用户名.密码,自定义密码加密方式,以及在前后端分离的情况下认证失败或成功处理返回json格式数据 温馨小提示:Spring Se ...

  3. wordpress密码生成与登录密码验证

    一.研究wordpress时wordpess的密码密码生成与登录密码验证方式很重要 WordPress密码已成为整合的首要目标,如何征服整合,就得了解WordPress密码算法. WordPress系 ...

  4. Spring Security源码解析一:UsernamePasswordAuthenticationFilter之登录流程

    一.前言 spring security安全框架作为spring系列组件中的一个,被广泛的运用在各项目中,那么spring security在程序中的工作流程是个什么样的呢,它是如何进行一系列的鉴权和 ...

  5. Spring Security在登录验证中增加额外数据(如验证码)

    在使用Spring Security框架过程中,经常会有这样的需求,即在登录验证时,附带增加额外的数据,如验证码.用户类型等.下面将介绍如何实现. 注:我的工程是在Spring Boot框架基础上的, ...

  6. 【权限管理系统】Spring security(三)---认证过程(原理解析,demo)

      在前面两节Spring security (一)架构框架-Component.Service.Filter分析和Spring Security(二)--WebSecurityConfigurer配 ...

  7. Spring Security 自定义登录页面

    SpringMVC + Spring Security,自定义登录页面登录验证 学习参考:http://www.mkyong.com/spring-security/spring-security-f ...

  8. Spring Security 入门—内存用户验证

    简介 作为 Spring 全家桶组件之一,Spring Security 是一个提供安全机制的组件,它主要解决两个问题: 认证:验证用户名和密码: 授权:对于不同的 URL 权限不一样,只有当认证的用 ...

  9. Spring Boot整合Spring Security自定义登录实战

    本文主要介绍在Spring Boot中整合Spring Security,对于Spring Boot配置及使用不做过多介绍,还不了解的同学可以先学习下Spring Boot. 本demo所用Sprin ...

随机推荐

  1. Linux命令CURL用法

    Curl是一个命令行方式下传输数据的开源传输工具,支持多种协议包括:FTP,HTTP,HTTPS,IMAP,POP3,TELNET等.同样支持HTTP POST方法,PUT方法,FTP上传,cooki ...

  2. stream求集合元素的属性值最值

    Person p1 = new Person("张三", new BigDecimal("10.0"));Person p2 = new Person(&quo ...

  3. bzoj 1034 [ZJOI2008]泡泡堂BNB——贪心

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1034 原来觉得和 bzoj4977跳伞求生 有点像(虽然还没做). 所以对于a[ ]从小到大 ...

  4. css上下左右居中

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. 原生ajax请求json数据

    <?php header("content-type:text/html;charset=utf-8"); echo '{"name":"小明& ...

  6. [jnhs]未完待续HttpServletRequest示例

    uri: /kaihu/showip.html method: GET QueryString: null Parameters: Headers: Name: host Value: localho ...

  7. MySQL忘记root密码重置密码(5.7版本)

    网上找了一堆方法都不行,经过折腾一番,发现MySQL不同版本重置密码也存在一定的差异!记录下Mysql5.7.18版本的重置密码方法. 1.找到/etc/mysql/my.cnf 修改此文件添加一行s ...

  8. Spring2.5依靠注入的方式有三种

    Spring2.5依靠注入的方式有三种: 1.通过setter方法注入: 2.通过构造方法注入: 3.通过注解进行注入: 第一种方式:通过setter方法注入 Java代码 package com.t ...

  9. history-之前发生了什么

    查看一下之前服务器上执行过的命令.看一下总是没错的,加上前面看的谁登录过的信息,应该有点用.另外作为admin要注意,不要利用自己的权限去侵犯别人的隐私哦. 到这里先提醒一下,等会你可能会需要更新 H ...

  10. NOIP模拟 9.09

    AK300分 果实计数 (count.pas/.c/.cpp) 时间限制:1s,空间限制32MB 题目描述: 淘淘家有棵奇怪的苹果树,这棵树共有n+1层,标号为0~n.这棵树第0层只有一个节点,为根节 ...