我正在尝试public
通过 servlet 获取客户端 IP 地址,如下所示:
String ip = request.getHeader("X-Forwarded-For");
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
System.out.println("1. ip: "+ip);
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
System.out.println("2. ip: "+ip);
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_CLIENT_IP");
}
System.out.println("3. ip: "+ip);
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
}
System.out.println("4. ip: "+ip);
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
System.out.println("4. ip: "+ip);
但这只能返回 localhost IP,如下面的输出所示:
1. ip: null
2. ip: null
3. ip: null
4. ip: null
4. ip: 127.0.0.1
有什么帮助吗?