• 获取客户端ip(包含中间反向代理)


     /**
         * 根据request获取用户的IP地址
         * @param request
         * @return
         */
        public static String getRemoteHost(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();
            }
            return ip.equals("0:0:0:0:0:0:0:1")?"127.0.0.1":ip;
        }


    经过代理以后,由于在客户端和服务之间增加了中间层,因此服务器无法直接拿到客户端的IP,服务器端应用也无法直接通过转发请求的地址返回给客户端。
    但是在转发请求的HTTP头信息中,增加了X-FORWARDED-FOR信息。用以跟踪原有的客户端IP地址和原来客户端请求的服务器地址。


    转载自:https://blog.csdn.net/jinwufeiyang/article/details/52102449
  • 相关阅读:
    玛里苟斯[清华集训2014 Day1]
    bzoj3585 mex
    Luogu 3793 由乃救爷爷
    Luogu5221 Product
    bzoj1834 [ZJOI2010]network 网络扩容
    CF650C Table Compression
    bzoj3211 花神游历各国
    bzoj1066 [SCOI2007]蜥蜴
    hdu2121 Ice_cream's world II
    Luogu2792 [JSOI2008]小店购物
  • 原文地址:https://www.cnblogs.com/yangyang2018/p/9227800.html
Copyright © 2020-2023  润新知