获取访问用户的客户端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;
    }
原文地址:https://www.cnblogs.com/yangxiaoli/p/14066783.html