Feign/Eureka client not passing X-Forwarded-For he

2020-06-21 02:45发布

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?

1条回答
\"骚年 ilove
2楼-- · 2020-06-21 03:28

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");
  }
}
查看更多
登录 后发表回答