gpt4 book ai didi

java - Spring 是否使 SecurityContext 可用于执行 Hystrix 命令的线程

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:05:52 25 4
gpt4 key购买 nike

我正在运行一个 spring boot 应用程序,并且刚刚开始从 spring-cloud-netflix 集成 Hystrix。我正在使用 @HystrixCommand 来包装使用假客户端进行的服务到服务调用。

@HystrixCommand(fallbackMethod = "updateThingFallback")
def updateRemoteThing(thingResourceClient: ThingResourceClient, thing: Thing) {
thingResourceClient.updateThing(thing) // Call using feign client
}

这个假装客户端使用 spring 安全上下文将安全 header 添加到它发出的请求中。

我遇到的问题是,当执行 HystrixCommand 时,它是在与 Hystrix 线程池不同的线程中运行的,当我的代码尝试访问 spring 安全上下文时,它在新线程上不可用。

我正在像这样访问 spring 安全上下文:

SecurityContextHolder.getContext().getAuthentication();

我的问题是,spring 是否提供了一种将 spring 安全上下文(和应用程序上下文)传递给运行 Hystrix 命令的 Hystrix 线程的方法?

最佳答案

Spring Cloud Netflix 1.2.0您可以使用配置参数启用与 Hystrix 的安全上下文共享:

hystrix.shareSecurityContext: true

关于java - Spring 是否使 SecurityContext 可用于执行 Hystrix 命令的线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29566777/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com