可是,如果通过了多级反向代理的话,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
public String getIpAddr(HttpServletRequest request) { 2 String ip = request.getHeader("x-forwarded-for"); 3 if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 4 ip = request.getHeader("Proxy-Client-IP"); 5 } 6 if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 7 ip = request.getHeader("WL-Proxy-Client-IP"); 8 } 9 if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 10 ip = request.getRemoteAddr(); 11 } 12 return ip; 13 }
posted on 2008-04-24 10:31 .VwV. 阅读(312) 评论(0) 编辑 收藏 所属分类: java
Powered by: BlogJava Copyright © .VwV.