class="java" name="code">//各种奇葩问题如下,考虑IPv4与IPv6 public static String getIpAddr(HttpServletRequest request) throws UnknownHostException { 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(); } if(ip != null && ip.equalsIgnoreCase("0:0:0:0:0:0:0:1")) { ip =InetAddress.getLocalHost().getHostAddress().toString(); } if (ip == null || ip.length() == 0 || "X-Real-IP".equalsIgnoreCase(ip)) { ip = request.getHeader("X-Real-IP"); } return ip; }
?