Séb*_*mer 7 spring-boot spring-cloud netflix-feign
我想转发带有假装客户端RequestInterceptor的请求标头,但是,在内RequestInterceptor.apply, RequestContextHolder.getRequestAttributes()是null,所以SecurityContextHolder.getContext().getAuthentication()(我最终也可以得到我的标头的值).
这曾经在升级到Spring-Cloud Brixton之前工作,其中hystrix命令现在必须可以在单独的线程中运行,因为更改为以下参数可以解决问题:
hystrix.command.default.execution.isolation.strategy: SEMAPHORE
Run Code Online (Sandbox Code Playgroud)
现在,如果没有必要,我不太热衷于更改这种默认值,是否有另一种推荐的转发标头现在的方式?
谢谢
对于 Spring Boot 2+ / Spring Cloud Finchley +,如果您只需要安全上下文,您可以设置以下属性:
hystrix.shareSecurityContext=true
Run Code Online (Sandbox Code Playgroud)
并且请求拦截器应该可以工作。
对于其他用例或更早版本(非常感谢Spring Cloud Sleuth的启发):
您所要做的“所有”就是实现一个HystrixConcurrencyStrategy在每次线程更改时传递信息的方法。在 Sleuth 中做一些非常相似的事情的类在这里。
对于我的具体情况,我会:
Callable在wrapCallable用,例如,CallableWithAuthentication创建时,将保持当前的认证类CallableWithAuthentication call方法将首先恢复之前保存的Authentication,然后调用原始动作,然后清理当前的Authentication,等等。一旦HystrixConcurrencyStrategy启动,您的请求拦截器将再次工作,即使使用线程隔离。
注意检查项目的其余部分,还有很多其他有趣的检测工具(例如 RxJava)。
| 归档时间: |
|
| 查看次数: |
4857 次 |
| 最近记录: |