在parallelStream中获取安全上下文时为空主体[重复]

问题描述 投票:1回答:1

这个问题在这里已有答案:

当我尝试从并行流中的安全上下文中获取主体时,它总是在不在主线程中时返回null。

用户通过身份验证后,以下代码失败:

listOfSomething.parallelStream()
                .foreach(el -> { 
if (SecurityContextHolder.getContext().getAuthentication().getPrincipal() == null){
            throw new RuntimeException();
}});

文件说:

定义与当前执行线程关联的最小安全信息的接口。

但是,有什么办法吗?它从主线程开始并使用ForkJoinPool

谢谢!

java spring spring-boot spring-security stream
1个回答
2
投票

看来你需要使用不同的SecurityContextHolder策略。有关如何更改它的更多详细信息,请参阅https://docs.spring.io/spring-security/site/docs/5.0.0.RELEASE/reference/htmlsingle/#securitycontextholder-securitycontext-and-authentication-objects

© www.soinside.com 2019 - 2024. All rights reserved.