Java中使用servlet内置对象request获取IP地址

Java中使用servlet内置对象request获取IP地址

Controller中获取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();
                }
  • 本文作者: dzou | 微信:17856530567
  • 本文链接: http://www.dzou.top/post/9a42916a.html
  • 版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!
  • 并保留本声明和上方二维码。感谢您的阅读和支持!