當前位置:首頁 » 編程語言 » java獲取請求ip

java獲取請求ip

發布時間: 2025-03-14 18:09:05

java中怎麼獲取客戶端的真實的IP-CSDN論壇

在JSP里,獲取客戶端的IP地址的方法是:request.getRemoteAddr(),這種方法在大部分情況下都是有效的。但是在通過了Apache,Squid等反向代理軟體就不能獲取到客戶端的真實IP地址了。
如果使用了反向代理軟體,將http://192.168.1.110:2046/ 的URL反向代理為 http://www.bt285.cn / 的URL時,用request.getRemoteAddr()方法獲取的IP地址是:127.0.0.1或192.168.1.110,而並不是客戶端的真實IP。

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

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

Java code?

1
2
3
4
5
6

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地址的方法二:

Java code?

1
2
3
4
5
6
7
8
9
10
11
12
13

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
更多Java精彩課程請點擊:http://e.51cto.com/index.php?do=search&m=course&q=Java

㈡ JavaWeb如何獲取當前訪問IP

背景

在進行JavaWeb開發時,通過會記錄日誌,在記錄日誌的時候,就會存在客戶端的ip存儲,那麼如何獲取客戶端對應的ip呢?

知識點

客戶端的訪問ip,是可以通過Request對象來獲取,代碼如下,也是我在項目中經常使用到的工具類。

publicclassIPUtils{/***獲取IP地址*@paramrequest*@return*/(HttpServletRequestrequest){Stringip=request.getHeader("x-forwarded-for");System.out.println("x-forwarded-forip:"+ip);if(ip!=null&&ip.length()!=0&&!"unknown".equalsIgnoreCase(ip)){//多次反向代理後會有多個ip值,第一個ip才是真實ipif(ip.indexOf(",")!=-1){ip=ip.split(",")[0];}}if(ip==null||ip.length()==0||"unknown".equalsIgnoreCase(ip)){ip=request.getHeader("Proxy-Client-IP");System.out.println("Proxy-Client-IPip:"+ip);}if(ip==null||ip.length()==0||"unknown".equalsIgnoreCase(ip)){ip=request.getHeader("WL-Proxy-Client-IP");System.out.println("WL-Proxy-Client-IPip:"+ip);}if(ip==null||ip.length()==0||"unknown".equalsIgnoreCase(ip)){ip=request.getHeader("HTTP_CLIENT_IP");System.out.println("HTTP_CLIENT_IPip:"+ip);}if(ip==null||ip.length()==0||"unknown".equalsIgnoreCase(ip)){ip=request.getHeader("HTTP_X_FORWARDED_FOR");System.out.println("HTTP_X_FORWARDED_FORip:"+ip);}if(ip==null||ip.length()==0||"unknown".equalsIgnoreCase(ip)){ip=request.getHeader("X-Real-IP");System.out.println("X-Real-IPip:"+ip);}if(ip==null||ip.length()==0||"unknown".equalsIgnoreCase(ip)){ip=request.getRemoteAddr();System.out.println("getRemoteAddrip:"+ip);}System.out.println("獲取客戶端ip:"+ip);returnip;}}

可以通過調用此工具類來獲取到當前客戶端訪問的IP地址了。

特殊情況

其實這裡面也存在一些特殊的情況,客戶端可能通過代理的方式進行調用後端代碼,所以這里的ip其實可能並不是真實的地址。

還有就是如果通過轉發的話,ip的獲取也可能會有所差異。

所以大家在使用獲取ip的情況下,還是不要太過依賴於他。

㈢ java怎麼獲取請求的ip

java獲取外網ip地址方法:
public class Main {

public static void main(String[] args) throws SocketException {
System.out.println(Main.getRealIp());
}

public static String getRealIp() throws SocketException {
String localip = null;// 本地IP,如果沒有配置外網IP則返回它
String netip = null;// 外網IP

Enumeration<NetworkInterface> netInterfaces =
NetworkInterface.getNetworkInterfaces();
InetAddress ip = null;
boolean finded = false;// 是否找到外網IP
while (netInterfaces.hasMoreElements() && !finded) {
NetworkInterface ni = netInterfaces.nextElement();
Enumeration<InetAddress> address = ni.getInetAddresses();
while (address.hasMoreElements()) {
ip = address.nextElement();
if (!ip.isSiteLocalAddress()
&& !ip.isLoopbackAddress()
&& ip.getHostAddress().indexOf(":") == -1) {// 外網IP
netip = ip.getHostAddress();
finded = true;
break;
} else if (ip.isSiteLocalAddress()
&& !ip.isLoopbackAddress()
&& ip.getHostAddress().indexOf(":") == -1) {// 內網IP
localip = ip.getHostAddress();
}
}
}

if (netip != null && !"".equals(netip)) {
return netip;
} else {
return localip;
}
}
}

熱點內容
我的世界斗羅伺服器電腦網易版 發布:2025-03-14 21:28:33 瀏覽:524
java是甲骨文 發布:2025-03-14 21:21:38 瀏覽:126
柱頂要加密 發布:2025-03-14 21:16:11 瀏覽:852
魔聲藍牙耳機怎麼在安卓顯示電量 發布:2025-03-14 21:15:32 瀏覽:618
智慧易店伺服器地址是啥 發布:2025-03-14 20:57:49 瀏覽:887
小米ID密碼忘記了有什麼危害 發布:2025-03-14 20:45:28 瀏覽:610
大麥路由器怎麼改密碼 發布:2025-03-14 20:35:42 瀏覽:87
資料庫片語 發布:2025-03-14 20:27:21 瀏覽:249
角色卡演算法 發布:2025-03-14 20:08:48 瀏覽:650
linux伺服器安全加固 發布:2025-03-14 19:59:21 瀏覽:779