From the Blogjava an article, wrote a good, original address:
Http://www.blogjava.net/Alpha/archive/2006/07/12/57764.html?Pending=true#Post
In many applications may have to the user's real IP records down, at this point to get the user's real IP address, in the JSP, obtain the client's IP address method is:request.getremoteaddr (), this method is effective in most cases. But in the adoption of APACHE,SQUID and other reverse proxy software can not get to the client's real IP address.
This time in doing IP statistics program design, because the server made a cluster, using the reverse proxy software, will http://192.168.1.110:2046/URL reverse proxy for the http://www.xxx.com/URL, withrequest.getremoteaddr ()Method gets the IP address: 127.0.0.1 or 192.168.1.110, not the real IP of the client. What is the reason for that?
This is the reason for the reverse proxy. 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.xxx.com/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 will return the results of the access to our browser, because it is the proxy server to access index.jsp, so index.jsprequest.getremoteaddr ()method to obtain the IP is actually the proxy server address, not the client's IP address.
Then you can get the client real IP address method one:
1 public String getipaddr (HttpServletRequest request) {
2 String IP = request.getheader ("X-forwarded-for");
3 if (IP = null | | ip.length () = = 0) {
4 IP = request.getremoteaddr ();
5}
6 return IP;
7}
But when I visit http://www.xxx.com/index.jsp/, the IP address that is returned is always unknown, not 127.0.0.1 or 192.168.1.110 as shown above, and I visit http:// 192.168.1.110:2046/index.jsp, you can return the client's real IP address, write a method to verify.
1 <% @ Page import= "java.util.*"%>
2 < table border =1 cellspacing =0 cellpadding =0 align =center >
3 < tr >
4 < th > Name </th >
5 < th > Value </th >
6 </TR >
7 <%
8Enumeration Enumnames;
9String Strname,strvalue;
10
11enumNames = Request.getheadernames ();
12while (Enumnames.hasmoreelements ()) {
StrName = (String) enumnames.nextelement ();
strvalue = Request.getheader (strName);
%>
< tr >
< td > <% = strName%> </td >
< td > <% = strvalue%> </td >
</TR >
<%
21}
%>
< tr >
</table >
25
Out of the result: X-forwarded-for:unknown. X-forwarded-for does exist, but its value is unknown, continue to find the reason. Search on the Internet, the reason for the squid on.
The squid.conf configuration file forwarded_for The default is on, if Forwarded_for is set to OFF:
X-forwarded-for:unknown
A check, found Forwarded_for set for off, the reason found, the forwarded_for set for on, after the restart, Access http://www.xxx.com/index.jsp/IP is the client's real IP.
Then you can get the client real IP address of the method two:
1 public String getipaddr (HttpServletRequest request) {
2 String IP = request.getheader ("X-forwarded-for");
3 if (IP = null | | ip.length () = 0 | | "Unknown". Equalsignorecase (IP)) {
4 IP = request.getheader ("Proxy-client-ip");
5}
6 if (ip == null | | ip.length () == 0 | | "Unknown". Equalsignorecase (IP)