JAVA:
httpservletrequest request = servletactioncontext. getrequest ();
String remoteip = (string) request. getremotehost ();
If the client uses a proxy, the method for obtaining the real IP address is:
after proxy, the server cannot directly obtain the IP address of the client because an intermediate layer is added between the client and the service, the server application cannot directly return the request address to the client. However, X-FORWARDED-FOR information is added in the HTTP header information that forwards the request. It is used to track the original Client IP address and the server address of the original client request. When we access index. JSP/is not actually the index on the server accessed by our browser. JSP file, but the index is first accessed by the proxy server. JSP, the proxy server returns the access results to our browser, because the proxy server accesses the index. JSP, so index. in JSP, request. the IP obtained by getremoteaddr () is actually the proxy server address, not the client IP address.
method 1,
Public String getremortip (httpservletrequest request) {
If (request. getheader ("X-forwarded-for") = NULL) {
return request. getremoteaddr ();
}< br> return request. getheader ("X-forwarded-for");
}
Method 2,
Public String getipaddr (httpservletrequest request ){
String IP = request. getheader ("X-forwarded-");
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 a multi-level reverse proxy is passed, there are more than one X-forwarded-for value, but a string of IP values, which is the real IP address of the real client?
the answer is to take the first non-unknown valid IP string in X-forwarded-. For example,
X-forwarded-for: 192.168.1.110, 192.168.1.120, 192.168.1.130, and 192.168.1.100
your real IP address is 192.168.1.110
reference:
http://www.jb51.net/article/21272.htm
http://www.iteye.com/problems/28963