JSP gets the real IP address code _jsp programming

Source: Internet
Author: User
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
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.