Skip to content

Commit 7b91c44

Browse files
committed
使用 mica-ip2region 解析IP地址信息
1 parent 08f6c58 commit 7b91c44

File tree

4 files changed

+63
-55
lines changed

4 files changed

+63
-55
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2019-2020 Zheng Jie
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package me.zhengjie.config;
17+
18+
import lombok.Data;
19+
import me.zhengjie.utils.StringUtils;
20+
import org.springframework.beans.factory.annotation.Value;
21+
import org.springframework.stereotype.Component;
22+
23+
/**
24+
* @author Zheng Jie
25+
* @description
26+
* @date 2021-11-22
27+
**/
28+
@Data
29+
@Component
30+
public class ElAdminProperties {
31+
32+
public static Boolean ipLocal;
33+
34+
@Value("${ip.local-parsing}")
35+
public void setIpLocal(Boolean ipLocal) {
36+
ElAdminProperties.ipLocal = ipLocal;
37+
}
38+
}

eladmin-common/src/main/java/me/zhengjie/utils/StringUtils.java

+19-52
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,13 @@
1818
import cn.hutool.http.HttpUtil;
1919
import cn.hutool.json.JSONObject;
2020
import cn.hutool.json.JSONUtil;
21+
import lombok.extern.slf4j.Slf4j;
22+
import me.zhengjie.config.ElAdminProperties;
23+
import net.dreamlu.mica.ip2region.core.Ip2regionSearcher;
24+
import net.dreamlu.mica.ip2region.core.IpInfo;
2125
import nl.basjes.parse.useragent.UserAgent;
2226
import nl.basjes.parse.useragent.UserAgentAnalyzer;
23-
import org.lionsoul.ip2region.DataBlock;
24-
import org.lionsoul.ip2region.DbConfig;
25-
import org.lionsoul.ip2region.DbSearcher;
26-
import org.slf4j.Logger;
27-
import org.slf4j.LoggerFactory;
28-
import org.springframework.core.io.ClassPathResource;
29-
3027
import javax.servlet.http.HttpServletRequest;
31-
import java.io.File;
32-
import java.net.Inet4Address;
3328
import java.net.InetAddress;
3429
import java.net.NetworkInterface;
3530
import java.net.UnknownHostException;
@@ -41,42 +36,25 @@
4136
* @author Zheng Jie
4237
* 字符串工具类, 继承org.apache.commons.lang3.StringUtils类
4338
*/
39+
@Slf4j
4440
public class StringUtils extends org.apache.commons.lang3.StringUtils {
4541

46-
private static final Logger log = LoggerFactory.getLogger(StringUtils.class);
47-
private static boolean ipLocal = false;
48-
private static File file = null;
49-
private static DbConfig config;
5042
private static final char SEPARATOR = '_';
5143
private static final String UNKNOWN = "unknown";
5244

53-
private static final UserAgentAnalyzer userAgentAnalyzer = UserAgentAnalyzer
45+
/**
46+
* 注入bean
47+
*/
48+
private final static Ip2regionSearcher IP_SEARCHER = SpringContextHolder.getBean(Ip2regionSearcher.class);
49+
50+
51+
private static final UserAgentAnalyzer USER_AGENT_ANALYZER = UserAgentAnalyzer
5452
.newBuilder()
5553
.hideMatcherLoadStats()
5654
.withCache(10000)
5755
.withField(UserAgent.AGENT_NAME_VERSION)
5856
.build();
5957

60-
61-
static {
62-
SpringContextHolder.addCallBacks(() -> {
63-
StringUtils.ipLocal = SpringContextHolder.getProperties("ip.local-parsing", false, Boolean.class);
64-
if (ipLocal) {
65-
/*
66-
* 此文件为独享 ,不必关闭
67-
*/
68-
String path = "ip2region/ip2region.db";
69-
String name = "ip2region.db";
70-
try {
71-
config = new DbConfig();
72-
file = FileUtil.inputStreamToFile(new ClassPathResource(path).getInputStream(), name);
73-
} catch (Exception e) {
74-
log.error(e.getMessage(), e);
75-
}
76-
}
77-
});
78-
}
79-
8058
/**
8159
* 驼峰命名法工具
8260
*
@@ -196,7 +174,7 @@ public static String getIp(HttpServletRequest request) {
196174
* 根据ip获取详细地址
197175
*/
198176
public static String getCityInfo(String ip) {
199-
if (ipLocal) {
177+
if (ElAdminProperties.ipLocal) {
200178
return getLocalCityInfo(ip);
201179
} else {
202180
return getHttpCityInfo(ip);
@@ -216,27 +194,16 @@ public static String getHttpCityInfo(String ip) {
216194
* 根据ip获取详细地址
217195
*/
218196
public static String getLocalCityInfo(String ip) {
219-
try {
220-
DbSearcher dbSearcher = new DbSearcher(config, file.getPath());
221-
DataBlock dataBlock = dbSearcher.binarySearch(ip);
222-
String region = dataBlock.getRegion();
223-
String address = region.replace("0|", "");
224-
char symbol = '|';
225-
if (address.charAt(address.length() - 1) == symbol) {
226-
address = address.substring(0, address.length() - 1);
227-
}
228-
if (dataBlock!=null){
229-
dbSearcher.close();
230-
}
231-
return address.equals(ElAdminConstant.REGION) ? "内网IP" : address;
232-
} catch (Exception e) {
233-
log.error(e.getMessage(), e);
197+
IpInfo ipInfo = IP_SEARCHER.memorySearch(ip);
198+
if(ipInfo != null){
199+
return ipInfo.getAddress();
234200
}
235-
return "";
201+
return null;
202+
236203
}
237204

238205
public static String getBrowser(HttpServletRequest request) {
239-
UserAgent.ImmutableUserAgent userAgent = userAgentAnalyzer.parse(request.getHeader("User-Agent"));
206+
UserAgent.ImmutableUserAgent userAgent = USER_AGENT_ANALYZER.parse(request.getHeader("User-Agent"));
240207
return userAgent.get(UserAgent.AGENT_NAME_VERSION).getValue();
241208
}
242209

Binary file not shown.

pom.xml

+6-3
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,15 @@
139139
<artifactId>druid-spring-boot-starter</artifactId>
140140
<version>${druid.version}</version>
141141
</dependency>
142+
143+
<!-- ip2region IP库 -->
142144
<dependency>
143-
<groupId>org.lionsoul</groupId>
144-
<artifactId>ip2region</artifactId>
145-
<version>1.7.2</version>
145+
<groupId>net.dreamlu</groupId>
146+
<artifactId>mica-ip2region</artifactId>
147+
<version>2.5.6</version>
146148
</dependency>
147149

150+
148151
<!--lombok插件-->
149152
<dependency>
150153
<groupId>org.projectlombok</groupId>

0 commit comments

Comments
 (0)