开发者社区> 问答> 正文

java得到客户端IP问题

目前我得到客户端的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;  
    }

展开
收起
长安归故里. 2020-01-31 14:16:06 2692 0
1 条回答
写回答
取消 提交回答
  • 还可能设置X-Real-IP http://relistan.com/http-header-hell-starring-x-real-ip-and-x-forwarded-for/

    2020-01-31 14:16:16
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载