JSP從服務器獲取客戶端訪問IP

在很多應用下都可能有需要將用戶的真實IP記錄下來,這時就要獲得用戶的真實IP地址,在JSP裏,獲取客戶端的IP地址的方法是: request.getRemoteAddr(),這種方法在大部分情況下都是有效的。但是在通過了Apache,Squid等反向代理軟件就不能獲取到客戶端的真實IP地址了。

  這段時間在做IP統計的程序設計,由於服務器作了集羣,使用了反向代理軟件,將http://192.168.1.110:2046/的 URL反向代理爲http://www.xxx.com/的URL時,用request.getRemoteAddr()方法獲取的IP地址是: 127.0.0.1 或 192.168.1.110,而並不是客戶端的真實IP。這是什麼原因呢?

  這是反向代理的原因。經過代理以後,由於在客戶端和服務之間增加了中間層,因此服務器無法直接拿到客戶端的IP,服務器端應用也無法直接通過轉發請求的地址返回給客戶端。但是在轉發請求的HTTP頭信息中,增加了X-FORWARDED-FOR信息。用以跟蹤原有的客戶端IP地址和原來客戶端請求的服務器地址。當我們訪問http://www.xxx.com/index.jsp/時,其實並不是我們瀏覽器真正訪問到了服務器上的 index.jsp文件,而是先由代理服務器去訪問http://192.168.1.110:2046/index.jsp,代理服務器再將訪問到的結果返回給我們的瀏覽器,因爲是代理服務器去訪問index.jsp的,所以index.jsp中通過request.getRemoteAddr()的方法獲取的IP實際上是代理服務器的地址,並不是客戶端的IP地址。

  於是可得出獲得客戶端真實IP地址的方法一:


Java代碼
public String getIpAddr(HttpServletRequest request) {  
      String ip = request.getHeader("x-forwarded-for");  
      if(ip == null || ip.length() == 0) {  
            ip = request.getRemoteAddr();  
        }  
        return ip;  
    } 
public String getIpAddr(HttpServletRequest request) {
      String ip = request.getHeader("x-forwarded-for");
      if(ip == null || ip.length() == 0) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }  可是當我訪問http://www.xxx.com/index.jsp/時,返回的IP地址始終是unknown,也並不是如上所示的 127.0.0.1 或 192.168.1.110了,而我訪問http://192.168.1.110:2046/index.jsp時,則能返回客戶端的真實IP地址,寫了個方法去驗證。


Java代碼
<%@ page import="java.util.*" %>  
<table border=1 cellspacing=0 cellpadding=0 align=center>   
<tr>   
<th>Name</th>   
<th>Value</th>   
</tr>   
<%   
Enumeration enumNames;   
String strName,strValue;   
  
enumNames = request.getHeaderNames();   
while(enumNames.hasMoreElements()){   
    strName = (String)enumNames.nextElement();   
    strValue = request.getHeader(strName);   
    %>   
    <tr>   
    <td><%=strName%></td>   
    <td><%=strValue%></td>   
    </tr>   
    <%   
}   
%>  
<tr>  
</table> 
<%@ page import="java.util.*" %>
<table border=1 cellspacing=0 cellpadding=0 align=center>
<tr>
<th>Name</th>
<th>Value</th>
</tr>
<%
Enumeration enumNames;
String strName,strValue;

enumNames = request.getHeaderNames();
while(enumNames.hasMoreElements()){
    strName = (String)enumNames.nextElement();
    strValue = request.getHeader(strName);
    %>
    <tr>
    <td><%=strName%></td>
    <td><%=strValue%></td>
    </tr>
    <%
}
%>
<tr>
</table>

  出來的結果:X-Forwarded-For: unknown 。X-Forwarded-For確實存在,但其值卻爲unknown,繼續找原因。上網搜了搜,原因出在了Squid上。

squid.conf 的配製文件 forwarded_for 項默認是爲on,如果 forwarded_for 設成了 off  則:

X-Forwarded-For: unknown

一查,發現forwarded_for 項設爲了off,原因找到了,把forwarded_for 項設爲了on,重啓後,訪問http://www.xxx.com/index.jsp/ 獲得的IP是客戶端的真實IP。

  於是可得出獲得客戶端真實IP地址的方法二:

Java代碼
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;  

    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;
    }

  可是,如果通過了多級反向代理的話,X-Forwarded-For的值並不止一個,而是一串Ip值,究竟哪個纔是真正的用戶端的真實IP呢?

  答案是取X-Forwarded-For中第一個非unknown的有效IP字符串。

  如:
  X-Forwarded-For:192.168.1.110, 192.168.1.120, 192.168.1.130, 192.168.1.100
  用戶真實IP爲: 192.168.1.110

本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/YooLive/archive/2008/07/16/2662887.aspx

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章