如何使用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& ...
随机推荐
- 机器学习:决策树--python
今天,我们介绍机器学习里比较常用的一种分类算法,决策树.决策树是对人类认知识别的一种模拟,给你一堆看似杂乱无章的数据,如何用尽可能少的特征,对这些数据进行有效的分类. 决策树借助了一种层级分类的概念, ...
- AtCoder AGC #3 Virtual Participation
Havana真好听qwq AB题就不写了 SB C.BBuBBBlesort! 有一个长度为$n$的数列 你每次可以用两种操作 1.交换两个相邻元素 2.交换两个隔且仅隔了一个的元素 求把数列排成有序 ...
- python之系统编程 --进程
1.调试(PDB) 代码: [root@master gaoji]# vim test2.py 1 #!/usr/local/bin/python3 2 # -*- coding:utf-8 -*- ...
- 一:AMQP协议标准简单介绍
一:AMQP协议?--->AMQP 是 Advanced Message Queuing Protocol,即高级消息队列协议.和前面罗列的技术不同,AMQP 是一个标准化的消息中间件协议--- ...
- ACM学习历程—HDU1028 Ignatius and the Princess(组合数学)
Ignatius and the Princess Description "Well, it seems the first problem is too easy. I w ...
- 【Lintcode】 035.Reverse Linked List
题目: Reverse a linked list. Example For linked list 1->2->3, the reversed linked list is 3-> ...
- RMI RPC socket
1.RPC RPC(Remote Procedure Call Protocol)远程过程调用协议,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议.RPC不依赖于具体的 ...
- TCP/IP详解卷1 - wireshark抓包分析
TCP/IP详解卷1 - 系列文 TCP/IP详解卷1 - 思维导图(1) TCP/IP详解卷1 - wireshark抓包分析 引言 在初学TCP/IP协议时,会觉得协议是一种很抽象的东西,通过wi ...
- Python3解leetcode Symmetric Tree
问题描述: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). ...
- UDK更改启动画面及载入动画
转自:http://www.unrealchina.org/forum.php?mod=viewthread&tid=246&extra=page%3D1 方法很简单: 1.更改启动画 ...