如何设置 Spring Security SecurityContextHolder 策略?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3467918/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 04:06:38  来源:igfitidea点击:

How to set up Spring Security SecurityContextHolder strategy?

springspring-security

提问by viator

I'm using asynchronous methods in my service (Spring 3 @Asyncannotation). And I've got a problem - spawned thread doesn't have security context. Cause of it is Spring Security by default uses SecurityContextHolder.MODE_THREADLOCALstrategy for its context holder. But I need to use SecurityContextHolder.MODE_INHERITABLETHREADLOCALstrategy. For the moment I set up strategy in my AuthenticationSuccessHandler. But in my point of view it's not a good practice.

我在我的服务中使用异步方法(Spring 3@Async注释)。我遇到了一个问题 - 生成的线程没有安全上下文。原因是 Spring Security 默认SecurityContextHolder.MODE_THREADLOCAL为其上下文持有者使用策略。但我需要使用SecurityContextHolder.MODE_INHERITABLETHREADLOCAL策略。目前,我在AuthenticationSuccessHandler 中设置了策略。但在我看来,这不是一个好习惯。

So how can I set it up in context configuration file?
Version of spring security is 3.0.0.

那么如何在上下文配置文件中设置它呢?
spring security 的版本是 3.0.0。

回答by Gandalf

You can set the environment variable spring.security.strategyto MODE_INHERITABLETHREADLOCAL. You could also have a simple bean that during your web applications startup calls SecurityContextHolder.setStrategyName(SecurityContextHolder.MODE_INHERITABLETHREADLOCAL)and initialize that value in your context configuration file.

您可以将环境变量设置spring.security.strategyMODE_INHERITABLETHREADLOCAL. 您还可以拥有一个简单的 bean,它在您的 Web 应用程序启动期间调用SecurityContextHolder.setStrategyName(SecurityContextHolder.MODE_INHERITABLETHREADLOCAL)并在您的上下文配置文件中初始化该值。

SecurityContextHolder API

SecurityContextHolder API

回答by Matt Broekhuis

The java config for @viator 's answer if it helps you.

@viator 答案的 java 配置如果对你有帮助的话。

@Bean
public MethodInvokingFactoryBean methodInvokingFactoryBean() {
    MethodInvokingFactoryBean methodInvokingFactoryBean = new MethodInvokingFactoryBean();
    methodInvokingFactoryBean.setTargetClass(SecurityContextHolder.class);
    methodInvokingFactoryBean.setTargetMethod("setStrategyName");
    methodInvokingFactoryBean.setArguments(new String[]{SecurityContextHolder.MODE_INHERITABLETHREADLOCAL});
    return methodInvokingFactoryBean;
}

回答by Taras Melon

A little bit another solution, like @viator write:

另一种解决方案,如@viator 写:

<bean
        class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="targetClass"
            value="org.springframework.security.core.context.SecurityContextHolder" />
        <property name="targetMethod" value="setStrategyName" />
        <property name="arguments" value="MODE_INHERITABLETHREADLOCAL" />
    </bean>

Working like a charm.

像魅力一样工作。