- 实现步骤如下:
(1)新建一个java web工程
(2)新建servers包,新建类GetRequestIp,用来处理获取请求IP,GetRequestIp类完整代码如下:
package servers; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class GetRequestIp {
public static void post(HttpServletRequest request, HttpServletResponse response) { getIpAddress(request);
} public static String getIpAddress(HttpServletRequest request) { String ip = null;
ip = request.getHeader("x-forwarded-for");
System.out.println("x-forwarded-for:" + ip); if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("X-Forwarded-For");
System.out.println("request.getHeader(\"X-Forwarded-For\")=" + ip);
} if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
System.out.println("request.getHeader(\"Proxy-Client-IP\")=" + ip);
} if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
System.out.println("request.getHeader(\"WL-Proxy-Client-IP\")=" + ip);
} if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_CLIENT_IP");
System.out.println("request.getHeader(\"HTTP_CLIENT_IP\")=" + ip);
} if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
System.out.println("request.getHeader(\"HTTP_X_FORWARDED_FOR\")=" + ip);
} if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
System.out.println("request.getRemoteAddr()=" + ip);
} if(null != ip && ip.indexOf(',') != -1)
{
//如果通过了多级反向代理的话,X-Forwarded-For的值并不止一个,而是一串 IP 值
//取X-Forwarded-For中第一个非unknown的有效IP字符串
//如:X-Forwarded-For:192.168.1.110, 192.168.1.120, 192.168.1.130, 192.168.1.100
//用户真实IP为: 192.168.1.110
//注意:当访问地址为 localhost 时 地址格式为 0:0:0:0:0:0:1
System.out.println("ip=" + ip);
String[] ips = ip.split(",");
for (int i = 0; i < ips.length; i++)
{
if(null != ips[i] && !"unknown".equalsIgnoreCase(ips[i]))
{
ip = ips[i];
break;
}
}
if("0:0:0:0:0:0:1".equals(ip))
{
System.out.println("由于客户端访问地址使用 localhost,获取客户端真实IP地址错误,请使用IP方式访问");
}
} if("unknown".equalsIgnoreCase(ip))
{
System.out.println("由于客户端通过Squid反向代理软件访问,获取客户端真实IP地址错误,请更改squid.conf配置文件forwarded_for项默认是为on解决");
} return ip;
}
}
(3)新建一个JSP页面,命名为MyRequest.jsp,完整代码如下:
<%@ page import="servers.GetRequestIp" %>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%> <%
out.clear();
out = pageContext.pushBody();
GetRequestIp.post(request, response);
%>
(4)运行,开启tomcat,在浏览器输入网址:http://127.0.0.1:8085/MyDemo/MyRequest.jsp,查看输出结果。
- 附:获取客户端IP地址时,使用不同代理时三个HTTP请求头的区别
(learn from:http://www.cnblogs.com/technic-emotion/articles/3701257.html)
一、没有使用代理服务器的情况:
- REMOTE_ADDR = 您的 IP
- HTTP_VIA = 没数值或不显示
- HTTP_X_FORWARDED_FOR = 没数值或不显示
二、使用透明代理服务器的情况:Transparent Proxies
- REMOTE_ADDR = 最后一个代理服务器 IP
- HTTP_VIA = 代理服务器 IP
- HTTP_X_FORWARDED_FOR = 您的真实 IP ,经过多个代理服务器时,这个值类似如下:203.98.182.163, 203.98.182.163, 203.129.72.215。
这类代理服务器还是将您的信息转发给您的访问对象,无法达到隐藏真实身份的目的。
三、使用普通匿名代理服务器的情况:Anonymous Proxies
- REMOTE_ADDR = 最后一个代理服务器 IP
- HTTP_VIA = 代理服务器 IP
- HTTP_X_FORWARDED_FOR = 代理服务器 IP ,经过多个代理服务器时,这个值类似如下:203.98.182.163, 203.98.182.163, 203.129.72.215。
此种情况,隐藏了您的真实IP,但是向访问对象透露了您是使用代理服务器访问他们的。
四、使用欺骗性代理服务器的情况:Distorting Proxies
- REMOTE_ADDR = 代理服务器 IP
- HTTP_VIA = 代理服务器 IP
- HTTP_X_FORWARDED_FOR = 随机的 IP,经过多个代理服务器时,这个值类似如下:203.98.182.163, 203.98.182.163, 203.129.72.215。
告诉了访问对象您使用了代理服务器,但编造了一个虚假的随机IP代替您的真实IP欺骗它。
五、使用高匿名代理服务器的情况:High Anonymity Proxies (Elite proxies)
- REMOTE_ADDR = 代理服务器 IP
- HTTP_VIA = 没数值或不显示
- HTTP_X_FORWARDED_FOR = 没数值或不显示 ,经过多个代理服务器时,这个值类似如下:203.98.182.163, 203.98.182.163, 203.129.72.215。
完全用代理服务器的信息替代了您的所有信息,就象您就是完全使用那台代理服务器直接访问对象。