When I turn Circuit Breaker on I'm getting authentication problems from my services, because authorization token is not present in headers of request.
I found in the web that Circuit Breaker runs on secondary thread, that doesn't has spring security context.
For hystrix, the solution I found is to use shareSecurityContext=true config option, and implement a request interceptor that get the token and set for request.
But, for resilience4j, I not found a solution.
Thanks for help.
Resilience4j circuit breaker itself does not spawn a new thread. But when used with Bulkhead, then code runs within a new thread.
i am assuming you are using Bulkhead with you CB. in that case you can use ContextPropogator
For e.g.
public class SpringSecurityContextPropagator implements ContextPropagator<SecurityContext> {
@Override
public Supplier<Optional<SecurityContext>> retrieve() {
return () -> Optional.of(SecurityContextHolder.getContext());
}
@Override
public Consumer<Optional<SecurityContext>> copy() {
return (context) -> context.ifPresent(SecurityContextHolder::setContext);
}
@Override
public Consumer<Optional<SecurityContext>> clear() {
return (context) -> SecurityContextHolder.clearContext();
}
}
then configure this in bulkhead config
resilience4j.thread-pool-bulkhead:
configs:
default:
maxThreadPoolSize: 10
coreThreadPoolSize: 10
queueCapacity: 15
contextPropagators:
- com.cobalt.cdservice.resilience4j.SpringSecurityContextPropagator