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

ops/2024/10/18 7:55:39/

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/ops/84777.html

相关文章

MySQL中,除了使用LIKE进行模糊搜索外,还有其他几种方法可以执行搜索操作

在PHP和MySQL中,除了使用LIKE进行模糊搜索外,还有其他几种方法可以执行搜索操作,具体使用哪种方法取决于你的具体需求(如性能、精确度、查询的复杂性等)。以下是一些常用的搜索方法: REGEXP 或 RLIKE&…

二阶段测试:

二阶段测试: 架构: 服务器类型部署组件ip地址DR1调度服务器 主(ha01)KeepalivedLVS-DR192.168.60.30DR2调度服务器 备 (ha02)KeepalivedLVS-DR192.168.60.40web1节点服务器 (slave01)NginxTomcatMySQL 备MHA managerMHA node192.…

后端返回列表中包含图片id,如何将列表中的图片id转化成url

问题描述 如果我有一个列表数据,列表中每个对象都包含一个图片id,现在我需要将列表中的图片id转化成图片,然后再页面上显示出来 如果你有一个列表数据,列表中每个对象都包含一个图片 ID,并且你想将这些图片 ID 转化为…

ffmpeg把pcm封装为wav

note 1.wav格式中,音频数据未经过压缩,直接封装即可 2.对于编码器的选择,应选择和pcm裸数据一致的编码器(本次实际不须编码) version #define LIBSWRESAMPLE_VERSION_MAJOR 2 #define LIBSWRESAMPLE_VERSION_MINOR 9 #define LIBSWRESAM…

【常见开源库的二次开发】基于openssl的加密与解密——SHA算法源码解析(六)

目录 一、SHA-1算法分析: 1.1 Merkle Tree可信树 1.2 源码实现: 1.3 哈希计算功能 1.4 两种算法的区别: 1.4.1 目的 1.4.2 实现机制 1.4.3 输出 1.4.4 应用场景: 1.4 运行演示: 二、SHA-2算法分析: 2.1哈…

在线教育数仓项目(数据采集部分1)

文章目录 数据仓库概念项目需求及架构设计项目需求分析系统数据流程设计框架版本选型集群规模估算集群资源规划设计 数据生成模块目标数据页面事件曝光启动播放错误 数据埋点主流埋点方式(了解)埋点数据上报时机埋点数据日志结构 服务器和JDK准备服务器准…

VUE 基础(一)

(直接在vscode上运行就可以&#xff0c;建一个html文件) 1 el的使用 Vue会管理el选项命中的元素及其内部的后代元素 <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><meta name"viewport" content…

AutoAgents: A Framework for Automatic AgentGeneration

https://arxiv.org/abs/2309.17288https://arxiv.org/abs/2309.17288 1.概述 大语言模型(LLM)已展现出作为通用任务解决智能体的卓越能力,其知识储备与技能水平令人瞩目。然而,在面对需要高度密集知识与复杂推理的任务时,如预防幻觉、采用深度思考策略、确保信息可信度以…