Feign/Eureka客户端未传递X-Forwarded-For标头 [英] Feign/Eureka client not passing X-Forwarded-For header
问题描述
我使用Spring boot + Eureka + Feign客户端将请求从一台发现的服务器转发到另一台,并且工作正常.
I use Spring boot + Eureka + Feign client to forward requests from one discovered server to another and it works fine.
在我的服务器中,我需要获取原始用户的IP.
In my server I need to get the IP of the original user.
我似乎找不到如何配置Feign客户端以自动编辑"X-Forwarded-For"标头,以便能够提取原始用户的IP地址.
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.
当我使用getRemoteAddr()时,我获得了代理IP地址(如预期的那样). 尝试提取request.getHeader("X-Forwarded-For")时,我总是得到null.
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?
推荐答案
您需要添加自己的拦截器,该拦截器会将此标头添加到请求中.
You need to add your own interceptor which adds this header to requests.
Feign文档中的一个很好的例子(但可能在问这个问题时还不存在):
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");
}
}
这篇关于Feign/Eureka客户端未传递X-Forwarded-For标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!