REQUEST.GETREMOTEADDR () How to get the real IP address of the user
Source: Internet
Author: User
REQUEST.GETREMOTEADDR () How to get the real IP address of the user
The x-forwarded-for variable in the request header is required to obtain the user's true IP address.
Request.getheader ("X-forwarded-for");
The complete example is as follows
<%@ page contenttype= "text/html; charset=gb2312 "%>
<%@ page import= "java.util.*"%>
<%
String Realip = Request.getheader ("X-forwarded-for");
String IP = request.getremoteaddr ();
enumeration enum = Request.getheadernames ();
while (Enum.hasmoreelements ())
{
String name = (string) enum.nextelement ();
String value = Request.getheader (name);
Out.write (name + "=" + Value + "<br$amp;>quot;$);
}
%>
Your IP address is:<%=realip%>.
In many applications may have the need to record the user's real IP, this time to obtain the user's real IP address, in the JSP, the way to obtain the client's IP address is: request.getremoteaddr (), this method in most cases is valid. However, the real IP address of the client cannot be obtained through the reverse proxy software such as Apache,squid.
This period of time in the design of IP statistics, because the server made a cluster, using the reverse proxy software, the [1] URL reverse proxy for [2] URL, the IP address obtained with the REQUEST.GETREMOTEADDR () method is: 127.0.0.1 or 192.168.1.110, not the real IP of the client. What is the reason for this?
This is the reason for the reverse proxy. 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 visit/[3], it is not our browser actually access to the server index.jsp file, but first by the proxy server to access [4], the proxy server and then return the results of the access to our browser, Because it is the proxy server to access the index.jsp, the IP index.jsp in the Request.getremoteaddr () method 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:
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}
When I visit/[5], however, the IP address returned is always unknown, not 127.0.0.1 or 192.168 as shown above, and when I visit [6], I can return to the real IP address of the client and write a method to verify it.
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$amp;>amp; $lt,%=strname% $amp;>amp; $lt;/td>
<td$amp;>amp; $lt,%=strvalue% $amp;>amp; $lt;/td>
</tr>
<%
21}
22%>
23<tr>
24</table>
Out of the result: X-forwarded-for:unknown. X-forwarded-for does exist, but its value is unknown, continue to find the reason. Search the Internet, the reason is on the squid.
The squid.conf configuration file Forwarded_for item is on by default, if Forwarded_for is set to OFF:
X-forwarded-for:unknown
A look, found that the forwarded_for is set to OFF, the reason found that the Forwarded_for entry is set to On, reboot, Access/[7] To obtain the IP is the client's real IP.
Then we can obtain the client real IP address 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)) {
7 IP = request.getheader ("Wl-proxy-client-ip");
8}
9 if (IP = = NULL | | ip.length () = = 0 | | "Unknown". Equalsignorecase (IP)) {
ip = request.getremoteaddr ();
11}
return IP;
13}
However, if through the multi-level reverse proxy, the value of x-forwarded-for and more than one, but a string of IP values, exactly which is the real user-side real 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
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.