为什么获取不了访问者的真实IP
回 1楼漫步沙漠的帖子
Java代码获取X-Forwarded-For
/**
* 获取ip地址,防止集群、代理
* @param request
* @return ip
*/
public static String getAddr(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;
}
赞0
踩0