转自:
下文讲述获取访问者的真实IP的方法分享,如下所示:
当使用反向代理后,我们<%=request.getRemoteAddr() %>,会返回127.0.0.1
那么如何获取访问者的IP信息呢?下文将一一道来,如下所示:
实现思路:
借助以下方法即可获取访问者的真实IP
public String getIpAddr(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(); } return ip; }