Java Web gets client real IP

Source: Internet
Author: User
Tags java web haproxy
Occurrence of the scene: the server side to receive client requests, the general need for signature verification, client IP restrictions, and so on, in the client IP qualification, the need to first obtain the real IP. Generally divided into two situations:

Mode one, the client is not through the proxy, direct access to the server side (Nginx,squid,haproxy);


Mode two, the client passes the multi-level proxy, finally arrives at the server end (Nginx,squid,haproxy);


The client request information is contained in httpservletrequest and can be obtained by means of getremoteaddr () of the client IP. At this point if the use of a form, you can directly obtain the client real IP. And if it is the way two through the form of proxy, at this time through a multi-level reverse agent, through the method getremoteaddr () without the client real IP, you can get forward after the X-forwarded-for request information. When a client request is forwarded, the IP is appended and separated by commas, for example: 10.47.103.13,4.2.2.2,10.96.112.230.

Parameters in the request:
Request.getheader ("X-forwarded-for"): 10.47.103.13,4.2.2.2,10.96.112.230
Request.getheader ("X-real-ip"): 10.47.103.13
REQUEST.GETREMOTEADDR (): 10.96.112.230

When client access is forwarded, IP is appended and separated by commas. The final and accurate client information is:
X-forwarded-for is not NULL, the first IP before the comma, the X-REAL-IP is not NULL, then the IP; otherwise getremoteaddr (); code example:

/** * Get the user's real IP address, do not use the REQUEST.GETREMOTEADDR () because it is possible that the user used the proxy software to avoid the real IP address, * But, if the adoption of multi-level reverse proxy, x-forwarded-for value does not stop One, but a string of IP values * * @return IP/private String getipaddr (HttpServletRequest request) {string I 
        p = Request.getheader ("X-forwarded-for");
        SYSTEM.OUT.PRINTLN ("X-forwarded-for IP:" + IP); if (IP!= null && ip.length ()!= 0 &&!)
                Unknown ". Equalsignorecase (IP)" {//Multiple reverse proxies will have more than one IP value, the first IP is true IP if (ip.indexof (",")!=-1) {
            ip = Ip.split (",") [0]; } if (IP = null | | ip.length () = = 0 | |  
            "Unknown". Equalsignorecase (IP) {ip = request.getheader ("Proxy-client-ip");
        SYSTEM.OUT.PRINTLN ("Proxy-client-ip IP:" + IP); } if (IP = null | | ip.length () = = 0 | |  
            "Unknown". Equalsignorecase (IP) {ip = request.getheader ("Wl-proxy-client-ip"); System.out.println ("Wl-proxy-client-ip IP:"+ IP); } if (IP = null | | ip.length () = = 0 | |  
            "Unknown". Equalsignorecase (IP) {ip = request.getheader ("Http_client_ip");
        SYSTEM.OUT.PRINTLN ("Http_client_ip IP:" + IP); } if (IP = null | | ip.length () = = 0 | |  
            "Unknown". Equalsignorecase (IP) {ip = request.getheader ("Http_x_forwarded_for");
        SYSTEM.OUT.PRINTLN ("Http_x_forwarded_for IP:" + IP); } if (IP = null | | ip.length () = = 0 | |  
            "Unknown". Equalsignorecase (IP) {ip = request.getheader ("X-real-ip");
        SYSTEM.OUT.PRINTLN ("X-real-ip IP:" + IP); } if (IP = null | | ip.length () = = 0 | |  
            "Unknown". Equalsignorecase (IP) {ip = request.getremoteaddr ();
        SYSTEM.OUT.PRINTLN ("Getremoteaddr IP:" + IP);
        SYSTEM.OUT.PRINTLN ("Get Client IP:" + IP);  
    return IP; }
At this point, the client's true IP can be obtained under normal circumstances. Note that you need to configure the Save X-forwarded-for for the form of load on the server side.

Reprint Address: http://www.cnblogs.com/xiaoxing/p/6565573.html

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.