9

I would like to forward a request header with a feign client RequestInterceptor but, within RequestInterceptor.apply, RequestContextHolder.getRequestAttributes() is null, so is SecurityContextHolder.getContext().getAuthentication() (where I could also eventually get the value of my header).

This used to work before upgrading to Spring-Cloud Brixton, where the hystrix commands must now probably be run in a separate thread, because changing to the following parameter solves the problem :

hystrix.command.default.execution.isolation.strategy: SEMAPHORE

Now, I'm not too keen on changing this kind of default values if not necessary, is there another, recommended, way of forwarding headers now ?

Thanks

4

2 回答 2

7

For Spring Boot 2+ / Spring Cloud Finchley +, if you just need the security context you can set the following property :

hystrix.shareSecurityContext=true

And the request interceptor should work.


For other uses cases or earlier versions (many thanks to Spring Cloud Sleuth for the inspiration) :

"All" you have to do is implement an HystrixConcurrencyStrategy that passes the information along each time there is a thread change. The class that does something very similar in Sleuth is here.

For my specific case I would :

  1. Wrap the Callable in wrapCallable with, for example, an CallableWithAuthentication class that would hold the current authentication when constructed
  2. The CallableWithAuthentication call method would first restore the previously saved Authentication, then call the original action, then clean up the current Authentication, et voilà.

Once your HystrixConcurrencyStrategy is up your request interceptor will work again, even with Thread isolation.

Note check the rest of the project there are lots of other interesting instrumenting stuff (for RxJava for example).

于 2016-06-30T14:31:34.300 回答
0

Just to elaborate on @sebastian 's answer

public class RequestContextHystrixConcurrencyStrategy extends HystrixConcurrencyStrategy {
@Override
public <T> Callable<T> wrapCallable(Callable<T> callable) {
    return new RequestAttributeAwareCallable<>(callable, RequestContextHolder.getRequestAttributes());
}

static class RequestAttributeAwareCallable<T> implements Callable<T> {

    private final Callable<T> delegate;
    private final RequestAttributes requestAttributes;

    public RequestAttributeAwareCallable(Callable<T> callable, RequestAttributes requestAttributes) {
        this.delegate = callable;
        this.requestAttributes = requestAttributes;
    }

    @Override
    public T call() throws Exception {
        try {
            RequestContextHolder.setRequestAttributes(requestAttributes);
            return delegate.call();
        } finally {
            RequestContextHolder.resetRequestAttributes();
        }
    }
}

}

then somewhere in configuration

@PostConstruct
public void init() {
    HystrixPlugins.getInstance().registerConcurrencyStrategy(new RequestContextHystrixConcurrencyStrategy());
}

Refer following post

于 2021-05-03T09:38:47.817 回答