springmvc怎么获取用户ip

springmvc怎么获取用户ip,第1张


/
  获取访问者IP地址
  <p>在一般情况下使用RequestgetRemoteAddr()即可,但是经过nginx等反向代理软件后,这个方法会失效。</p>
  <p>本方法先从Header中获取X-Real-IP,如果不存在再从X-Forwarded-For获得第一个IP(用,分割)。</p>
  <p>如果还不存在则调用RequestgetRemoteAddr()。</p>
  @param request
  @return
 /
public static String getIp(HttpServletRequest request) {
String ip = requestgetHeader("X-Real-IP");
if (ValidateUtilisNotEmpty(ip) && !"unknown"equalsIgnoreCase(ip)) {
return ip;
}
ip = requestgetHeader("X-Forwarded-For");
if (ValidateUtilisNotEmpty(ip) && !"unknown"equalsIgnoreCase(ip)) {
int index = ipindexOf(",");
if (index != -1) {
return ipsubstring(0, index);
} else {
return ip;
}
} else {
return requestgetRemoteAddr();
}
}

从HttpServletRequest中获取

以上就是关于springmvc怎么获取用户ip全部的内容,包括:springmvc怎么获取用户ip、、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9726400.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-01
下一篇2023-05-01

发表评论

登录后才能评论

评论列表(0条)

    保存