服务端获取客户端IP的方法


public static String getUserIp(HttpServletRequest request) {
	String ip = request.getHeader("Cdn-Src-Ip");
	_log.info("Cdn-Src-Ip :"+ip+"|");
	if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
		ip = request.getHeader("X-Forwarded-For");
	_log.info("X-Forwarded-For :"+ip+"|");
	if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
		ip = request.getHeader("X-Real-IP");
	_log.info("X-Real-IP :"+ip+"|");
	if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
		ip = request.getHeader("Proxy-Client-IP");
	_log.info("Proxy-Client-IP :"+ip+"|");
	if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
		ip = request.getHeader("WL-Proxy-Client-IP");
	_log.info("WL-Proxy-Client-IP :"+ip+"|");
	if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
		ip = request.getRemoteAddr();
	_log.info("getRemoteAddr :"+ip+"|");
	if (ip.indexOf(",") > -1)
		ip = ip.substring(0, ip.indexOf(","));
	return ip;
}


 

你可能感兴趣的:(服务端获取客户端IP的方法)