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();
}});
The documentation says:
Interface defining the minimum security information associated with the current thread of execution.
But, is there any way to do it? It starts in main thread and use ForkJoinPool
Thank you!
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With