认证过程如下

一、先判断请求(请求必须是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. K8s 学习者绝对不能错过的最全知识图谱(内含 56个知识点链接)

    导读:Kubernetes 作为云原生时代的“操作系统”,熟悉和使用它是每名用户的必备技能.本篇文章概述了容器服务 Kubernetes 的知识图谱,部分内容参考了网上的知识图谱,旨在帮助用户更好的了 ...

  2. [转]深入理解ajax系列——进度事件

    一般地,使用readystatechange事件探测HTTP请求的完成.XHR2规范草案定义了进度事件Progress Events规范,XMLHttpRequest对象在请求的不同阶段触发不同类型的 ...

  3. gitlab上fork别人的代码后更新的2种解决方式

    1.解决方式1 首先要先确定一下是否建立了主repo的远程源: git remote -v如果里面只能看到你自己的两个源(fetch 和 push),那就需要添加主repo的源: git remote ...

  4. Jboss 默认加载项目访问

    修改JBOSS的server.xml路径为: D:\Program Files\jboss-4.2.2.GA\server\default\deploy\jboss-web.deployer\serv ...

  5. vue 使用 element ui动态添加表单

    html部分 <div class="hello"> <el-form :model="dynamicValidateForm" ref=&q ...

  6. 洛谷P1314 [NOIP2011提高组Day2T2] 聪明的质监员

    P1314 聪明的质监员 题目描述 小T 是一名质量监督员,最近负责检验一批矿产的质量.这批矿产共有 n 个矿石,从 1到n 逐一编号,每个矿石都有自己的重量 wi 以及价值vi .检验矿产的流程是: ...

  7. vim中NERDTREE插件的使用

    一个显示目录树的插件,很不错 学习于: http://blog.csdn.net/xiongzhengxiang/article/details/7375607

  8. hdu 2444 The Accomodation of Students(二分匹配 匈牙利算法 邻接表实现)

    The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  9. LintCode刷题笔记--Longest Increasing Subsequence

    标签: 动态规划 描述: Given a sequence of integers, find the longest increasing subsequence (LIS). You code s ...

  10. python3.7 安装gensim使用word2Vec库

    应用的文章(个人试验过,完全正确):https://radimrehurek.com/gensim/index.html#install