在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;
}
相关推荐
同样,如果在反向代理环境中,我们需要检查`X-Forwarded-For`头来获取真实的IP: ```java String forwardedFor = request.getHeaders().getFirst("X-Forwarded-For"); if (forwardedFor != null) { ipAddress = ...
为解决这一问题,可以通过检查`x-forwarded-for`字段获取到一个IP地址列表,并从中选取最后一个作为客户端真实IP地址。 ```java if (ip != null && ip.indexOf(",") > 0) { ip = ip.substring(ip.lastIndexOf(",")...
String ipAddress = request.getHeader("X-Forwarded-For"); if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) { ipAddress = request.getRemoteAddr(); } // 处理...
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...
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...
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() =...
String clientIP = request.getHeader("X-Forwarded-For"); if (clientIP == null || clientIP.length() == 0 || "unknown".equalsIgnoreCase(clientIP)) { clientIP = request.getRemoteAddr(); } else { // ...
4. `request.getHeader("HTTP_CLIENT_IP")` 和 `request.getHeader("HTTP_X_FORWARDED_FOR")`: 这两个字段与"X-Forwarded-For"类似,是备用的获取IP的尝试。 5. `request.getRemoteAddr()`: 如果所有上述尝试都失败...
String ip = request.getHeader("X-Forwarded-For"); if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getRemoteAddr(); } ``` 这段代码首先尝试从`X-Forwarded-For`头...
String forwardedFor = request.getHeader("X-Forwarded-For"); if (forwardedFor != null && !forwardedFor.isEmpty()) { clientIP = forwardedFor.split(",")[0]; } String userAgent = request.getHeader(...
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() =...
可以通过 `HttpServletRequest.getHeader("X-Forwarded-For")` 获取,但这个值可能包含多个 IP,因为请求可能经过多个代理服务器,需要处理好这种情况。 3. **WebFilter**: 如果使用 Spring WebFlux,可以实现 `Web...
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...
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...
在这种情况下,可以尝试使用`getHeader("X-Forwarded-For")`方法,它通常会包含原始客户端IP。但请注意,这个头字段可以被轻易伪造,因此在依赖它时需谨慎。 总的来说,这个"Test-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...
在这个例子中,我们通过`request.getHeader("X-Forwarded-For")`获取用户的IP地址,如果无法获取,就使用`request.getRemoteAddr()`作为备选。然后,调用`saveVisitorRecord(ip)`方法将IP地址保存到数据库或日志文件...
String xForwardedFor = request.getHeader("X-Forwarded-For"); if (StringUtils.isNotEmpty(xForwardedFor) && !"unKnown".equalsIgnoreCase(xForwardedFor)) { // 多次反向代理后会有多个ip值,第一个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)) { // 多次反向代理后会有多...
String ip = request.getHeader("x-forwarded-for"); if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("Proxy-Client-IP"); } // ... 其他检查代理服务器的...