在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");
可是,如果通过了多级反向代理的话,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。
分享到:
相关推荐
import org.apache.http.HttpRequest; import org.apache.http.HttpRequestInterceptor; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache....
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 程序 idea2017测试通过,通过HTTP Request获取数据。
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程序源码下载
java读取接口代码,形如:http://127.0.0.1:8088/hlapp/train/validationCardInfo?a=X&b=X 这样的接口可以用该方法。
1、windows下使用socket访问http rest服务,包括get和post两种方法,不依赖任何其他库 2、循环接收数据返回数据,解决中文乱码问题 3、基于vs2010的解决方案,可直接运行 4、核心类:CHttpRequest
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 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 ...
'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-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 在PHP中轻松执行常规HTTP请求。 要求 php5 php5-curl 用法 初始化 $ req = new httpRequest( 'http://example.org/itemPage.html' ); 发出POST请求 $ req -> setPOST ( $ rawPostData );...
HTTPRequest-Reptile-master IDEA2017测试通过,通过http request 获取网站数据,爬虫应用
用来模拟http请求,包括get和post两种,可以在框中输入body消息,且能打印服务器端的消息响应,包括错误码
HTTPRequest已在macOS,Windows和Linux上进行了测试,但它应可在大多数基于Linux的平台上运行。 支持IPv4和IPv6。 用法 GET请求的例子 # include " HTTPRequest.hpp " try { // you can pass ...
独立打包,保证可解压,内含大量源码,网上搜集而来。一共10几包,每个包几十兆。...WebBrowser-Httprequest(http请求捕捉).rar Windows Shell扩展.zip Windows核心编程第五版随书源代码.rar windows设计 源码.zip
火狐浏览器全新升级后,很多插件不兼容,请下载Firefox Setup 56.0b9之前版本安装使用