从反向代理隐藏客户端IP

时间:2020-03-04 09:50:39

标签: spring-boot nginx nginx-reverse-proxy

我正在尝试配置反向代理服务器以调用第三方,我们的访问仅限于特殊IP,但是当我调用代理服务器时,服务器会将客户端IP传递给第三方服务器。

我试图通过Spring应用程序获取Ip地址,但我发现Spring getRemoteAddr使用的不是X-Forwarded-For

我正在寻找一种在HttpServletRequest.getRemoteAddr()

中操纵Ip的方法

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

0 个答案:

没有答案