获取访问用户的客户端IP(适用于公网与局域网)

 /**
     * 获取访问用户的客户端IP(适用于公网与局域网).
     */
    private 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();
        }

   // 多个路由时,取第一个非unknown的ip  
    final String[] arr = ip.split(",");  
    for (final String str : arr) {  
        if (!"unknown".equalsIgnoreCase(str)) {  
            ip= str;  
            break;  
        }  
    }  
        return ip;
    }

 

posted @ 2020-12-01 10:48  掀起你的头盖骨  阅读(370)  评论(0编辑  收藏  举报