- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我创建了一个 @Aspect 类并尝试获取主要对象,例如..
SecurityContextHolder.getContext().getAuthentication() .getPrincipal()
在我的方面类中,但我得到了空指针。我的方面没有可用的上下文。有任何指示吗?
最佳答案
SecurityContextHolder将给定的 SecurityContext 与当前执行线程关联。由于方面在单独的线程上拦截方法(不太确定),因此您可能需要更改安全上下文持有者策略。如SecurityContextHolder提供一系列委托(delegate)给 SecurityContextHolderStrategy 实例的静态方法。 SecurityContextHolder的目的的目的是提供一种方便的方法来指定应该用于给定 JVM 的策略。
如果没有定义策略 SecurityContextHolder类将默认使用 MODE_THREADLOCAL。
因此需要将策略更改为 MODE_INHERITABLETHREADLOCAL。
有两种方法可以指定所需的策略模式。第一种是通过 SYSTEM_PROPERTY 上键入的系统属性来指定它。第二个是调用 setStrategyName(String) before using the class 。
在Spring中,您需要在应用程序上下文中定义一个bean,如下所示:
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetClass"><value>org.springframework.security.core.context.SecurityContextHolder</value></property>
<property name="targetMethod"><value>setStrategyName</value></property>
<property name="arguments">
<list>
<value>MODE_INHERITABLETHREADLOCAL</value>
</list>
</property>
</bean>
关于spring - SecurityContextHolder.getContext() 在 AspectJ 类中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18229619/
回答这个问题securitycontextholder-session-or-request-bound 。我正在使用此示例 how-to-customAuthenticationManager 实现
我使用 SecurityContextHolder 和自定义 UserDetailsService 从 SecurityContextHolder 获取 UserDetails: Object o
我有登录参数 1.userName 2.password 3.companyId 我使用以下代码获得了用户名和密码 Authentication auth = SecurityContextHold
我正在尝试在Spring Boot应用程序中使用Spring Security添加Facebook授权。目前,我的问题是从Principal提取数据。 这是我的安全配置: public class S
我使用这样的代码进行身份验证: @PreAuthorize("isAnonymous()") @RequestMapping(value = "/login", method = RequestMet
我正在阅读 Spring Security 文档,它说 SecurityContextHolder 为具有特定线程行为的应用程序提供了不同类型的策略。例如 - Swing 应用程序。 我知道在网络应用
我遇到了 SecurityContextHolder.getContext().getAuthentication() 的问题,它是 null。我尝试了很多与注释和示例的组合。 (来自站点的代码在我的
我尝试实现存储所有登录的日志文件。 到目前为止,我在 LoginHandler 中添加了一些代码,但总是收到错误: org.springframework.security.core.userdeta
如何通过将ManualUserDetails扩展org.springframework.security.core.userdetails.UserDetails添加到SecurityContextH
我在我的 Spring Boot 项目中处理安全层时遇到了以下问题: SecurityContextHolder.getContext().getAuthentication() 此代码返回: 匿名用
我正在使用 Spring Security 来验证用户身份。 问题是动态更新权限的最佳方法是什么?我想根据请求更新它,现在我只需在用户登录系统后执行一次。 我有基于经理的应用程序,因此管理员可以随时决
我正在基于一些开源项目和网上找到的一些文档,使用 Spring boot 来实现 JWT。 什么在起作用?我能够生成我的 token ,并且在第一次点击时,当我尝试调用一些安全方法时,我被撤销,这很好
我知道这个问题经常被问到,但也许我有一些特别的事情。我正在尝试对支持 REST(不是 Spring MVC)的 Spring Boot 应用程序进行一些集成测试,出于某种原因 SecurityCont
当我尝试模拟时 SecurityContextHolder.getContext().getAuthentication().getCredentials() 要获得 UserDetails 的 Ma
我创建了一个 @Aspect 类并尝试获取主要对象,例如.. SecurityContextHolder.getContext().getAuthentication() .getPrincipal(
我正在实现 MyUsernamePasswordAuthenticationFilter 扩展默认过滤器。 我没有在其中做任何事情。我只是想在这里获取请求以使其变得简单。 public class M
在我的 spring 应用程序中,我对 Controller 方法有一些方面,我在其中进行一些安全检查。因为我需要更频繁地进行多次检查,所以我将它们包装到“sercurityUtil”类的静态辅助方法
我在我的网络项目中使用 SpringSecurity。我有保存在数据库中的实体用户。我需要使用用户 ID 在 BD 中执行请求,以从该 BD 中的其他表获取任何信息。在 Spring Security
我有两个不同的应用程序,例如 A 和 .两者都使用具有相同配置的 Spring Security。这是我的情况: 我登录到我的 A 应用程序。一切正常。但是,当我在同一浏览器的另一个选项卡中登录我的
我一直在尝试为以下代码行编写测试用例,但我不断收到 java.lang.NullPointerException,我已尝试遵循/复制其他人在此处提出的建议 Unit testing with Spri
我是一名优秀的程序员,十分优秀!