我有一个 Spring Boot 应用程序版本 1.3.3.RELEASE,并且我添加了 Spring Cloud Sleuth 来获取那些跟踪好东西。添加 spring-cloud-sleuth 后,我看到我的一些异步 API 正在中断,因为传入的org.springframework.security.concurrent.DelegatingSecurityContextExecutor.
是 null。简短的故事,我使用org.springframework.security.concurrent.DelegatingSecurityContextExecutor
spring security 来传递不同执行者的安全上下文。为此,我只是org.springframework.security.concurrent.DelegatingSecurityContextExecutor
在应用程序启动期间将这些执行程序包装到实例中。我不确定为什么 Spring Sleuth 在这里成为一个问题。我们使用异步处理的方式是,我们创建了一个自定义注释,然后围绕它创建了一个切入点来注入异步行为。
例子:
public class Async implements AsyncConfigurer {
public Executor getCustomAsyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
//all configuration for thread pool
return new DelegatingSecurityContextAsyncTaskExecutor(executor);
}
public Executor getProcessingExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
//all configuration for thread pool
return new DelegatingSecurityContextAsyncTaskExecutor(executor);
}
}
public class MyControllerClass {
@Autowired
private Executor myProcessingExecutor;
@MyCustomAsyncAnnotation
public ResponseObject getResponse() {
//As soon as below code get's executed I get NPE as
//`org.springframework.security.concurrent.DelegatingSecurityContextExecutor.executor` is null. What I'm not sure is why it's null??
// As you see above in my Async class I've registered executor in DelegatingSecurityContextAsyncTaskExecutor, then why it's null here?
//If I remove spring-cloud-sleuth code works fine once again.
CompletableFuture.supplyAsync(() -> ..., myProcessingExecutor);
}
}
class MyCustomAspect {
private Executor myCustomAsyncExecutor;
//create point cut here for those method which have
// @MyCustomAsyncAnnotionation and process it async via above executor.
}
我正在使用 spring boot 1.3.3.RELEASE,请放心,spring-security 4.0.3.RELEASE,spring-cloud-sleuth 1.0.12.RELEASE