我正在尝试配置反向代理服务器以调用第三方,我们的访问仅限于特殊IP,但是当我调用代理服务器时,服务器会将客户端IP传递给第三方服务器。
我试图通过Spring应用程序获取Ip地址,但我发现Spring getRemoteAddr
使用的不是X-Forwarded-For
。
我正在寻找一种在HttpServletRequest.getRemoteAddr()
Nginx配置:
server {
listen 80;
server_name _;
location / {
real_ip_header X-Forwarded-For;
proxy_set_header X-Forwarded-For '192.168.1.1';
proxy_set_header X-Real-IP '192.168.1.1';
proxy_pass http://192.168.30.31:8080;
}
}
Spring Controller:
@RestController
public class TestController {
@RequestMapping("/")
public String getIp(HttpServletRequest request) {
return "getRemoteAddr: " + request.getRemoteAddr() +
"\ngetRemoteHost: " + request.getRemoteHost() +
"\nX-Forwarded-For: " + request.getHeader("X-Forwarded-For");
}
}
结果
getRemoteAddr: 192.168.99.219 //expected 192.168.1.1
getRemoteHost: 192.168.99.219
X-Forwarded-For: 192.168.1.1