springboot 獲取訪問接口的請求的IP地址的實(shí)現(xiàn)
工具類:
import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* @Author : JCccc
* @CreateTime : 2018-11-23
* @Description :
* @Point: Keep a good mood
**/
public class IpUtil {
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")) {
// 根據(jù)網(wǎng)卡取本機(jī)配置的IP
InetAddress inet = null;
try {
inet = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}
ipAddress = inet.getHostAddress();
}
}
// 對于通過多個(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="";
}
// ipAddress = this.getRequest().getRemoteAddr();
return ipAddress;
}
}
方法調(diào)用:
(當(dāng)接口 /test 被調(diào)用,request就能自動(dòng)獲取出來,然后調(diào)用工具類方法進(jìn)行解析獲取了。)
@RequestMapping(value = "/test", method = RequestMethod.GET)
public String test(HttpServletRequest request){
//獲取IP地址
String ipAddress =IpUtil.getIpAddr(request);
return ipAddress;
}
到此這篇關(guān)于springboot 獲取訪問接口的請求的IP地址的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)springboot 獲取接口IP地址內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- springboot實(shí)現(xiàn)獲取客戶端IP地址的示例代碼
- SpringBoot項(xiàng)目中使用OkHttp獲取IP地址的示例代碼
- SpringBoot項(xiàng)目中獲取IP地址的實(shí)現(xiàn)示例
- springboot如何獲取請求者的ip地址
- SpringBoot如何獲取客戶端的IP地址
- SpringBoot實(shí)現(xiàn)IP地址解析的示例代碼
- SpringBoot獲取客戶端的IP地址的實(shí)現(xiàn)示例
- SpringBoot整合Ip2region獲取IP地址和定位的詳細(xì)過程
- springboot獲取真實(shí)ip地址的方法實(shí)例
- springboot獲取訪問的ip地址的實(shí)現(xiàn)步驟
相關(guān)文章
Java實(shí)現(xiàn)微信支付的項(xiàng)目實(shí)踐
最近的一個(gè)項(xiàng)目中涉及到了支付業(yè)務(wù),其中用到了微信支付和支付寶支付,本文就來介紹一下Java實(shí)現(xiàn)微信支付的項(xiàng)目實(shí)踐,具有一定的參考價(jià)值,感興趣的可以了解一下2023-10-10
Spring中@ExceptionHandler注解的工作原理詳解
這篇文章主要介紹了Spring中@ExceptionHandler注解的工作原理詳解,Spring Web注解@ExceptionHandler可以用來指定處理某類異常的控制器方法,從而在這些異常發(fā)生時(shí),會(huì)有相應(yīng)的控制器方法來處理此類異常,需要的朋友可以參考下2024-01-01
5個(gè)JAVA入門必看的經(jīng)典實(shí)例
這篇文章主要為大家詳細(xì)介紹了5個(gè)JAVA入門必看的經(jīng)典實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
java實(shí)現(xiàn)簡單學(xué)生管理系統(tǒng)項(xiàng)目
這篇文章主要介紹了java實(shí)現(xiàn)簡單學(xué)生管理系統(tǒng)項(xiàng)目,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07
關(guān)于Java中阻塞隊(duì)列BlockingQueue的詳解
這篇文章主要介紹了關(guān)于Java中阻塞隊(duì)列BlockingQueue的詳解,BlockingQueue是為了解決多線程中數(shù)據(jù)高效安全傳輸而提出的,從阻塞這個(gè)詞可以看出,在某些情況下對阻塞隊(duì)列的訪問可能會(huì)造成阻塞,需要的朋友可以參考下2023-05-05

