I am using graphql-java, and it executes the different nested resolvers in separate threads.
I have a custom filter on spring security that parses the Bearer token from the Authorization Header and adds that to the current spring security context.
I was temporarily able to solve the issue by listening to the ContextRefreshedEvent and forcing the strategy to Global, as you can see here:
@EventListener
fun setupSecurityContext(event: ContextRefreshedEvent) {
SecurityContextHolder.setStrategyName(SecurityContextHolder.MODE_GLOBAL)
}
I am not happy with this solution because I have to access the tokens on the resolvers with a global static call when I would like to receive it passed as a parameter on the resolver.
SecurityContextHolder.getContext().authentication.principal
I believe this is not a good practice. I guess the data from one request can be exposed to another user by a different thread.