`

servlet request getHeader("x-forwarded-for") 获取真实IP

    博客分类:
  • Java
阅读更多

在JSP里,获取客户端的IP地址的方法是:request.getRemoteAddr(),这种方法在大部分情况下都是有效的。但是在通过了 Apache,Nagix等反向代理软件就不能获取到客户端的真实IP地址了。如果使用了反向代理软件,用 request.getRemoteAddr()方法获取的IP地址是:127.0.0.1或 192.168.1.110,而并不是客户端的真实IP。

经过代理以后,由于在客户端和服务之间增加了中间层,因此服务器无法直接拿到客户端的 IP,服务器端应用也无法直接通过转发请求的地址返回给客户端。但是在转发请求的HTTP头信息中,增加了X-FORWARDED-FOR信息。用以跟踪原有的客户端 IP地址和原来客户端请求的服务器地址。

举例来说,当我们访问口碑网首页hangzhou.jsp时,其实并不是我们浏览器真正访问到了服务器上的hangzhou.jsp 文件,而是先由代理服务器Nagix去访问hagnzhou.jsp ,代理服务器再将访问到的结果返回给我们的浏览器,因为是代理服务器去访问hangzhou.jsp的,所以hangzhou.jsp中通过 request.getRemoteAddr()的方法获取的IP实际上是代理服务器的地址,并不是客户端的IP地址。

<hr/>

获得客户端真实IP地址的方法一:

public String getRemortIP(HttpServletRequest request) {
   if (request.getHeader("x-forwarded-for") == null) {
   return request.getRemoteAddr();
   }
   return request.getHeader("x-forwarded-for");
  }

<hr/>

获得客户端真实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;
  }

 

 

转:http://blog.csdn.net/z69183787/article/details/21187485

分享到:
评论

相关推荐

    IP工具类,通过传入的request获取ip地址(JAVA)

    同样,如果在反向代理环境中,我们需要检查`X-Forwarded-For`头来获取真实的IP: ```java String forwardedFor = request.getHeaders().getFirst("X-Forwarded-For"); if (forwardedFor != null) { ipAddress = ...

    java获取用户真实ip

    为解决这一问题,可以通过检查`x-forwarded-for`字段获取到一个IP地址列表,并从中选取最后一个作为客户端真实IP地址。 ```java if (ip != null && ip.indexOf(",") &gt; 0) { ip = ip.substring(ip.lastIndexOf(",")...

    idea,java获取ip地址

    String ipAddress = request.getHeader("X-Forwarded-For"); if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) { ipAddress = request.getRemoteAddr(); } // 处理...

    获取客户端IP

    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...

    Java获取登录用户的IP地址示例代码.rar

    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...

    Java根据Request获取客户端IP

    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() =...

    获取客户的ip小例子

    String clientIP = request.getHeader("X-Forwarded-For"); if (clientIP == null || clientIP.length() == 0 || "unknown".equalsIgnoreCase(clientIP)) { clientIP = request.getRemoteAddr(); } else { // ...

    登录ip BS java

    4. `request.getHeader("HTTP_CLIENT_IP")` 和 `request.getHeader("HTTP_X_FORWARDED_FOR")`: 这两个字段与"X-Forwarded-For"类似,是备用的获取IP的尝试。 5. `request.getRemoteAddr()`: 如果所有上述尝试都失败...

    解析ip,用户给一个请求,获取用户的ip,浏览器,操作系统等信息

    String ip = request.getHeader("X-Forwarded-For"); if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getRemoteAddr(); } ``` 这段代码首先尝试从`X-Forwarded-For`头...

    案例111

    String forwardedFor = request.getHeader("X-Forwarded-For"); if (forwardedFor != null && !forwardedFor.isEmpty()) { clientIP = forwardedFor.split(",")[0]; } String userAgent = request.getHeader(...

    jsp获取客户端IP地址的方法

    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() =...

    spring,获取IP精选.doc

    可以通过 `HttpServletRequest.getHeader("X-Forwarded-For")` 获取,但这个值可能包含多个 IP,因为请求可能经过多个代理服务器,需要处理好这种情况。 3. **WebFilter**: 如果使用 Spring WebFlux,可以实现 `Web...

    java脚本开发根据客户IP获取IP的具体地理位置信息

    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...

    java webservice服务器端获取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...

    获得访问者的IP地址

    在这种情况下,可以尝试使用`getHeader("X-Forwarded-For")`方法,它通常会包含原始客户端IP。但请注意,这个头字段可以被轻易伪造,因此在依赖它时需谨慎。 总的来说,这个"Test-IP"项目提供了一个基础的平台,让...

    java servlet获得客户端相关信息的简单代码

    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...

    网站访客记录数

    在这个例子中,我们通过`request.getHeader("X-Forwarded-For")`获取用户的IP地址,如果无法获取,就使用`request.getRemoteAddr()`作为备选。然后,调用`saveVisitorRecord(ip)`方法将IP地址保存到数据库或日志文件...

    Java利用Request请求如何获取IP地址对应的省份、城市详解

    String xForwardedFor = request.getHeader("X-Forwarded-For"); if (StringUtils.isNotEmpty(xForwardedFor) && !"unKnown".equalsIgnoreCase(xForwardedFor)) { // 多次反向代理后会有多个ip值,第一个ip才是...

    java获取登录者IP和登录时间的两种实现代码详解

    String ip = request.getHeader("x-forwarded-for"); System.out.println("x-forwarded-for ip: " + ip); if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) { // 多次反向代理后会有多...

    Java获取用户访问IP及地理位置的方法详解

    String ip = request.getHeader("x-forwarded-for"); if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("Proxy-Client-IP"); } // ... 其他检查代理服务器的...

Global site tag (gtag.js) - Google Analytics