Java 获取请求ip的正确方法是什么

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18350318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:47:24  来源:igfitidea点击:

what is the right way to get request's ip

javaip

提问by sprite

I find some different ways to get ip in servlet. but i don't know which one is right and why.

我找到了一些不同的方法来获取 servlet 中的 ip。但我不知道哪一个是正确的,为什么。

1:

1:

request.getHeader( "X-Real-IP" )

2:

2:

  String ip = request.getHeader("X-Forwarded-For"); 
  if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
        ip = request.getHeader("Proxy-Client-IP");  
    }  
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
        ip = request.getHeader("WL-Proxy-Client-IP");  
    }  
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
        ip = request.getHeader("HTTP_CLIENT_IP");  
    }  
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
        ip = request.getHeader("HTTP_X_FORWARDED_FOR");  
    }  
    if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
        ip = request.getRemoteAddr();  
    } `

3:

3:

 String ip=request.getHeader("x-forwarded-for");
                if(ip==null){
                    ip=request.getRemoteAddr();
                }
                String ips[]=ip.split(",");`
                                ip=ips[0];

采纳答案by Stephen C

The answer is complicated.

答案很复杂。

  • If your servlet is running on a webserver that is behind a reverse proxy or load balancer, then that web proxy can be configured to inject a request header that gives the IP address that the request came from. Different reverse proxies will inject different headers. Consult the documentation for your (front-end) server.

  • If your client uses a (forward) proxy, then it mightinsert headers to say what the client IP address is ... or it might not. And the IP address it insert mightbe incorrect.

  • The value you get by calling request.getRemoteAddr()is going to be the IP address of the immediateupstream source of the request.

  • 如果您的 servlet 运行在位于反向代理或负载均衡器后面的网络服务器上,那么该网络代理可以配置为注入一个请求标头,该标头提供请求来自的 IP 地址。不同的反向代理将注入不同的标头。请查阅您的(前端)服务器的文档。

  • 如果您的客户端使用(转发)代理,则它可能会插入标头以说明客户端 IP 地址是什么……或者可能不会。它插入的 IP 地址可能不正确。

  • 您通过调用获得的值将request.getRemoteAddr()是请求的直接上游源的 IP 地址。

None of the headers that you listed is standard, but "x-forwarded-for" is reputed to be a defacto standard; i.e. it is the one that is most likely to be inserted by a proxy, etc ... if anything is injected.

您列出的标题都不是标准的,但“x-forwarded-for”被认为是事实上的标准;即它是最有可能被代理插入的那个,等等......如果注入了任何东西。

Finally, even if you did get an IP address, it wouldn't necessarily help you. For instance, if the client sits on a private network and connects to the internet via a NAT gateway, then the IP address in HTTP request will be an address of the NAT server ... not the actual client IP.

最后,即使您确实获得了 IP 地址,也不一定对您有帮助。例如,如果客户端位于私有网络上并通过 NAT 网关连接到 Internet,那么 HTTP 请求中的 IP 地址将是 NAT 服务器的地址……而不是实际的客户端 IP。



So what does this all mean? Well basically, it means that in generalyou cannot reliablyfind out the IP address of the system that the request originated from.

那么,这意味着什么?基本上,这意味着通常您无法可靠地找出请求源自的系统的 IP 地址。

回答by Burak Keceli

This seems the most we can do to get the original IP address of the client

这似乎是我们获取客户端原始 IP 地址所能做的最多的事情

String ipAddress = request.getHeader("X-FORWARDED-FOR");  
if (ipAddress == null) {  
   ipAddress = request.getRemoteAddr();  
}