Java 获取客户端的IP地址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16558869/
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-16 07:24:55  来源:igfitidea点击:

Getting IP address of client

javaweb-applicationsservletsip

提问by Bhushan

I am developing a web application using JSP, Servlets(Container: Glassfish) in which I need to get clients IP Address.

我正在使用JSPServlets(容器:Glassfish)开发一个 web 应用程序,我需要在其中获取客户端IP Address

I am getting the clients IP address, because I want to give access to some pages (like Customer maintenance forms) only on computers withing the office, I want to restrict access to those pages outside office.

我正在获取客户的 IP 地址,因为我只想在办公室内的计算机上访问某些页面(如客户维护表格),我想限制对办公室外这些页面的访问。

Following is my code so far:

以下是我到目前为止的代码:

way1

方式1

String ipAddress =  request.getRemoteAddr();
System.out.println("IP Address: "+ipAddress);

way2

方式2

String ipAddress=null;
String getWay = request.getHeader("VIA");   // Gateway
ipAddress = request.getHeader("X-FORWARDED-FOR");   // proxy
if(ipAddress==null)
{
    ipAddress = request.getRemoteAddr();
}
System.out.println("IP Address: "+ipAddress);

Above code gives me different IP Addresseach time when I restart my computer (Shutdown->Start or Restart).

different IP Address每次我重新启动计算机(关机-> 启动或重新启动)时,上面的代码都会给我。

I am getting IP6like:

我越来越IP6像:

fe80:0:0:0:20ca:1776:f5ff:ff15%13

Let me know what is wrong with this code?

让我知道这段代码有什么问题?

回答by coffee

I do like this,you can have a try

我喜欢这个,你可以试试

public String getIpAddr(HttpServletRequest request) {      
   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.getRemoteAddr();      
   }      
   return ip;      
}   

回答by Raza

I believe it is more to do with how your network is configured. Servlet is simply giving you the address it is finding.

我相信这更多地与您的网络配置方式有关。Servlet 只是为您提供它正在查找的地址。

I can suggest two workarounds. First try using IPV4. See this SO Answer

我可以建议两种解决方法。首先尝试使用 IPV4。看到这个SO答案

Also, try using the request.getRemoteHost() method to get the names of the machines. Surely the names are independent of whatever IP they are mapped to.

另外,尝试使用 request.getRemoteHost() 方法来获取机器的名称。当然,名称独立于它们映射到的任何 IP。

I still think you should discuss this with your infrastructure guys.

我仍然认为你应该与你的基础设施人员讨论这个问题。

回答by basZero

I use the following static helper method to retrieve the IP of a client:

我使用以下静态辅助方法来检索客户端的 IP:

public static String getClientIpAddr(HttpServletRequest request) {  
    String ip = request.getHeader("X-Forwarded-For");  
    if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) {  
        ip = request.getHeader("Proxy-Client-IP");  
    }  
    if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) {  
        ip = request.getHeader("WL-Proxy-Client-IP");  
    }  
    if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) {  
        ip = request.getHeader("HTTP_X_FORWARDED_FOR");  
    }  
    if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) {  
        ip = request.getHeader("HTTP_X_FORWARDED");  
    }  
    if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) {  
        ip = request.getHeader("HTTP_X_CLUSTER_CLIENT_IP");  
    }  
    if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) {  
        ip = request.getHeader("HTTP_CLIENT_IP");  
    }  
    if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) {  
        ip = request.getHeader("HTTP_FORWARDED_FOR");  
    }  
    if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) {  
        ip = request.getHeader("HTTP_FORWARDED");  
    }  
    if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) {  
        ip = request.getHeader("HTTP_VIA");  
    }  
    if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) {  
        ip = request.getHeader("REMOTE_ADDR");  
    }  
    if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase("unknown")) {  
        ip = request.getRemoteAddr();  
    }  
    return ip;  
}

回答by Xavier Delamotte

As @martin and this answerexplained, it is complicated. There is no bullet-proof way of getting the client's ip address.

正如@martin 和这个答案所解释的那样,它很复杂。没有获得客户端 IP 地址的万无一失的方法。

The best that you can do is to try to parse "X-Forwarded-For"and rely on request.getRemoteAddr();

你能做的最好的事情就是尝试解析"X-Forwarded-For"和依赖request.getRemoteAddr();

public static String getClientIpAddress(HttpServletRequest request) {
    String xForwardedForHeader = request.getHeader("X-Forwarded-For");
    if (xForwardedForHeader == null) {
        return request.getRemoteAddr();
    } else {
        // As of https://en.wikipedia.org/wiki/X-Forwarded-For
        // The general format of the field is: X-Forwarded-For: client, proxy1, proxy2 ...
        // we only want the client
        return new StringTokenizer(xForwardedForHeader, ",").nextToken().trim();
    }
}

回答by mkilic

As basZero mentioned, X-Forwarded-For should be checked for comma. (Look at : http://en.wikipedia.org/wiki/X-Forwarded-For). The general format of the field is: X-Forwarded-For: clientIP, proxy1, proxy2... and so on. So we will be seeing something like this : X-FORWARDED-FOR: 129.77.168.62, 129.77.63.62.

正如 basZero 提到的,X-Forwarded-For 应该检查逗号。(查看:http: //en.wikipedia.org/wiki/X-Forwarded-For)。该字段的一般格式为:X-Forwarded-For:clientIP、proxy1、proxy2...等。所以我们会看到这样的东西:X-FORWARDED-FOR: 129.77.168.62, 129.77.63.62。