如何使用Spring Security手动验证用户
1.概述
在这篇快速文章中,我们将重点介绍如何以编程方式在Spring Security和Spring MVC中设置经过身份验证的用户。
2. Spring Security
简而言之,Spring Security在ThreadLocal中保存每个经过身份验证的用户的主要信息 - 保存的是Authentication对象。
为了构造和设置此Authentication对象,通常我们需要使用Spring Security在标准身份验证上构建对象的相同方法。
要让我们手动触发身份验证,然后将生成的身份验证对象设置为框架用来保存当前登录用户的当前SecurityContext:
UsernamePasswordAuthenticationToken authReq
= new UsernamePasswordAuthenticationToken(user, pass);
Authentication auth = authManager.authenticate(authReq);
SecurityContext sc = SecurityContextHolder.getContext();
securityContext.setAuthentication(auth);
在上下文中设置身份验证后,我们现在可以使用securityContext.getAuthentication()。isAuthenticated()检查当前用户是否经过身份验证。
3. Spring MVC
默认情况下,Spring Security在Spring Security过滤器链中添加了一个额外的过滤器。它能够持久化Security 上下文(SecurityContextPersistenceFilter类)。
反过来,它将Security上下文的持久性委托给SecurityContextRepository的实例,默认为HttpSessionSecurityContextRepository类。
因此,为了在请求上设置身份验证并因此使其可用于来自客户端的所有后续请求,我们需要在HTTP会话中手动设置包含身份验证的SecurityContext:
public void login(HttpServletRequest req, String user, String pass) {
UsernamePasswordAuthenticationToken authReq
= new UsernamePasswordAuthenticationToken(user, pass);
Authentication auth = authManager.authenticate(authReq);
SecurityContext sc = SecurityContextHolder.getContext();
sc.setAuthentication(auth);
HttpSession session = req.getSession(true);
session.setAttribute(SPRING_SECURITY_CONTEXT_KEY, sc);
}
SPRING_SECURITY_CONTEXT_KEY是静态导入的HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY。
应该注意的是,我们不能直接使用HttpSessionSecurityContextRepository - 因为它与SecurityContextPersistenceFilter一起使用。
这是因为过滤器使用存储库来加载和存储Security上下文在前,在链中执行其余已定义的过滤器在后,但是它在传递给链的响应上使用自定义包装器。。
因此,在这种情况下,您应该知道所使用的包装器的类类型,并将其传递给存储库中的相应save方法。
4.总结
在这个快速教程中,我们讨论了如何在Spring Security上下文中手动设置用户身份验证以及如何使其可用于Spring MVC的目标。专注于代码示例,说明实现它的最简单方法。
与往常一样,可以在GitHub上找到代码示例。
如何使用Spring Security手动验证用户的更多相关文章
- 使用 Spring Security 手动验证用户
1.概述 在这篇快速文章中,我们将重点介绍如何在 Spring Security 和 Spring MVC 中手动验证用户的身份. 2.Spring Security 简单地说,Spring Secu ...
- Spring security 获取当前用户
spring security中当前用户信息 1:如果在jsp页面中获取可以使用spring security的标签库 在页面中引入标签 1 <%@ taglib prefix=" ...
- Spring Security默认的用户登录表单 页面源代码
Spring Security默认的用户登录表单 页面源代码 <html><head><title>Login Page</title></hea ...
- spring security实现记录用户登录时间等信息
目录 spring security实现记录用户登录时间等信息 一.原理分析 二.实现方式 2.1 自定义AuthenticationSuccessHandler实现类 2.2 在spring-sec ...
- Spring Security 入门—内存用户验证
简介 作为 Spring 全家桶组件之一,Spring Security 是一个提供安全机制的组件,它主要解决两个问题: 认证:验证用户名和密码: 授权:对于不同的 URL 权限不一样,只有当认证的用 ...
- Spring Security 安全验证
摘自:https://www.cnblogs.com/shiyu404/p/6530894.html 这篇文章是对Spring Security的Authentication模块进行一个初步的概念了解 ...
- Spring Security登录验证流程源码解析
一.登录认证基于过滤器链 Spring Security的登录验证流程核心就是过滤器链.当一个请求到达时按照过滤器链的顺序依次进行处理,通过所有过滤器链的验证,就可以访问API接口了. SpringS ...
- Spring Security实现禁止用户重复登陆(配置及原理)
系统使用了Spring Security做权限管理,现在对于系统的用户,需要改动配置,实现无法多地登陆. 一.SpringMVC项目,配置如下: 首先在修改Security相关的XML,我这里是s ...
- spring security LDAP获取用户信息
很多企业内部使用LDAP保存用户信息,这章我们来看一下如何从LDAP中获取Spring Security所需的用户信息. 首先在pom.xml中添加ldap所需的依赖. <dependency& ...
随机推荐
- vim 模式下的几个快捷用法
1.ctrl + v (-- VISUAL BLOCK --) 选中块模式,y 复制,d 剪切,p 粘贴,Esc退出模式 2.Shift + v (-- VISUAL LINE -- ) 快速行选 ...
- python后台架构Django教程——manage.py命令
一.manage.py命令选项 manage.py是每个Django项目中自动生成的一个用于管理项目的脚本文件,需要通过python命令执行.manage.py接受的是Django提供的内置命令. 内 ...
- thinkjs,promise
thinkjs是奇舞团开源的一款NodejsMVC框架,该框架底层基于Promise来实现,很好的解决了Nodejs里异步回调的问题. 可参考: http://www.thinkjs.org/ htt ...
- bzoj 3680(洛谷1337) 吊打XXX——模拟退火
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3680 https://www.luogu.org/problemnew/show/P1337 ...
- POJ1330(LCA入门题)
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 23388 Accept ...
- [yii]Trying to get property of non-object
今天接触gridview的时候,发现总是报错,如图. array( 'name'=>'user_info.userinfo', 'value'=>'$data->user_info- ...
- java.endorsed.dirs的作用
java.endorsed.dirs java.ext.dirs 用于扩展jdk的系统库,那么 -Djava.endorsed.dirs 又有什么神奇的作用呢? java提供了endorsed技术 ...
- 交互原型设计软件axure rp学习之路(一)
开始学习之前,请一定要明白:Axure是个极其极其极其简单的软件.因为你所做的就是拖部件,打字,拖部件,打字,最多加个对齐.如果你能把axure当作word或者ppt来用,那你就学会了axure. A ...
- The web.config file for this project is missing the required DirectRequestModule.
The web.config file for this project is missing the required DirectRequestModule. 将应用程序集的模式由集成改为经典 ...
- js混杂笔记
1.判断对象为空的方法 1)Object.keys({}).length === 0 // true 2)Object.getOwnPropertyNames({}).length === 0 // ...