Feign/Eureka client not passing X-Forwarded-For header
Asked Answered
B

1

7

I use Spring boot + Eureka + Feign client to forward requests from one discovered server to another and it works fine.

In my server I need to get the IP of the original user.

I can't seem to find how to configure Feign client to automatically edit the 'X-Forwarded-For' header so I could be able to extract the original user's IP address.

When I use getRemoteAddr() I get the proxy IP address (As expected). When Trying to extract the request.getHeader("X-Forwarded-For") I always get null.

Where should I add / configure this feature?

Bucharest answered 8/2, 2016 at 15:51 Comment(0)
M
0

You need to add your own interceptor which adds this header to requests.

There is a good example in Feign docs (but probably it wasn't there at the time of asking this question):

static class ForwardedForInterceptor implements RequestInterceptor {
  @Override public void apply(RequestTemplate template) {
    template.header("X-Forwarded-For", "origin.host.com");
  }
}

public class Example {
  public static void main(String[] args) {
    Bank bank = Feign.builder()
                 .decoder(accountDecoder)
                 .requestInterceptor(new ForwardedForInterceptor())
                 .target(Bank.class, "https://api.examplebank.com");
  }
}
Markitamarkka answered 24/2, 2020 at 22:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.