问题描述
目前我得到客户端的IP方法是这么定义的,但100个IP记录中,还有那么几个IP获取不了。想了半天没找到具体原因,各大帮忙想想?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();}return ip;}
解决方案
还可能设置X-Real-IPhttp://relistan.com/http-header-hell-starring-x-real-ip-and-x-forwarded-for/
解决方案二:
已经很全面了,表示关注