Java – does spring provide securitycontext for threads executing hystrix commands
I'm running a spring boot application and just started integrating hystrix from spring cloud Netflix I'm using @ hystrixcommand to encapsulate Service - to - service calls that use fake clients
@HystrixCommand(fallbackMethod = "updateThingFallback") def updateRemoteThing(thingResourceClient: ThingResourceClient,thing: Thing) { thingResourceClient.updateThing(thing) // Call using feign client }
This client uses the spring security context to add security headers to its requests
The problem I encountered was that when executing the hystrix command, it ran in a separate thread in the hystrix thread pool, and when my code tried to access the spring security context, it was not available on the new thread
I am accessing the spring security context as follows:
SecurityContextHolder.getContext().getAuthentication();
My question is, does spring provide a way to pass the spring security context (and application context) to the hystrix thread running the hystrix command?
Solution
You should be able to get ApplicationContext in your bean through the normal method I can see two ways to pass authentication objects: 1) as a parameter to the method, or 2) using semaphore isolation instead of a separate thread to run hystrix
@HystrixCommand(fallbackMethod = "updateThingFallback",commandProperties = { @HystrixProperty(name = "execution.isolation.strategy",value = "SEMAPHORE") })