In the JSP, the method to obtain the client's IP address is:
request.getremoteaddr (), this method is effective in most cases. However, the real IP address of the client cannot be obtained through the reverse proxy software such as Apache,squid. If the reverse proxy software is used, thehttp://192.168.1.110:2046/URL Reverse proxy for http://www.xxx.com/URL, use
request.getremoteaddr () The IP address obtained by the method is: 127.0.0.1 or 192.168.1.110, not the real IP of the client. After the agent, due to the addition of the middle tier between the client and the service, so the server can not directly get the client's IP, the server-side application can not directly forward the requested address to the client. However, the x-forwarded-for information is added to the HTTP header information of the forwarding request. Used to track the original client IP address and the server address of the original client request. When we visithttp://www.xxx.com/index.jsp/, in fact, is not our browser actually access to the server index.jsp file, but first by the proxy server to access http://192.168.1.110:2046/ INDEX.JSP, the proxy server will return the results of the access to our browser, because it is the proxy server to access index.jsp, so index.jsp through the
request.getremoteaddr () is actually the address of the proxy server, not the IP address of the client. So we can get the real IP address of the client method one: Java code
- Public String Getremortip (HttpServletRequest request) {
- if (Request.getheader ("x-forwarded-for") = = null) {
- return request.getremoteaddr ();
- }
- return Request.getheader ("x-forwarded-for");
- }
But when I visit http://www.xxx.com/index.jsp/, the IP address returned is always unknown, not 127.0.0.1 or 192.168.1.110 as shown above , and I access the http ://192.168.1.110:2046/index.jsp, you can return the real IP address of the client, write a method to verify. The reason was on the squid. The squid.conf configuration file Forwarded_for entry is on by default, if Forwarded_for is set to OFF: X-forwarded-for:unknown
Then we can obtain the client real IP address method two:
Java code
- 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 through the multi-level reverse proxy, x-forwarded-for value and more than one, but a string of IP values, exactly which is the real client IP?
The answer is to take the first non-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.110
Note: According to this method is not necessarily 100% quasi, online many people mentioned to be accurate words must do a client space, such as applets.
reprint to: http://xiaoboss.iteye.com/blog/1181488
Java Get IP Address: request.getremoteaddr () beware