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

守給你的承諾、 提交于 2020-02-26 10:17:25

问题


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:


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");
  }
}


来源:https://stackoverflow.com/questions/35273706/feign-eureka-client-not-passing-x-forwarded-for-header

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!