6

I have a spring boot app and want to create a Feign client which has a statically defined header value (for auth, but not basic auth). I found the @Headers annotation but it doesn't seem to work in the realm of Spring Boot. My suspicion is this has something to do with it using the SpringMvcContract.

Here's the code I want to work:

@FeignClient(name = "foo", url = "http://localhost:4444/feign")
@Headers({"myHeader:value"})
public interface LocalhostClient {

But it does not add the headers.

I made a clean spring boot app with my attempts and posted to github here: github example

The only way I was able to make it work was to define the RequestInterceptor as a global bean, but I don't want to do that because it would impact other clients.

1
  • I ran into the same issue. I found a way to do it through the application.yaml file.
    – AElMehdi
    Aug 3, 2022 at 18:27

4 Answers 4

13

You can also achieve this by adding header to individual methods as follows:

@RequestMapping(method = RequestMethod.GET, path = "/resource", headers = {"myHeader=value"})

Using @Headers with dynamic values in Feign client + Spring Cloud (Brixton RC2) discusses a solution for dynamic values using @RequestHeader.

1
  • This is what works with FeignClient apart from the interceptor changes.
    – Imran AK
    Feb 20 at 11:32
11

You can set a specific configuration class on your feign interface and define a RequestInterceptor bean in there. For example:

@FeignClient(name = "foo", url = "http://localhost:4444/feign", 
configuration = FeignConfiguration.class)
public interface LocalhostClient {
}

@Configuration
public class FeignConfiguration {

  @Bean
  public RequestInterceptor requestTokenBearerInterceptor() {
    return new RequestInterceptor() {
      @Override
      public void apply(RequestTemplate requestTemplate) {
        // Do what you want to do
      }
    };
  }
}
1
  • 1
    Thanks! My only concern with the @Configuration approach is that Feign requires that annotation, and you have to be careful to keep it out of the component scan. This seems dangerous to me. If this class were to get added to the component scan somehow, suddenly we'd be sending sensitive information to ALL of our feign endpoints. +1 though because this technically answers my specific question even though I'm going to use Ali's suggestion
    – Stimp
    May 22, 2018 at 15:33
1

You could specify that through the application.yml file:

feign:
  client:
    config:
      default:
        defaultRequestHeaders:
          Authorization:
            - Basic 3ncond2dS3cr2t
          otherHeader:
            - value 

Note that using the default configuration will affect all your Feign Clients. To tune this per client, you can do the following:

feign:
  client:
    config:
      default:
        defaultRequestHeaders:
          # request headers to all feign clients
      my-client-name:
        defaultRequestHeaders:
          Authorization:
            - Basic 4ncond2dS3cr2t
          API_KEY:
            - apiKey
2
  • You can actually tune this per client by its name.
    – Incepter
    May 22 at 10:34
  • @incepter Yeap definitely!
    – AElMehdi
    May 22 at 15:24
0

Try this

@Component
public class AuthFeignInterceptor implements RequestInterceptor {

    @Override
    public void apply(RequestTemplate template) {
        final RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if (requestAttributes != null) {
            final HttpServletRequest httpServletRequest = ((ServletRequestAttributes) requestAttributes).getRequest();
            template.header("Header_name","Value");
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.