获取客户端的请求IP地址
获取客户端的请求IP地址
/**
* 获取IP
* @param request
* @return
*/
public static String getIp(HttpServletRequest request) {
String ip = request.getHeader("X-Forwarded-For");
if (StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)) {
logger.info("=====1.查看是否多个ip地址=======" + ip);
//多次反向代理后会有多个ip值,第一个ip才是真实ip
int index = ip.indexOf(",");
if (index != -1) {
return ip.substring(0, index);
} else {
return ip;
}
}
ip = request.getHeader("X-Real-IP");
if (StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)) {
logger.info("=====2.查看是否多个ip地址=======" + ip);
return ip;
}
return request.getRemoteAddr();
}