当我尝试从并行流中的安全上下文获取主体时,如果主体不在主线程中,它总是返回 null。
当用户通过身份验证时,以下代码段失败:
listOfSomething.parallelStream()
.foreach(el -> {
if (SecurityContextHolder.getContext().getAuthentication().getPrincipal() == null){
throw new RuntimeException();
}});
Run Code Online (Sandbox Code Playgroud)
文档说:
定义与当前执行线程关联的最小安全信息的接口。
但是,有什么办法可以做到吗?它在主线程中启动并使用 ForkJoinPool
谢谢你!