首页 > Java > java教程 > 获得客户端的真实IP

获得客户端的真实IP

巴扎黑
发布: 2016-12-10 09:52:24
原创
1339 人浏览过

public class IPUtil { 
    public static String getIpAddr(HttpServletRequest request) { 
        String ip = request.getHeader("X-Real-IP"); 
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 
         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(); 
        } 
        //防止多级代理时返回过个ip。 
        if(ip != null && ip.indexOf(",") != -1){ 
            ip= ip.substring(0,ip.indexOf(",")); 
        } 
        return ip; 
    } 
}

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板