java 获取访问的真实ip

request 是 javax.servlet.http.HttpServletRequest

 获取其他机器访问自己服务时的真实ip

 

 public String getIP(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;
    }

 

posted @ 2021-12-29 15:43  不是安逸  阅读(153)  评论(0编辑  收藏  举报