Java擷取請求用戶端的真實IP地址

來源:互聯網
上載者:User

標籤:col   print   try   增加   對象   cli   web   indexof   exce   

Java擷取請求用戶端的真實IP地址

Java,擷取用戶端的IP地址的方法:

request.getRemoteAddr()

這種方法在大部分情況下都是有效。但是在通過了Apache,Squid等反向 Proxy軟體就不能擷取到用戶端的真實IP地址了;

如果使用了反向 Proxy軟體,將http://192.168.1.110:2046 的URL反向 Proxy為 http://www.javapeixun.com.cn的URL時,

用 request.getRemoteAddr() 方法擷取的IP地址是:127.0.0.1 或 192.168.1.110,而並不是用戶端的真實IP。

分析:

ISAPI過濾器也會對request對象進行再封裝,附加一些WLS要用的頭資訊。實際的iisforward附加頭如下:

WL-Proxy-Client-IP=211.161.1.239  Proxy-Client-IP=211.161.1.239  X-Forwarded-For=211.161.1.239  WL-Proxy-Client-Keysize=   WL-Proxy-Client-Secretkeysize=   X-WebLogic-Request-ClusterInfo=true  X-WebLogic-KeepAliveSecs=30  X-WebLogic-Force-JVMID=-327089098  WL-Proxy-SSL=false

經過代理以後,由於在用戶端和服務之間增加了中介層,因此伺服器無法直接拿到用戶端的IP,伺服器端應用也無法直接通過轉寄請求的地址返回給客戶

端。但是在轉寄請求的HTTP頭資訊中,增加了X-FORWARDED-FOR資訊。用以跟蹤原有的用戶端IP地址和原來用戶端請求的伺服器位址。當我們訪

問:http://www.javapeixun.com.cn /index.jsp 時,其實並不是我們瀏覽器真正訪問到了伺服器上的index.jsp檔案,

而是先由Proxy 伺服器去訪問  http://192.168.1.110:2046/index.jsp ,Proxy 伺服器再將訪問到的結果返回給我們的瀏覽器,因為是Proxy 伺服器去訪問

index.jsp的,所以index.jsp中通過 request.getRemoteAddr() 的方法擷取的IP實際上是Proxy 伺服器的地址,並不是用戶端的IP地址。

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

public String getRemortIP(HttpServletRequest request) {   if (request.getHeader("x-forwarded-for") == null) {    return request.getRemoteAddr();   }   return request.getHeader("x-forwarded-for");  }

可是當我訪問 http://www.5a520.cn /index.jsp 時,返回的IP地址始終是unknown,也並不是如上所示的127.0.0.1 或 192.168.1.110了,

而我訪問http://192.168.1.110:2046/index.jsp 時,則能返回用戶端的真實IP地址,寫了個方法去驗證。原因出在了Squid上。

squid.conf 的配製檔案 forwarded_for 項預設是為on,如果 forwarded_for 設成了 off  則:X-Forwarded-For: unknown

於是可得出獲得用戶端真實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;    }

可是,如果通過了多級反向 Proxy的話,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

以上方法還不行的話就採用如下方法:

/**      * 擷取當前網路ip      * @param request      * @return      */      public String getIpAddr(HttpServletRequest request){          String ipAddress = request.getHeader("x-forwarded-for");              if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {                  ipAddress = request.getHeader("Proxy-Client-IP");              }              if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {                  ipAddress = request.getHeader("WL-Proxy-Client-IP");              }              if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {                  ipAddress = request.getRemoteAddr();                  if(ipAddress.equals("127.0.0.1") || ipAddress.equals("0:0:0:0:0:0:0:1")){                      //根據網卡取本機配置的IP                      InetAddress inet=null;                      try {                          inet = InetAddress.getLocalHost();                      } catch (UnknownHostException e) {                          e.printStackTrace();                      }                      ipAddress= inet.getHostAddress();                  }              }              //對於通過多個代理的情況,第一個IP為用戶端真實IP,多個IP按照‘,‘分割              if(ipAddress!=null && ipAddress.length()>15){ //"***.***.***.***".length() = 15                  if(ipAddress.indexOf(",")>0){                      ipAddress = ipAddress.substring(0,ipAddress.indexOf(","));                  }              }              return ipAddress;       }

 

Java擷取請求用戶端的真實IP地址

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.