在 parallelStream 中获取安全上下文时为空主体 [英] Null principal when get security context in parallelStream
问题描述
当我尝试从并行流中的安全上下文获取主体时,它总是在不在主线程中时返回 null.
When I try to obtain the principal from the security context in a parallel stream, it always returns null when isn't in the main thread.
以下代码在用户通过身份验证时失败:
The following piece of code fails when the user is authenticated:
listOfSomething.parallelStream()
.foreach(el -> {
if (SecurityContextHolder.getContext().getAuthentication().getPrincipal() == null){
throw new RuntimeException();
}});
文档说:
定义与相关联的最低安全信息的接口当前执行线程.
Interface defining the minimum security information associated with the current thread of execution.
但是,有什么办法吗?它在主线程中启动并使用 ForkJoinPool
But, is there any way to do it? It starts in main thread and use ForkJoinPool
谢谢!
推荐答案
看来您需要使用不同的 SecurityContextHolder
策略.见 https://docs.spring.io/spring-security/site/docs/5.0.0.RELEASE/reference/htmlsingle/#securitycontextholder-securitycontext-and-authentication-objects 了解更多关于如何改变它
It seems that you need to use a different SecurityContextHolder
strategy. See https://docs.spring.io/spring-security/site/docs/5.0.0.RELEASE/reference/htmlsingle/#securitycontextholder-securitycontext-and-authentication-objects for more details on how to change it
这篇关于在 parallelStream 中获取安全上下文时为空主体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!