• Java获取客户端真实IP地址


    Java代码

    import javax.servlet.http.HttpServletRequest;  
      
    /** 
     * 获取对象的IP地址等信息 */  
    public class IPUtil {  
      
        /** 
         * 获取用户真实IP地址,不使用request.getRemoteAddr();的原因是有可能用户使用了代理软件方式避免真实IP地址, 
         * 可是,如果通过了多级反向代理的话,X-Forwarded-For的值并不止一个,而是一串IP值,究竟哪个才是真正的用户端的真实IP呢? 
         * 答案是取X-Forwarded-For中第一个非unknown的有效IP字符串。 
         * 如:X-Forwarded-For:192.168.1.110, 192.168.1.120, 192.168.1.130, 
         * 192.168.1.100 
         * 用户真实IP为: 192.168.1.110 
         * @param request 
         * @return 
         */  
        public static String getIpAddress(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.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();  
            }  
            return ip;  
        }  
          
    }

    参考:

    https://www.cnblogs.com/chenglc/p/6856734.html

  • 相关阅读:
    react-native 调用原生方法
    react-native 生命周期
    查看ubuntu系统信息
    Python之DataFrame将列作为索引
    Python之读取文本文件
    Python 之 直接赋值、Deepcopy、Copy区别
    Python之time与datetime模块
    Python之连接MySQL数据库,执行建表语句
    Python之读取csv文件
    MySQL之count() 函数
  • 原文地址:https://www.cnblogs.com/aeolian/p/12059818.html
Copyright © 2020-2023  润新知