Java uses HttpRequest to obtain the user real IP address _jsp programming

Source: Internet
Author: User

In JSP, the method of obtaining the IP address of the client is: Request.getremoteaddr (), which is effective in most cases. But in the adoption of Apache,squid,nginx and other reverse proxy software can not get to the client's real IP address.

If the reverse proxy software is used, the http://192.168.1.110:2046/URL is reversed to the http://www.jb51.net/URL, 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 http://www.jb51.net/index.jsp/, actually not our browser actually accesses the index.jsp file on the server, but first by the proxy server to access the http://192.168.1.110:2046/ INDEX.JSP, the proxy server returns the results of the visit 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.

Package com.rapido.utils; 
 
Import Javax.servlet.http.HttpServletRequest; /** * Custom Access Object Tool class * * Get the IP address of the object and other information * @author x-rapido * * */public class Cusaccessobjectutil {/** * received Take the user's real IP address, do not use REQUEST.GETREMOTEADDR (), the reason is that it is possible to use the proxy software method to avoid the real IP address, * But, if the adoption of multi-level reverse proxy, the value of x-forwarded-for is more than one, but a string of 
   IP value, which is really the real user side 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: 192.168.1. 
    @param request * @return/public static String getipaddress (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.getheader ("Http_client_ip"); } if (IP = null | | ip.length () = = 0 | | 
    "Unknown". Equalsignorecase (IP) {ip = request.getheader ("Http_x_forwarded_for"); } if (IP = null | | ip.length () = = 0 | | 
    "Unknown". Equalsignorecase (IP) {ip = request.getremoteaddr (); 
  } return IP; 
 } 
   
}

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.