But in the adoption of APACHE,SQUID and other reverse proxy software can not get to the client's real IP address. If the reverse proxy software is used, the IP address obtained using the REQUEST.GETREMOTEADDR () method is 127.0.0.1 or 192.168.1.110, not the real IP of the client.
After the agent, because the client and the service increased between the middle tier, so the server can not directly to the client IP, server-side applications can not directly by forwarding the requested address to return to the client. However, in the HTTP header message that forwards the request, the X-forwarded-for information is added. The server address used to track the original client IP address and the original client request. When we visit index.jsp/, in fact, is not our browser really access to the server index.jsp files, but first by proxy server to access index.jsp, Proxy server will access to the results returned to our browser, Because it is the proxy server to access index.jsp, so index.jsp through the Request.getremoteaddr () method of IP is actually proxy server address, not the client's IP address.
Then you can get the client real IP address method one:
Copy Code code as follows:
Public String Getremortip (HttpServletRequest request) {
if (Request.getheader ("x-forwarded-for") = = null) {
return request.getremoteaddr ();
}
Return Request.getheader ("X-forwarded-for");
}
How to obtain the client's true IP address two:
Copy Code code as follows:
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;
}
However, if the adoption of multi-level reverse proxy, x-forwarded-for value and more than one, but a series of IP values, exactly which is the real user end of the real IP?
The answer is to take the first 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 is: 192.168.1.110