//获取客户端访问的ip地址
public static String getIp() {
HttpServletRequest request = ServletActionContext.getRequest();
String ip = request.getHeader("X-Forwarded-For");
if(StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(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)){
return ip;
}
return request.getRemoteAddr();
}
获取客户端访问的ip地址
猜你喜欢
转载自blog.csdn.net/M_Jack/article/details/105404635
今日推荐
周排行