In the development work, we often need to obtain the client's IP. The general method to obtain the IP address of the client is: Request.getremoteaddr (), but the real IP address of the client cannot be obtained by the reverse proxy software such as Apache,squid.
Cause: Because the intermediary agent is added between the client and the service, the server cannot get the client's IP directly, and the server-side application cannot return to the client directly by forwarding the requested address.
Now the diagram of proxy Internet and IP relationship:
First case: Do not go online through the proxy, the server to get the real IP
The second situation: through Proxy server such as: Nginx,squid, such as a layer of proxy or multi-layer proxy network, such as:
Note that x-forwarded-for and X-REAL-IP are not the official HTTP protocol header, but squid and other reverse proxy software introduced the earliest, the reason resin can get, is because Nginx generally default will be so configured to forward HTTP requests:
Location/{
Proxy_pass http://yourdomain.com;
Proxy_set_header Host $host;
Proxy_set_header X-real-ip $remote _addr;
Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;
}, from the definition of x-forwarded-for, Ips[0] is the original client IP, if this is not, then take the second is more unreliable, we usually test the time, may be directly in the network hanging agent to visit, and outside the network path visited by the Netizen is not the same, The following is not always added is through each layer of proxy IP only, below an example;
REQUEST.GETREMOTEADDR () 192.168.239.196
Request.getheader ("X-forwarded-for") 58.63.227.162, 192.168.237.178, 192.168.238.218
Request.getheader ("X-real-ip") 192.168.238.218
So the process of access should be this way, the client 58.63.227.162 the request, after 192.168.237.178, 192.168.238.218 Two-tier forwarding, To the 192.168.239.196 this nginx, Nginx put X-REAL-IP head set as oneself see REMOTE_ADDR, That is, sent directly to his 192.168.238.218, this time resin received this package, for resin directly to his remote_addr is Nginx IP, That is 192.168.239.196, then resin inside the request.getremoteaddr () 192.168.239.196, then take the most primitive IP logic in resin (that is, take the outermost IP that can be known):
If Xff is not empty, take Xff's left first
If Xff is empty, take XRI
If Xri is empty, can only take request.getremoteaddr (), that is, can only get the most direct to his machine IP,
Others are not elegant, the reference code is as follows:
The first type of code:
1 /** 2 * Obtains the client IP from the request object, handles the HTTP proxy server and the Nginx reverse proxy intercepts the IP 3 * @param request 4 * @return IP 5 * *6 Public StaticString Getlocalip (HttpServletRequest request) {7String remoteaddr = Request.getremoteaddr ();8String forwarded = Request.getheader ("x-forwarded-for");9String Realip = Request.getheader ("X-real-ip");Ten OneString IP =NULL; A if(Realip = =NULL) { - if(Forwarded = =NULL) { -ip = remoteaddr; the}Else{ -IP = remoteaddr + "/"+ Forwarded.split (",") [0]; -} -}Else{ + if(Realip.equals (forwarded)) { -ip = realip; +}Else{ A if(Forwarded! =NULL){ atforwarded = Forwarded.split (",") [0]; -} -IP = realip + "/"+ forwarded; -} -} - returnip in}
The second type of code:
1 Public StaticString GetIP (HttpServletRequest request) {2String remoteaddr = Request.getremoteaddr ();3String forwarded = Request.getheader ("x-forwarded-for");4String Realip = Request.getheader ("X-real-ip");5 6String IP =NULL;7 if(Realip = =NULL) {8 if(Forwarded = =NULL) {9ip = remoteaddr;Ten}Else{ OneIP = remoteaddr + "/"+ forwarded; A} -}Else{ - if(Realip.equals (forwarded)) { theip = realip; -}Else{ -IP = realip + "/"+ Forwarded.replaceall (", "+ Realip,""); -} +} - returnip +}
The third type of code:
1 Public StaticString GETIP2 (HttpServletRequest request) {2String IP = Request.getheader ("x-forwarded-for");3 if(Stringutils.isnotempty (IP) &&! "UnKnown". Equalsignorecase (IP)) {4 //Multiple reverse proxies will have more than one IP value, the first IP is the real IP5 intindex = Ip.indexof (",");6 if(Index! =-1) {7 returnIp.substring (0,index);8}Else{9 returnipTen} One} Aip = Request.getheader ("X-real-ip"); - if(Stringutils.isnotempty (IP) &&! "UnKnown". Equalsignorecase (IP)) { - returnip the} - returnRequest.getremoteaddr (); -}
The third is the most appropriate, most clearly understood
Content from: cnblogs: Milk, unsweetened
Java get client IP