`
huayu0815
  • 浏览: 57808 次
  • 性别: Icon_minigender_1
  • 来自: 河南
文章分类
社区版块
存档分类
最新评论

httpRequest.getHeader("x-forworded-for") 获得客户端IP

 
阅读更多
在JSP里,获取客户端的IP地址的方法是:request.getRemoteAddr(),这种方法在大部分情况下都是有效的。但是在通过了Apache,Squid等反向代理软件就不能获取到客户端的真实IP地址了。
如果使用了反向代理软件,将http://192.168.1.110:2046/ 的URL反向代理为http://www.xxx.com/ 的URL时,用request.getRemoteAddr()方法获取的IP地址是:127.0.0.1 或 192.168.1.110,而并不是客户端的真实IP。
经过代理以后,由于在客户端和服务之间增加了中间层,因此服务器无法直接拿到客户端的IP,服务器端应用也无法直接通过转发请求的地址返回给客户端。但是在转发请求的HTTP头信息中,增加了X-FORWARDED-FOR信息。用以跟踪原有的客户端IP地址和原来客户端请求的服务器地址。当我们访问http://www.xxx.com/index.jsp/ 时,其实并不是我们浏览器真正访问到了服务器上的index.jsp文件,而是先由代理服务器去访问http://192.168.1.110:2046/index.jsp ,代理服务器再将访问到的结果返回给我们的浏览器,因为是代理服务器去访问index.jsp的,所以index.jsp中通过request.getRemoteAddr()的方法获取的IP实际上是代理服务器的地址,并不是客户端的IP地址。
于是可得出获得客户端真实IP地址的方法一:
public String getRemortIP(HttpServletRequest request) {
if (request.getHeader("x-forwarded-for") == null) {
return request.getRemoteAddr();
}
return request.getHeader("x-forwarded-for");

}

可是当我访问http://www.xxx.com/index.jsp/ 时,返回的IP地址始终是unknown,也并不是如上所示的127.0.0.1 或 192.168.1.110了,而我访问http://192.168.1.110:2046/index.jsp 时,则能返回客户端的真实IP地址,写了个方法去验证。原因出在了Squid上。squid.conf 的配制文件 forwarded_for 项默认是为on,如果 forwarded_for 设成了 off  则:X-Forwarded-For: unknown

于是可得出获得客户端真实IP地址的方法二:

1publicStringgetIpAddr(HttpServletRequestrequest){
2Stringip=request.getHeader("x-forwarded-for");
3if(ip==null||ip.length()==0||"unknown".equalsIgnoreCase(ip)){
4ip=request.getHeader("Proxy-Client-IP");
5}

6if(ip==null||ip.length()==0||"unknown".equalsIgnoreCase(ip)){
7ip=request.getHeader("WL-Proxy-Client-IP");
8}

9if(ip==null||ip.length()==0||"unknown".equalsIgnoreCase(ip)){
10ip=request.getRemoteAddr();
11}

12returnip;
13}

  可是,如果通过了多级反向代理的话,X-Forwarded-For的值并不止一个,而是一串Ip值,究竟哪个才是真正的用户端的真实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


说明:按这种方法不一定100%准,网上很多人提到要准确的话必须做一个客户端空间,如applet。


分享到:
评论

相关推荐

    java开发常用jar包

    import org.apache.http.HttpRequest; import org.apache.http.HttpRequestInterceptor; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache....

    获取IP地址和MAC地址

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

    HttpRequest.java-master.rar

    HttpRequest 程序 idea2017测试通过,通过HTTP Request获取数据。

    org.apache.http.httpentity jar包-系列jar包

    import org.apache.http.HttpRequest; import org.apache.http.HttpRequestInterceptor; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.NameValuePair; ...

    HttpRequest.java下载

    HttpRequest.java程序源码下载

    HttpRequest--java读取http post请求

    java读取接口代码,形如:http://127.0.0.1:8088/hlapp/train/validationCardInfo?a=X&b=X 这样的接口可以用该方法。

    httprequest.zip

    1、windows下使用socket访问http rest服务,包括get和post两种方法,不依赖任何其他库 2、循环接收数据返回数据,解决中文乱码问题 3、基于vs2010的解决方案,可直接运行 4、核心类:CHttpRequest

    HTTPRequest 源码

    HTTPRequest is a single-header C++ library for making HTTP requests. You can just include it in your project and use it. HTTPRequest was tested on macOS, Windows, Haiku, BSD, and GNU/Linux, but it ...

    HTTPRequest源码

    HTTPRequest is a single-header C++ library for making HTTP requests. You can just include it in your project and use it. HTTPRequest was tested on macOS, Windows, Haiku, BSD, and GNU/Linux, but it ...

    php 模拟get/post封装类

    'ip' => '我是IP', 'cookie' => 'cookie.txt', ); //第一种GET /* $http = new HttpRequest(); $res = $http->set($config) ->open('http://127.0.0.1/HttpRequest/r.php') ->get(); */ //第二种GET /* $...

    httprequest.c

    httprequest.c

    HTTPRequest-lib-master.rar

    HTTPRequest-lib-master IDEA2017测试通过,通过http request 获取网站数据

    服务器下载文件

    //sPath = HttpRequest.forwardIn(restTemplate,"http://common-method/","getFileUrl",obj); InputStream fis=new BufferedInputStream(new FileInputStream(path)); System.out.println("----当前路径----"+new...

    httpRequest.class.php:适用于一般HTTP请求PHP cURL包装器

    httpRequest.class.php 在PHP中轻松执行常规HTTP请求。 要求 php5 php5-curl 用法 初始化 $ req = new httpRequest( 'http://example.org/itemPage.html' ); 发出POST请求 $ req -> setPOST ( $ rawPostData );...

    HttpRequest-Reptile-master.rar

    HTTPRequest-Reptile-master IDEA2017测试通过,通过http request 获取网站数据,爬虫应用

    HTTPrequest.exe 模拟HTTP请求

    用来模拟http请求,包括get和post两种,可以在框中输入body消息,且能打印服务器端的消息响应,包括错误码

    HTTPRequest:单头C ++ HTTP请求类

    HTTPRequest已在macOS,Windows和Linux上进行了测试,但它应可在大多数基于Linux的平台上运行。 支持IPv4和IPv6。 用法 GET请求的例子 # include " HTTPRequest.hpp " try { // you can pass ...

    vc++ 应用源码包_10

    独立打包,保证可解压,内含大量源码,网上搜集而来。一共10几包,每个包几十兆。...WebBrowser-Httprequest(http请求捕捉).rar Windows Shell扩展.zip Windows核心编程第五版随书源代码.rar windows设计 源码.zip

    火狐浏览器-firefox-httprequester-2.2-fx.xpi

    火狐浏览器全新升级后,很多插件不兼容,请下载Firefox Setup 56.0b9之前版本安装使用

Global site tag (gtag.js) - Google Analytics