获取登陆ip

public static String getClientAddress(HttpServletRequest request) {
    if (request == null) {
        return null;
    }
    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();
    }
    if (("0:0:0:0:0:0:0:1").equals(ip)) {
        ip = "127.0.0.1";
    }
    if (ip != null && ip.contains(",")) {
        ip = ip.split(",")[0];
    }
    if (!isIpAddress(ip)) {
        return "";
    }
    return ip;
}

你可能感兴趣的:(获取登陆ip)