springSecurity学习之springSecurity web如何取得用户信息

devtools/2024/9/20 7:10:45/ 标签: java

web如何取得用户信息

之前说过SecurityContextHolder默认使用的是ThreadLocal来进行存储的,而且每次都会清除,但是web每次请求都会验证用户权限,这是如何做到的呢?

这是通过SecurityContextPersistenceFilter来实现的,每次请求过来都会session中来获取SecurityContext,然后设置到SecurityContextHolder中,请求结束后再清除掉

java">public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)throws IOException, ServletException {HttpServletRequest request = (HttpServletRequest) req;HttpServletResponse response = (HttpServletResponse) res;if (request.getAttribute(FILTER_APPLIED) != null) {// ensure that filter is only applied once per requestchain.doFilter(request, response);return;}request.setAttribute(FILTER_APPLIED, Boolean.TRUE);HttpRequestResponseHolder holder = new HttpRequestResponseHolder(request,response);// 从session获取SecurityContextSecurityContext contextBeforeChainExecution = repo.loadContext(holder);try {// 将SecurityContext存入SecurityContextHolderSecurityContextHolder.setContext(contextBeforeChainExecution);chain.doFilter(holder.getRequest(), holder.getResponse());}finally {SecurityContext contextAfterChainExecution = SecurityContextHolder.getContext();// Crucial removal of SecurityContextHolder contents - do this before anything// else.SecurityContextHolder.clearContext();// 存储SecurityContextrepo.saveContext(contextAfterChainExecution, holder.getRequest(),holder.getResponse());request.removeAttribute(FILTER_APPLIED);}
}

loadContext获取SecurityContext

使用HttpSessionSecurityContextRepository

java">public SecurityContext loadContext(HttpRequestResponseHolder requestResponseHolder) {HttpServletRequest request = requestResponseHolder.getRequest();HttpServletResponse response = requestResponseHolder.getResponse();HttpSession httpSession = request.getSession(false);// 从session中获取SecurityContextSecurityContext context = readSecurityContextFromSession(httpSession);if (context == null) {context = generateNewContext();}SaveToSessionResponseWrapper wrappedResponse = new SaveToSessionResponseWrapper(response, request, httpSession != null, context);requestResponseHolder.setResponse(wrappedResponse);return context;
}

从session中获取SecurityContext

java">private SecurityContext readSecurityContextFromSession(HttpSession httpSession) {final boolean debug = logger.isDebugEnabled();if (httpSession == null) {return null;}// Session exists, so try to obtain a context from it.Object contextFromSession = httpSession.getAttribute(springSecurityContextKey);if (contextFromSession == null) {return null;}// Everything OK. The only non-null return from this method.return (SecurityContext) contextFromSession;
}

存储SecurityContext

java">public void saveContext(SecurityContext context, HttpServletRequest request,HttpServletResponse response) {SaveContextOnUpdateOrErrorResponseWrapper responseWrapper = WebUtils.getNativeResponse(response,SaveContextOnUpdateOrErrorResponseWrapper.class);if (!responseWrapper.isContextSaved()) {responseWrapper.saveContext(context);}
}
java">protected void saveContext(SecurityContext context) {final Authentication authentication = context.getAuthentication();HttpSession httpSession = request.getSession(false);// See SEC-776if (authentication == null || trustResolver.isAnonymous(authentication)) {if (httpSession != null && authBeforeExecution != null) {// SEC-1587 A non-anonymous context may still be in the session// SEC-1735 remove if the contextBeforeExecution was not anonymoushttpSession.removeAttribute(springSecurityContextKey);}return;}if (httpSession == null) {httpSession = createNewSessionIfAllowed(context);}// If HttpSession exists, store current SecurityContext but only if it has// actually changed in this thread (see SEC-37, SEC-1307, SEC-1528)if (httpSession != null) {// We may have a new session, so check also whether the context attribute// is set SEC-1561if (contextChanged(context)|| httpSession.getAttribute(springSecurityContextKey) == null) {// 存到session中httpSession.setAttribute(springSecurityContextKey, context);}}
}

https://zhhll.icu/2024/框架/springSecurity/7.web如何取得用户信息/


http://www.ppmy.cn/devtools/85013.html

相关文章

Spring Boot入门指南:留言板

一.留言板 1.输⼊留⾔信息,点击提交.后端把数据存储起来. 2.⻚⾯展⽰输⼊的表⽩墙的信息 规范: 1.写一个类MessageInfo对象,添加构造方法 虽然有快捷键,但是还是不够偷懒 项目添加Lombok。 Lombok是⼀个Java⼯具库,通过添加注…

ElasticSearch(四)— 数据检索与查询

一、基本查询语法 所有的 REST 搜索请求使用_search 接口,既可以是 GET 请求,也可以是 POST请求,也可以通过在搜索 URL 中指定索引来限制范围。 _search 接口有两种请求方法,一种是基于 URI 的请求方式,另一种是基于…

IEC104转MQTT网关轻松将IEC104设备数据传输到Zabbix、阿里云、华为云、亚马逊AWS、ThingsBoard、Ignition云平台

随着工业4.0的深入发展和物联网技术的广泛应用,IEC 104(IEC 60870-5-104)作为电力系统中的重要通信协议,正逐步与各种现代监控、管理和云平台实现深度融合。IEC104转MQTT网关BE113作为这一融合过程中的关键设备,其能够…

构建坚不可摧的Memcached:高可用性策略全解析

🛡️ 构建坚不可摧的Memcached:高可用性策略全解析 Memcached是一个广泛使用的高性能分布式内存缓存系统,它通过减少对数据库的访问来加速数据检索。然而,为了确保服务的连续性和数据的可靠性,高可用性设计成为Memcac…

第13周 简历职位功能开发与Zookeeper实战

第13周 简历职位功能开发与Zookeeper实战 本章概述1. Mysql8窗口函数over使用1.1 演示表结构与数据1.2 案例1:获取男女总分数1.3 案例2****************************************************************************************本章概述 1. Mysql8窗口函数over使用 参考案例…

yarn 和 npm 的区别

yarn 和 npm 有以下区别: 工作原理: npm(Node Package Manager) 是 JavaScript 的包管理器,是随 Node.js 自带的工具。它通过访问 Node.js 包注册表来管理软件包的依赖关系和版本。yarn 也是 JavaScript 的包管理器&a…

力扣第三十题——串联所有单词的子串

内容介绍 给定一个字符串 s 和一个字符串数组 words。 words 中所有字符串 长度相同。 s 中的 串联子串 是指一个包含 words 中所有字符串以任意顺序排列连接起来的子串。 例如,如果 words ["ab","cd","ef"], 那么 &quo…

谷粒商城实战笔记-54-商品服务-API-三级分类-拖拽效果

文章目录 一,54-商品服务-API-三级分类-修改-拖拽效果1,el-tree控件加上允许拖拽的属性2,是否允许拖拽3,完整代码 一,54-商品服务-API-三级分类-修改-拖拽效果 本节的主要内容是给三级分类树形结构加上拖拽功能&#…

学习pcie—20240724

因为前一段时间看了xdma的IP核手册,发现只看xdma看不太懂,不清楚pcie的传输的流程,不了解报文格式,所以看看pcie手册,主要关注发送接收时序 首先是pcieIP核与xdmaIP核的区别: Integrated Block for PCI E…

Android 13 大屏显示时关于SystemUI和Launcher3问题

当系统运行在大屏上时,原来显示SystemUI导航栏的位置会变成Launcher3的任务栏,然后导航栏的3个按键显示靠右下角显示 我的博客 1.先看SystemUI的导航栏为什么会消失,移动 /SystemUI/src/com/android/systemui/statusbar/NavigationBarController.javapublic void createNavigat…

LeetCode Hot100 将有序数组转换为二叉搜索树

给你一个整数数组 nums ,其中元素已经按 升序 排列,请你将其转换为一棵 平衡 二叉搜索树。 示例 1: 输入:nums [-10,-3,0,5,9] 输出:[0,-3,9,-10,null,5] 解释:[0,-10,5,null,-3,null,9] 也将被视为正确…

【iOS】—— 类与对象的底层

【iOS】—— 类与对象的底层 1. OC编译生成C代码的方法1.1 clang1.2 xcrun 2. 对象的本质2.1 objc_class 和 objc_object 有什么关系?2.2 objc_object与对象的关系: 3. 类结构3.1 Class isa(1)isa指针是什么? (2&…

MySQL第四次作业

先创建库和表 处理表 1. 修改 student 表中年龄(sage)字段属性,数据类型由 int 改变为 smallint ALTER TABLE student MODIFY sage SMALLINT; 2. 为 Course 表中 Cno 课程号字段设置索引,并查看索引 ALTER TABLE course ADD INDEX index_cno (Cno); …

ARM32开发——PWM蜂鸣器案例

🎬 秋野酱:《个人主页》 🔥 个人专栏:《Java专栏》《Python专栏》 ⛺️心若有所向往,何惧道阻且长 文章目录 需求原来的驱动移植操作替换初始化 更新Play函数完整代码 需求 通过控制PB9来播放音乐,PB9对应的定时器通道&#xff1…

基于上云api前端开发经验教训(loading...)

问题一:部署前端代码时npm报错 由于npm源在国外,出现安装异常或比较慢的情况,使用cnpm(淘宝镜像)来解决。 安装cnpm npm install -g cnpm --registryhttp://registry.npmmirror.com使用cnpm(同npm一样) cnpm install

【MySQL进阶之路 | 高级篇】范式概述与第一范式

1. 范式简介 在关系型数据库中,关于数据表的设计的基本原则,规则就称为范式。可以理解为,一张数据表的设计结果需要满足的某种设计标准的级别。要想设计一个结构合理的关系型数据库,必须满足一定的范式。 范式的英文名是Normal …

苍穹外卖(一)之环境搭建篇

Ngnix启动一闪而退 启动之前需要确保ngnix.exe的目录中没有中文字体,在conf目录下的nginx.conf文件查看ngnix的端口号,一般默认为80,若80端口被占用就会出现闪退现象。我们可以通过logs/error.log查看错误信息,错误信息如下&…

前端路由快速上手-React-Router

1. 前端路由简介 前端路由是一种在单页面应用(SPA)中实现页面跳转的技术,它允许我们通过改变URL地址而无需重新加载页面来显示不同的内容。在前端路由中,每个路径(path)都对应一个组件(compone…

C语言学习笔记

前言 ———————————————————— ——c语言是各大编程技术的基础,成为优秀的程序员,应当对c熟练掌握并且拥有自己的理解。我将在这里持续更新本人从零基础开始学习c语言中的学习笔记,个人理解及感受。一方面为了记录我在学习中…

11 - FFmpeg - 编码 AAC

Planar 模式是 ffmpeg内部存储模式,我们实际使用的音频文件都是Packed模式的。 FFmpeq解码不同格式的音频输出的音频采样格式不是一样。 其中AAC解码输出的数据为浮点型的 AV_SAMPLE_FMT_FLTP 格式,MP3 解码输出的数据为 AV_SAMPLE_FMT_S16P 格式(使用的…