In the JSP, the method to obtain the IP address of the client is: Request.getremoteaddr (), which is valid in most cases. However, the real IP address of the client cannot be obtained through the reverse proxy software such as Apache,squid.
If the reverse proxy software is used, the URL of the http://192.168.1.110:2046/is reverse proxy to http://www.javapeixun.com.cn/URL, The IP address obtained with the REQUEST.GETREMOTEADDR () method is: 127.0.0.1 or 192.168.1.110, not the real IP of the client.
After the agent, due to the addition of the middle tier between the client and the service, so the server can not directly get the client's IP, the server-side application can not directly forward the requested address to the client. However, the x-forwarded-for information is added to the HTTP header information of the forwarding request. Used to track the original client IP address and the server address of the original client request. When we visit http://www.javapeixun.com.cn/index.jsp/, it is not that our browser actually accesses the index.jsp file on the server, but first the proxy server accesses HTTP/ 192.168.1.110:2046/INDEX.JSP, the proxy server will return the results of the access to our browser, because it is the proxy server to access the index.jsp, so index.jsp through The Request.getremoteaddr () method gets the IP address of the proxy server, which is actually the address of the client.
So we can get the real IP address of the client method one:
- publi Cif == nullreturnreturn
- }
When I visit http://www.5a520.cn/index.jsp/, however, the IP address returned is always unknown, not the 127.0.0.1 or 192.168.1.110 as shown above, and I visit http:// 192.168.1.110:2046/index.jsp, you can return the real IP address of the client and write a method to verify it. The reason was on the squid. The squid.conf configuration file Forwarded_for entry is on by default, if Forwarded_for is set to OFF: X-forwarded-for:unknown
Then we can obtain the client real IP address method two:
- public
- Ifnull | | . Equalsignorecase (IP)) {
- );
- Ifnull | | . Equalsignorecase (IP)) {
- );
- ifnull | |
- Return }
However, if through the multi-level reverse proxy, x-forwarded-for value and more than one, but a string of IP values, exactly which is the real client IP?
The answer is to take the first non-unknown valid IP string in x-forwarded-for.
such as: x-forwarded-for:192.168.1.110, 192.168.1.120, 192.168.1.130, 192.168.1.100 user real IP: 192.168.1.110
If the above method is not enough, use the following method:
[Java]View Plain copy
- /**
- * Get current network IP
- * @param request
- * @return
- */
- Public String getipaddr (httpservletrequest request) {
- String ipAddress = Request.getheader ("x-forwarded-for");
- if (ipAddress = = Null | | ipaddress.length () = = 0 | | "Unknown". Equalsignorecase (ipAddress)) {
- ipAddress = Request.getheader ("Proxy-client-ip");
- }
- if (ipAddress = = Null | | ipaddress.length () = = 0 | | "Unknown". Equalsignorecase (ipAddress)) {
- ipAddress = Request.getheader ("Wl-proxy-client-ip");
- }
- if (ipAddress = = Null | | ipaddress.length () = = 0 | | "Unknown". Equalsignorecase (ipAddress)) {
- IpAddress = Request.getremoteaddr ();
- if (ipaddress.equals ("127.0.0.1") | | | ipaddress.equals ("0:0:0:0:0:0:0:1")) {
- ///IP based on NIC for native configuration
- InetAddress inet=null;
- try {
- inet = Inetaddress.getlocalhost ();
- } catch (Unknownhostexception e) {
- E.printstacktrace ();
- }
- Ipaddress= inet.gethostaddress ();
- }
- }
- //For the case of multiple agents, the first IP is the client real IP, multiple IPs according to ', ' split
- if (ipaddress!=null && ipaddress.length () >){ //"***.***.***.***". Length () =
- if (Ipaddress.indexof (",") >0) {
- ipAddress = ipaddress.substring (0,ipaddress.indexof (","));
- }
- }
- return ipAddress;
- }
The above is from the network. I just integrate with it. Hope to help everyone!
Multi-method integration of the real IP address of Java GET request client under multi-level reverse proxy