Spring Security3默认过滤器链的第一个过滤器就是org.springframework.security.web.context.SecurityContextPersistenceFilter,这个Filter就是持久化SecurityContext实例的,流程图:
看SecurityContextPersistenceFilter代码:
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 request chain.doFilter(request, response); return; } final boolean debug = logger.isDebugEnabled(); request.setAttribute(FILTER_APPLIED, Boolean.TRUE); if (forceEagerSessionCreation) { HttpSession session = request.getSession(); if (debug && session.isNew()) { logger.debug("Eagerly created session: " + session.getId()); } } HttpRequestResponseHolder holder = new HttpRequestResponseHolder(request, response); // 从SecurityContextRepository中获取SecurityContext实例 SecurityContext contextBeforeChainExecution = repo.loadContext(holder); try { // 将SecurityContext实例到SecurityContextHolder中,后面的过滤器以及我们在程序中一般都从这个holder中拿SecurityContext实例 SecurityContextHolder.setContext(contextBeforeChainExecution); chain.doFilter(holder.getRequest(), holder.getResponse()); } finally { // 先获取SecurityContext实例 SecurityContext contextAfterChainExecution = SecurityContextHolder.getContext(); // Crucial removal of SecurityContextHolder contents - do this before anything else. //再从holder中清除 SecurityContextHolder.clearContext(); // 当SecurityContext实例被update,或session中SecurityContext实例为空,将其放入session中 repo.saveContext(contextAfterChainExecution, holder.getRequest(), holder.getResponse()); request.removeAttribute(FILTER_APPLIED); if (debug) { logger.debug("SecurityContextHolder now cleared, as request processing completed"); } } }
分析一下HttpSessionSecurityContextRepository的loadContext方法:
public SecurityContext loadContext(HttpRequestResponseHolder requestResponseHolder) { HttpServletRequest request = requestResponseHolder.getRequest(); HttpServletResponse response = requestResponseHolder.getResponse(); HttpSession httpSession = request.getSession(false); // 从session中获取SecurityContext对象 SecurityContext context = readSecurityContextFromSession(httpSession); if (context == null) { if (logger.isDebugEnabled()) { logger.debug("No SecurityContext was available from the HttpSession: " + httpSession +". " + "A new one will be created."); } // 如果SecurityContext对象是空的,创建一个SecurityContext实例 context = generateNewContext(); } // 注意:此处用SaveToSessionResponseWrapper替换了原生的httpresponse对象,可能和自定义的response wrapper对象有冲突。 requestResponseHolder.setResponse(new SaveToSessionResponseWrapper(response, request, httpSession != null, context.hashCode())); return context; }
这个过滤器比较简单,它维护了SecurityContextHolder中SecurityContext实例,我们也很方便的从SecurityContextHolder中获取SecurityContext对象
相关推荐
Spring Security-3.0.1 中文官方文档(翻译版) 这次发布的Spring Security-3.0.1 是一个bug fix 版,主要是对3.0 中存在的一些问题进 行修 正。文档中没有添加新功能的介绍,但是将之前拼写错误的一些类名进行...
3. Spring Security 4.2的新特性 27 3.1 Web改进 27 3.2配置改进 28 3.3杂项 28 4.样品和指南(从这里开始) 28 5. Java配置 29 5.1 Hello Web安全Java配置 29 5.1.1 AbstractSecurityWebApplicationInitializer 31 ...
1.1. Spring Security是什么? 1.2. 历史 1.3. 发行版本号 1.4. 获得Spring Security 1.4.1. 项目模块 1.4.1.1. Core - spring-security-core.jar 1.4.1.2. Web - spring-security-web.jar 1.4.1.3. ...
1.1. Spring Security是什么? 1.2. 历史 1.3. 发行版本号 1.4. 获得Spring Security 1.4.1. 项目模块 1.4.1.1. Core - spring-security-core.jar 1.4.1.2. Web - spring-security-web.jar 1.4.1.3. Config -...
Spring-security-oauth-poc 这是一个展示 Spring 与 OAUTH 集成的 POC 下面的过程描述了如何运行这个应用程序###Step 1 此步骤描述了如何访问该服务。 当我们直接访问受保护的服务时,我们会从该服务获得未经授权的...
基于SpringSecurity4.2.2权限框架搭建教程的详细源码,实现用户登录时,验证其合法性,并通过security访问数据库可以获取到相对应角色和资源信息存储到SecurityContext里面等功能。
springboot_springsecurity_jwt_demo
受 Spring Security CAS 客户端保护的 Java webapp ( ) Maven 演示使用来自 Spring Security 项目 (v3.2.5) 的 CAS 客户端来保护 Web 应用程序。 使用mvn clean compile jetty:run在上启动 webapp。 url '...
Spring-boot库可使用Firebase集成安全性。 此外,它还使用内部微服务之间的预共享密码来提供基本身份验证。 介绍 这些项目使用X-Firebase-Auth请求标头提供firebase安全性。 使用FirebaseAuth.verifyIdTokenAsync(id...
seccomp.security.alpha.kubernetes.io/allowedProfileNames: docker/default seccomp.security.alpha.kubernetes.io/defaultProfileName: docker/default apparmor.security.beta.kubernetes.io/...
非根调试容器 Ubuntu容器和一些其他工具。 容器被创建为以非根用户身份运行。 容器已上传到 ... --overrides= ' {"spec":{"securityContext":{"runAsUser":1000,"runAsGroup":1000}}} ' \ ${ * } }
克拉肯德·赫尔姆 在kubernetes集群中部署krakend的头盔图表 安装api-gateway系统 TL; DR; $ helm install krakend-helm 安装图表 要使用发布名称my-release安装... securityContext 部署安全性 {} image.repository
SecurityContext,用于编程登录和主体/主题的单一位置 地位 没有成功 -> 但可能是因为我无法将所需的代码集成到服务器核心中。 问题 我们如何使用 JASPIC 在 web.xml 中定义身份验证方法? <auth>FORM <realm>...
尽管仍然可以以根用户身份运行容器,但是Webhook确保只有在Pod的securityContext runAsNonRoot设置显式设置为false ,才有可能。 如果没有为runAsNonRoot设置任何值,那么将应用默认值true ,并且用户ID默认为1234 ...
例子由于nginx容器已启用allowPrivilegeEscalation因此以下Pod将被拒绝: apiVersion : v1kind : Podmetadata : name : nginxspec : containers : - name : nginx image : nginx securityContext : ...
特征使用 SecurityContext 确定访问资源的用户的角色如何使用客户端删除资源如何使资源不可修改目标metadata.json 文件中指定的目标是为 /ephemeralscript/storage/emphemeral 路径下的每次访问设置的,这意味着只有...
- Redis集群/独立模式设置带有Prometheus出口商的内置监控使用pvc模板进行动态存储配置k8s请求和限制的资源限制密码/无密码设置节点选择器和关联优先级管理设置优先级SecurityContext操作内核参数入门如果要从头开始...
蜜蜂netem 此docker镜像用于模拟更实际的网络条件以进行蜜蜂测试。 它具有几个预定义的配置文件[ netemCmd ...securityContext: capabilities: add: - NET_ADMIN netemCmd: "wifi" beeCommand: ["/entrypoint.sh"]
Conftest帮助您针对结构化配置数据编写测试。 使用Conftest,您可以为Kubernetes配置,Tekton管道定义,Terraform代码,无服务器配置或任何其他配置文件编写测试。 Conftest使用来自的Rego语言编写断言。 您可以在...
SampleJerseyREST Jersey 示例,包含过滤器、securityContext 和即将推出的 oauth2