这个问题在这里已有答案:
当我尝试从并行流中的安全上下文中获取主体时,它总是在不在主线程中时返回null。
用户通过身份验证后,以下代码失败:
listOfSomething.parallelStream()
.foreach(el -> {
if (SecurityContextHolder.getContext().getAuthentication().getPrincipal() == null){
throw new RuntimeException();
}});
文件说:
定义与当前执行线程关联的最小安全信息的接口。
但是,有什么办法吗?它从主线程开始并使用ForkJoinPool
谢谢!
看来你需要使用不同的SecurityContextHolder
策略。有关如何更改它的更多详细信息,请参阅https://docs.spring.io/spring-security/site/docs/5.0.0.RELEASE/reference/htmlsingle/#securitycontextholder-securitycontext-and-authentication-objects