51

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

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

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

方式1

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

方式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);

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

我越来越IP6喜欢:

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

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

4

5 回答 5

77

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

你能做的最好的就是尝试解析"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();
    }
}
于 2014-02-19T15:15:07.753 回答
35

我使用以下静态辅助方法来检索客户端的 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;  
}
于 2014-02-03T14:32:03.867 回答
7

我喜欢这个,你可以试试

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;      
}   
于 2013-05-15T07:31:19.803 回答
3

正如 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。

于 2014-03-18T05:39:42.173 回答
1

我相信这更多地与您的网络配置方式有关。Servlet 只是给你它找到的地址。

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

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

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

于 2013-05-15T14:41:38.610 回答