springboot獲取訪問的ip地址的實(shí)現(xiàn)步驟
1、用shiro的方式獲取到的是127.0.0.1,或者用request.getRemoteAddr()也會(huì)是127.0.0.1(這個(gè)與瀏覽器地址有關(guān))
HttpServletRequest request; request.getRemoteAddr();
shiro
/**
* 獲取ip
*
* @return
*/
private String getIp() {
Subject subject = SecurityUtils.getSubject();
String host = subject.getSession().getHost();
return host;
}
發(fā)現(xiàn)全部是127.0.0.1
2、網(wǎng)上有個(gè)兄弟寫了一個(gè)方法,測試以后發(fā)現(xiàn)是可以獲取到ip的,在本機(jī)上測試成功,地址欄上面輸入Ip:port的形式訪問。
public static String getIpAddr(HttpServletRequest request) {
String ipAddress = null;
try {
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")) {
InetAddress inet = null;
try {
inet = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}
ipAddress = inet.getHostAddress();
}
}
// 對(duì)于通過多個(gè)代理的情況,第一個(gè)IP為客戶端真實(shí)IP,多個(gè)IP按照','分割
if (ipAddress != null && ipAddress.length() > 15) { // "***.***.***.***".length()
// = 15
if (ipAddress.indexOf(",") > 0) {
ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
}
}
} catch (Exception e) {
ipAddress = "";
}
return ipAddress;
}
不是在controller調(diào)用也是可以的,也就是沒有HttpServletRequest request參數(shù)情況下:
HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
System.out.println("ip:"+IpUtil.getIpAddr(request));這樣就有ip地址了。
到此這篇關(guān)于springboot獲取訪問的ip地址的實(shí)現(xiàn)步驟的文章就介紹到這了,更多相關(guān)springboot獲取訪問ip地址內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- springboot實(shí)現(xiàn)獲取客戶端IP地址的示例代碼
- SpringBoot項(xiàng)目中使用OkHttp獲取IP地址的示例代碼
- SpringBoot項(xiàng)目中獲取IP地址的實(shí)現(xiàn)示例
- springboot如何獲取請(qǐng)求者的ip地址
- SpringBoot如何獲取客戶端的IP地址
- SpringBoot實(shí)現(xiàn)IP地址解析的示例代碼
- SpringBoot獲取客戶端的IP地址的實(shí)現(xiàn)示例
- SpringBoot整合Ip2region獲取IP地址和定位的詳細(xì)過程
- springboot獲取真實(shí)ip地址的方法實(shí)例
- springboot 獲取訪問接口的請(qǐng)求的IP地址的實(shí)現(xiàn)
相關(guān)文章
JAVA中ArrayList和數(shù)組的轉(zhuǎn)換與遇到的問題解決
做研發(fā)的朋友都知道,在項(xiàng)目開發(fā)中經(jīng)常會(huì)碰到ArrayList與數(shù)組類型之間的相互轉(zhuǎn)換,這篇文章主要給大家介紹了關(guān)于JAVA中ArrayList和數(shù)組的轉(zhuǎn)換與遇到的問題解決,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05
SpringCloud OpenFeign遠(yuǎn)程調(diào)用傳遞請(qǐng)求頭信息方式
這篇文章主要介紹了SpringCloud OpenFeign遠(yuǎn)程調(diào)用傳遞請(qǐng)求頭信息方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2026-03-03
Java調(diào)用C++程序的實(shí)現(xiàn)方式
這篇文章主要介紹了Java調(diào)用C++程序的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
SpringBoot項(xiàng)目中讀取resource目錄下的文件六種方法
這篇文章給大家總結(jié)了SpringBoot項(xiàng)目中讀取resource目錄下的文件六種方法,文中有詳細(xì)的代碼示例供大家參考,具有一定的參考價(jià)值,需要的朋友可以參考下2024-05-05
Jmeter壓力測試簡單教程(包括服務(wù)器狀態(tài)監(jiān)控)
Jmeter是一個(gè)非常好用的壓力測試工具。Jmeter用來做輕量級(jí)的壓力測試,非常合適,本文詳細(xì)的介紹了Jmeter的使用,感性的可以了解一下2021-11-11
SpringBoot3 整合Docker-Compose的實(shí)現(xiàn)步驟
本文主要介紹了SpringBoot3 整合Docker-Compose的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-08-08
使用spring攔截器實(shí)現(xiàn)日志管理實(shí)例
本篇文章主要介紹了使用spring攔截器實(shí)現(xiàn)日志管理實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03

