获取访问者真实IP:HttpServletRequestServerHttpRequest

Posted 蜡笔小心_

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了获取访问者真实IP:HttpServletRequestServerHttpRequest相关的知识,希望对你有一定的参考价值。

1、HttpServletRequest

public static String getIpAddr(HttpServletRequest request)
        String ip = null;

        // X-Forwarded-For:Squid 服务代理
        String ipAddresses = request.getHeader("X-Forwarded-For");
        if (ipAddresses == null || ipAddresses.length() == 0 || "unknown".equalsIgnoreCase(ipAddresses))
        
            // Proxy-Client-IP:apache 服务代理
            ipAddresses = request.getHeader("Proxy-Client-IP");
        
        if (ipAddresses == null || ipAddresses.length() == 0 || "unknown".equalsIgnoreCase(ipAddresses))
        
            // WL-Proxy-Client-IP:weblogic 服务代理
            ipAddresses = request.getHeader("WL-Proxy-Client-IP");
        
        if (ipAddresses == null || ipAddresses.length() == 0 || "unknown".equalsIgnoreCase(ipAddresses))
        
            // HTTP_CLIENT_IP:有些代理服务器
            ipAddresses = request.getHeader("HTTP_CLIENT_IP");
        
        if (ipAddresses == null || ipAddresses.length() == 0 || "unknown".equalsIgnoreCase(ipAddresses))
        
            // X-Real-IP:nginx服务代理
            ipAddresses = request.getHeader("X-Real-IP");
        

        // 有些网络通过多层代理,那么获取到的ip就会有多个,一般都是通过逗号(,)分割开来,并且第一个ip为客户端的真实IP
        if (ipAddresses != null && ipAddresses.length() != 0)
        
            ip = ipAddresses.split(",")[0];
        

        // 还是不能获取到,最后再通过request.getRemoteAddr();获取
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ipAddresses))
        
            ip = request.getRemoteAddr();
        
        return ip.equals("0:0:0:0:0:0:0:1") ? "127.0.0.1" : ip;
    

二、ServerHttpRequest

public static String getIpAddress(ServerHttpRequest request) 
        HttpHeaders headers = request.getHeaders();
        String ip = headers.getFirst("x-forwarded-for");
        if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) 
            // 多次反向代理后会有多个ip值,第一个ip才是真实ip
            if (ip.indexOf(",") != -1) 
                ip = ip.split(",")[0];
            
        
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) 
            ip = headers.getFirst("Proxy-Client-IP");
        
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) 
            ip = headers.getFirst("WL-Proxy-Client-IP");
        
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) 
            ip = headers.getFirst("HTTP_CLIENT_IP");
        
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) 
            ip = headers.getFirst("HTTP_X_FORWARDED_FOR");
        
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) 
            ip = headers.getFirst("X-Real-IP");
        
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) 
            ip = request.getRemoteAddress().getAddress().getHostAddress();
        
        return ip.equals("0:0:0:0:0:0:0:1") ? "127.0.0.1" : ip;
    

以上是关于获取访问者真实IP:HttpServletRequestServerHttpRequest的主要内容,如果未能解决你的问题,请参考以下文章

HttpServletRequest获取访问者真实IP

PHP 获取访问者的真实IP地址。

获取访问者真实IP:HttpServletRequestServerHttpRequest

获取访问者真实IP:HttpServletRequestServerHttpRequest

如何通过httpclient获取访问域名的真实ip

使用LVS或者阿里云的SLB后如何获取访客真实的IP地址