这个问题已经在这里有一个答案:
- 我如何在servlet的客户端的远程地址? 10个回答
我正在开发使用JSP,Servlet的 (容器:Glassfish的 )Web应用程序中,我需要得到客户的IP Address
。
我收到客户端的IP地址,因为我想给访问某些网页(例如客户维护表单)只能在电脑withing办公室,我想限制访问这些页面之外的办公室。
以下是我到目前为止的代码:
WAY1
String ipAddress = request.getRemoteAddr();
System.out.println("IP Address: "+ipAddress);
way2
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
每次当我重新启动我的电脑上(Shutdown->启动或重新启动)。
我越来越IP6
,如:
fe80:0:0:0:20ca:1776:f5ff:ff15%13
让我知道什么是错的代码?
正如@马丁和这个答案解释,它是复杂的。 有获取客户端的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();
}
}
我用下面的静态辅助方法来获取客户端的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;
}
我不这样,你可以试试
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;
}
作为basZero提到,X - 转发,对于应检查逗号。 (请看: http://en.wikipedia.org/wiki/X-Forwarded-For )。 该字段的一般格式为:X - 转发,对于:clientIP,PROXY1,Proxy2将...等等。 因此,我们将看到这样的事情:X-转发-FOR:129.77.168.62,129.77.63.62。
我相信这是更多的与您的网络是如何配置的。 Servlet是简单地给你它是找到的地址。
我可以建议两种解决方法。 首先尝试使用IPv4。 看到这个SO回答
此外,尝试使用request.getRemoteHost()方法来获取计算机的名称。 当然名字都是独立的任何IP它们映射到的。
我还是觉得你应该跟你的基础设施球员讨论这个问题。