title: Forward request headers to multiple service calls in Spring + Netflix + Feign
body: <p>I have a bunch of intermediate and core services within my application. All services are Spring Boot and using Netflix Library. When a user requests information, the request will/might pass other services in the chain eg:</p>
  
  <pre><code>Client &lt;-&gt; Zuul &lt;-&gt; Service B &lt;-&gt; Service A
  </code></pre>
  
  <p>I have configured all services (A and B) to be ResourceServer so that every access needs to be authenticated. When requesting an access token (From a Spring Security Server) and use it to request information directly from Service A, everything works fine. When I use the same token to access information from Service B (which needs Service A down the line) I get an "HTTP 401: Full authentification is required" error. Service B uses a FeignClient to call Service A.</p>
  
  <p>After some debugging, I found out, that the Authorization-Header is not passed from Service B to Service A. Service B checks the token itself correctly, grants access to the method and tries to perform the request of Service A.</p>
  
  <p>I tried a RequestInterceptor but without any success (Error "Scope 'request' is not active for the current thread")</p>
  
  <pre><code>@Component
  public class OAuth2FeignRequestInterceptor implements RequestInterceptor {
      private static final String AUTHORIZATION_HEADER = "Authorization";
      private static final String BEARER_TOKEN_TYPE = "Bearer";
      private final OAuth2ClientContext oauth2ClientContext;
  
      public OAuth2FeignRequestInterceptor(OAuth2ClientContext oauth2ClientContext) {
          Assert.notNull(oauth2ClientContext, "Context can not be null");
          this.oauth2ClientContext = oauth2ClientContext;
      }
  
      @Override
      public void apply(RequestTemplate template) {
          if (template.headers().containsKey(AUTHORIZATION_HEADER)) {
              ...
          } else if (oauth2ClientContext.getAccessTokenRequest().getExistingToken() == null) {
              ...
          } else {
              template.header(AUTHORIZATION_HEADER, String.format("%s %s", BEARER_TOKEN_TYPE,
                      oauth2ClientContext.getAccessTokenRequest().getExistingToken().toString()));
          }
      }
  }
  </code></pre>
  
  <p>This is an example proxy function that uses the FeignClient:</p>
  
  <pre><code>@Autowired
  private CategoryClient cat;
  
  
  @HystrixCommand(fallbackMethod = "getAllFallback", commandProperties = {@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "2") })
  @GetMapping("/category")
  public ResponseEntity&lt;List&lt;Category&gt;&gt; getAll() {
      try {
          ResponseEntity&lt;List&lt;Category&gt;&gt; categories = this.cat.getAll();
          ...
          return categories;
      } catch(Exception e) {
          ...
      }
  }
  </code></pre>
  
  <p>Is there any working solution to pass the Authorization-Header from the proxy function to the FeignClient so that Service A will receive the header and can do its own auth check with it?</p>