其他分享
首页 > 其他分享> > Spring MVC获取请求的IP通用方案

Spring MVC获取请求的IP通用方案

作者:互联网

纯Java代码片段:

private 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.getHeader("X-Real-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

标签:IP,ip,request,unknown,equalsIgnoreCase,MVC,Spring,null
来源: https://blog.csdn.net/Saintmm/article/details/123635924