Java獲取用戶IP屬地模擬抖音詳解
介紹
細心的小伙伴可能會發(fā)現(xiàn),抖音新上線了IP屬地的功能,小伙伴在發(fā)表動態(tài)、發(fā)表評論以及聊天的時候,都會顯示自己的IP屬地信息

下面,我就來講講,Java中是如何獲取IP屬地的,主要分為以下幾步
- 通過 HttpServletRequest 對象,獲取用戶的IP地址
- 通過 IP 地址,獲取對應(yīng)的省份、城市
首先需要寫一個IP獲取的工具類,因為每一次用戶的Request請求,都會攜帶上請求的IP地址放到請求頭中。
public class IpUtil {
public static String getIpAddr(ServerHttpRequest request) {
HttpHeaders headers = request.getHeaders();
String ipAddress = headers.getFirst("X-Forwarded-For");
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = headers.getFirst("Proxy-Client-IP");
}
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = headers.getFirst("WL-Proxy-Client-IP");
}
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getRemoteAddress().getAddress().getHostAddress();
if (ipAddress.equals("127.0.0.1") || ipAddress.equals("0:0:0:0:0:0:0:1")) {
// 根據(jù)網(wǎng)卡取本機配置的IP
try {
InetAddress inet = InetAddress.getLocalHost();
ipAddress = inet.getHostAddress();
} catch (UnknownHostException e) {
log.error("根據(jù)網(wǎng)卡獲取本機配置的IP異常", e);
}
}
}
// 對于通過多個代理的情況,第一個IP為客戶端真實IP,多個IP按照','分割
if (ipAddress != null && ipAddress.indexOf(",") > 0) {
ipAddress = ipAddress.split(",")[0];
}
return ipAddress;
}
}這里有三個名詞,分別是
- X-Forwarded-For:一個 HTTP擴展頭部,主要是為了讓W(xué)eb服務(wù)器獲取訪問用戶的真實IP地址。每個IP地址,每個值通過逗號+空格分開,最左邊是最原始客戶端的IP地址,中間如果有多層代理,每?層代理會將連接它的客戶端IP追加在X-Forwarded-For右邊。
- X-Real-IP:一般只記錄真實發(fā)出請求的客戶端IP
- Proxy-Client-IP:這個一般是經(jīng)過Apache http服務(wù)器的請求才會有,用Apache http做代理時一般會加上Proxy-Client-IP請求頭
- WL-Proxy-Client-IP:也是通過 Apache http 服務(wù)器,在weblogic插件加上的頭。
在我們獲取到用戶的IP地址后,那么就可以獲取對應(yīng)的ip信息了
我在Github沖浪的時候,發(fā)現(xiàn)了Ip2region項目。
一個準(zhǔn)確率99.9%的離線IP地址定位庫,0.0x毫秒級查詢,ip2region.db數(shù)據(jù)庫只有數(shù)MB,提供了 java,php,c,python,nodejs,golang,c# 等查詢綁定和Binary,B樹,內(nèi)存三種查詢算法。

數(shù)據(jù)聚合了一些知名ip到地名查詢提供商的數(shù)據(jù),這些是他們官方的的準(zhǔn)確率,經(jīng)測試著實比經(jīng)典的純真IP定位準(zhǔn)確一些。ip2region的數(shù)據(jù)聚合自以下服務(wù)商的開放API或者數(shù)據(jù)。
- 80%, 淘寶IP地址庫, http://ip.taobao.com/
- ≈10%, GeoIP, https://geoip.com/
- ≈2%, 純真IP庫, http://www.cz88.net/
備注:如果上述開放API或者數(shù)據(jù)都不給開放數(shù)據(jù)時ip2region將停止數(shù)據(jù)的更新服務(wù)。
每條ip數(shù)據(jù)段都固定了格式:
_城市Id|國家|區(qū)域|省份|城市|ISP_
只有中國的數(shù)據(jù)精確到了城市,其他國家有部分?jǐn)?shù)據(jù)只能定位到國家,后前的選項全部是0,已經(jīng)包含了全部你能查到的大大小小的國家
生成的數(shù)據(jù)庫文件ip2region.db只有幾MB,最小的版本只有1.5MB,隨著數(shù)據(jù)的詳細度增加數(shù)據(jù)庫的大小也慢慢增大,目前還沒超過8MB。
內(nèi)置的三種查詢算法
全部的查詢客戶端單次查詢都在0.x毫秒級別,內(nèi)置了三種查詢算法
- memory算法:整個數(shù)據(jù)庫全部載入內(nèi)存,單次查詢都在0.1x毫秒內(nèi),C語言的客戶端單次查詢在0.00x毫秒級別。
- binary算法:基于二分查找,基于ip2region.db文件,不需要載入內(nèi)存,單次查詢在0.x毫秒級別。
- b-tree算法:基于btree算法,基于ip2region.db文件,不需要載入內(nèi)存,單詞查詢在0.x毫秒級別,比binary算法更快。
ip2region安裝
下面,就讓我們給項目引入ip2region,進行ip信息轉(zhuǎn)換吧
首先引入maven依賴
<dependency>
<groupId>org.lionsoul</groupId>
<artifactId>ip2region</artifactId>
<version>1.7.2</version>
</dependency>然后編寫一個工具類IpUtils,首先需要加載ip2region.db文件
static {
dbPath = createFtlFileByFtlArray() + "ip2region.db";
try {
config = new DbConfig();
} catch (DbMakerConfigException e) {
e.printStackTrace();
}
try {
searcher = new DbSearcher(config, dbPath);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}在加載的時候,需要下載倉庫中的ip2region.db文件,然后放到resource目錄下

然后,通過內(nèi)置的三種算法,分別轉(zhuǎn)換用戶ip地址
public static String getCityInfo(String ip) {
if (StringUtils.isEmpty(dbPath)) {
log.error("Error: Invalid ip2region.db file");
return null;
}
if(config == null || searcher == null){
log.error("Error: DbSearcher or DbConfig is null");
return null;
}
//查詢算法
//B-tree, B樹搜索(更快)
int algorithm = DbSearcher.BTREE_ALGORITHM;
//Binary,使用二分搜索
//DbSearcher.BINARY_ALGORITHM
//Memory,加載內(nèi)存(最快)
//DbSearcher.MEMORY_ALGORITYM
try {
// 使用靜態(tài)代碼塊,減少文件讀取操作
// DbConfig config = new DbConfig();
// DbSearcher searcher = new DbSearcher(config, dbPath);
//define the method
Method method = null;
switch (algorithm) {
case DbSearcher.BTREE_ALGORITHM:
method = searcher.getClass().getMethod("btreeSearch", String.class);
break;
case DbSearcher.BINARY_ALGORITHM:
method = searcher.getClass().getMethod("binarySearch", String.class);
break;
case DbSearcher.MEMORY_ALGORITYM:
method = searcher.getClass().getMethod("memorySearch", String.class);
break;
default:
}
DataBlock dataBlock = null;
if (Util.isIpAddress(ip) == false) {
System.out.println("Error: Invalid ip address");
}
dataBlock = (DataBlock) method.invoke(searcher, ip);
String ipInfo = dataBlock.getRegion();
if (!StringUtils.isEmpty(ipInfo)) {
ipInfo = ipInfo.replace("|0", "");
ipInfo = ipInfo.replace("0|", "");
}
return ipInfo;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}下面,我們編寫main函數(shù)進行測試,發(fā)現(xiàn)可以正常的解析出ip信息

由于 ip 屬地在國內(nèi)的話,只會展示省份,而國外的話,只會展示國家。所以我們還需要對這個方法進行一下封裝,得到獲取 IP 屬地的信息。
/**
* 獲取IP屬地
* @param ip
* @return
*/
public static String getIpPossession(String ip) {
String cityInfo = getCityInfo(ip);
if (!StringUtils.isEmpty(cityInfo)) {
cityInfo = cityInfo.replace("|", " ");
String[] cityList = cityInfo.split(" ");
if (cityList.length > 0) {
// 國內(nèi)的顯示到具體的省
if ("中國".equals(cityList[0])) {
if (cityList.length > 1) {
return cityList[1];
}
}
// 國外顯示到國家
return cityList[0];
}
}
return "未知";
}下面,我們在找一個 國外的IP測試一下效果??梢钥吹揭呀?jīng)能夠正常的顯示IP屬地信息了~

到這里如果獲取用戶的 IP 屬地已經(jīng)完成啦,如果想要了解關(guān)于更多ip2region的功能,歡迎訪問其Github地址進行學(xué)習(xí)。
項目地址
https://github.com/lionsoul2014/ip2region
到此這篇關(guān)于Java獲取用戶IP屬地模擬抖音詳解的文章就介紹到這了,更多相關(guān)Java IP屬地內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot集成nacos動態(tài)刷新數(shù)據(jù)源的實現(xiàn)示例
這篇文章主要介紹了SpringBoot集成nacos動態(tài)刷新數(shù)據(jù)源的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
為了多次讀取ServletInputStream引發(fā)的一系列問題
這篇文章主要介紹了為了多次讀取ServletInputStream引發(fā)的一系列問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10

