2016-12-30 63 views
1

传播Spring安全上下文我想通过Spring集成异步消息流传播spring安全上下文,但发现即使我添加了SecurityContextPropagationChannelInterceptor安全上下文总是以null结尾我的消息处理器。如何在Spring集成异步消息传递网关

@Bean 
@GlobalChannelInterceptor(patterns = {"*"}) 
public ChannelInterceptor securityContextPropagationInterceptor() 
{ 
    return new SecurityContextPropagationChannelInterceptor(); 
} 

我开始从通过拨打电话到我的网关接口有一个人口稠密的安全上下文服务我的流程:

@MessagingGateway 
public interface AssignmentsService 
{ 
    @Gateway(requestChannel = "applyAssignmentsFlow.input") 
    ListenableFuture<AssignmentResult> applyAssignments(AssignmentRequest assignmentRequest); 
} 

关于进一步调试我发现,GatewayProxyFactoryBean创建一个新线程时启动我的流程,但不传播安全上下文。

我已经搜索,但一直无法找到如何配置此传播安全上下文。

回答

2

这是非常有趣的任务。确实 :) !

但无论如何,你可以做这样的:

@Bean 
public AsyncTaskExecutor securityContextExecutor() { 
    return new DelegatingSecurityContextAsyncTaskExecutor(new SimpleAsyncTaskExecutor()); 
} 

... 

@MessagingGateway(asyncExecutor = "securityContextExecutor") 
public interface AssignmentsService 

这里的主要技巧是使用Spring Security和它的并发utils,我们应该用TaskExecutor包装拿起当前SecurityContext,并传播到新产生线程。

虽然没有什么关于Spring Integration的 - 只是使用Security的正确方法。

很快就会在参考手册中添加这样的技巧。

关于此事的拉求:https://github.com/spring-projects/spring-integration/pull/2015

+0

嗨!这几乎工作,除了我发现我不得不继承'DelegatingSecurityContextAsyncTaskExecutor',因为它没有实现'AsyncListenableTaskExecutor'接口。我应该向Spring Security团队提出一个更改请求,将其更改为实现'AsyncListenableTaskExecutor'? –

+0

是的,请:https://github.com/spring-projects/spring-security/issues。 –

相关问题