step1:首先ApplicationUserDetailsService需要实现UserDetailsService接口(在

org.springframework.security.core.userdetails里面),实现获取用户Details信息的回调函数,必须要实现

loadUserByUsername方法,注意这里的User实现了UserDetails,CredentialsContainer,User类里面的参数分别有:

password,username,authorities,accountNonExpired,accountNonLocked,credentialsNonExpired,enable,

代码如下:

我们可以自定义添加新的User信息,这里我们自定义一个SimpleUser类继承User。

代码如下:

注意这里的SimpleUser必须继承User,而且必须继承上面这5个属性和hashCode,equals方法,构造方法中必须给上述这5个

属性赋值,ApplicationUserDetailsService调用构造方法,生成一个SimpleUser对象。如果user不存在,抛出一个

UsernameNotFoundException异常。

代码如下:

step2:loadUserByUsername方法返回后,会跳到DaoAuthenticationProvider(在

org.springframework.security.authentication.dao)类中的retrieveUser方法中

step3:retrieveUser方法返回后,会跳到AbstractUserDetailsAuthenticationProvider(在

org.springframework.security.authentication.dao)authenticate方法中,这个方法会先从缓存中查找user。

如果没有的话会从上一步DaoAuthenticationProvider的loadUserByUsername方法中把user查出来,这里会抛出

UserNameNotFoundException这个异常,应该是用户不存在这个错误,即Bad credentials。

step4:authenticate方法preAuthenticationChecks.check(user),这里进行基本有效性验证(是否有效,是否

被锁,是否过期)代码如下:

step5:authenticate方法中additionalAuthenticationChecks(user,

(UsernamePasswordAuthenticationToken) authentication)这里进行方法验证。代码如下:

Spring security 浅谈用户验证机制的更多相关文章

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

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

  2. Spring security 获取当前用户

    spring security中当前用户信息 1:如果在jsp页面中获取可以使用spring security的标签库 在页面中引入标签   1 <%@ taglib prefix=" ...

  3. Spring Security默认的用户登录表单 页面源代码

    Spring Security默认的用户登录表单 页面源代码 <html><head><title>Login Page</title></hea ...

  4. spring security实现记录用户登录时间等信息

    目录 spring security实现记录用户登录时间等信息 一.原理分析 二.实现方式 2.1 自定义AuthenticationSuccessHandler实现类 2.2 在spring-sec ...

  5. 详解Spring Security的HttpBasic登录验证模式

    一.HttpBasic模式的应用场景 HttpBasic登录验证模式是Spring Security实现登录验证最简单的一种方式,也可以说是最简陋的一种方式.它的目的并不是保障登录验证的绝对安全,而是 ...

  6. Spring Security实现禁止用户重复登陆(配置及原理)

    系统使用了Spring Security做权限管理,现在对于系统的用户,需要改动配置,实现无法多地登陆.   一.SpringMVC项目,配置如下: 首先在修改Security相关的XML,我这里是s ...

  7. 深入Spring Security魔幻山谷-获取认证机制核心原理讲解(新版)

    文/朱季谦 本文基于Springboot+Vue+Spring Security框架而写的原创学习笔记,demo代码参考<Spring Boot+Spring Cloud+Vue+Element ...

  8. spring security LDAP获取用户信息

    很多企业内部使用LDAP保存用户信息,这章我们来看一下如何从LDAP中获取Spring Security所需的用户信息. 首先在pom.xml中添加ldap所需的依赖. <dependency& ...

  9. spring security使用数据库管理用户权限

    <authentication-provider> <user-service> <user name="admin" password=" ...

随机推荐

  1. 编写一个程序,将 a.txt 文件中的单词与 b.txt 文件中的单词交替合并到 c.txt 文件中,a.txt 文件中的单词用回车符分隔,b.txt 文件中用回车或空格进行分隔。

    package IO; import java.io.*; public class test { public void connectWords(File file1, File file2, F ...

  2. caffe2 教程入门(python版)

    学习思路 1.先看官方文档,学习如何使用python调用caffe2包,包括 Basics of Caffe2 - Workspaces, Operators, and Nets Toy Regres ...

  3. 智课雅思词汇---二十五、形容词后缀-ate-fic-ose-ulent-olent-ous-ulous-y

    智课雅思词汇---二十五.形容词后缀-ate-fic-ose-ulent-olent-ous-ulous-y 一.总结 一句话总结: 1.形容词后缀-ate(determinate)? determi ...

  4. LCS 的 NlogN作法

    这个算法其实是因为LIS有NlogN的作法,把LCS转化为了LIS来做. 对于序列A{},B{},我们可以记录下来B中的每个元素在A中出现的位置,按顺序保存在一个新序列当中, 如果有多个位置按倒序写, ...

  5. Node.js小白开路(一)-- Buffer篇

    Buffer是nodeJS中的二进制缓存操作模块内容.先来看一段简短的代码. // 创建一个长度为 10.且用 0 填充的 Buffer. const buf1 = Buffer.alloc(10); ...

  6. 转:oracle驱动表

    以一个比较两本字典来做例子:       一本字典有索引目录(dict a), 一本没有(dict b)       现在要找出所有a开头的单词的异同       那么比较的时候,你会怎么比较?    ...

  7. Prism5.0新内容 What's New in Prism Library 5.0 for WPF(英汉对照版)

    Prism 5.0 includes guidance in several new areas, resulting in new code in the Prism Library for WPF ...

  8. javaScript-条件语句优化

    1.多重判断时使用 Array.includes test = (fruit: string) => { if (fruit == 'apple' || fruit == 'strawberry ...

  9. .Net版SQLite无法访问网络位置的数据库文件-winOpen,os_win.c 36702异常

    最近一个C#小程序,希望将SQLite数据库放在网络共享的位置,让多个客户端同时访问.却发现SQLite连接不上该网络位置的数据库,而如果数据库在本地则一切正常. 例如将SQLite数据库 test. ...

  10. Ubuntu+Rmarkdown的中文slides实现(附GitHub template)

    这两天要做毕业论文的答辩slides,搜Rmarkdown中文slides的时候百度到了自己两年前的博客 R+markdown+LaTeX 中文编译解决方案.讲真我一开始还真没有认出来,一看这文风和博 ...