mirror of https://github.com/elunez/eladmin
使用 mica-ip2region 解析IP地址信息
parent
08f6c58211
commit
7b91c4405c
|
@ -0,0 +1,38 @@
|
|||
/*
|
||||
* Copyright 2019-2020 Zheng Jie
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package me.zhengjie.config;
|
||||
|
||||
import lombok.Data;
|
||||
import me.zhengjie.utils.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author Zheng Jie
|
||||
* @description
|
||||
* @date 2021-11-22
|
||||
**/
|
||||
@Data
|
||||
@Component
|
||||
public class ElAdminProperties {
|
||||
|
||||
public static Boolean ipLocal;
|
||||
|
||||
@Value("${ip.local-parsing}")
|
||||
public void setIpLocal(Boolean ipLocal) {
|
||||
ElAdminProperties.ipLocal = ipLocal;
|
||||
}
|
||||
}
|
|
@ -18,18 +18,13 @@ package me.zhengjie.utils;
|
|||
import cn.hutool.http.HttpUtil;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.zhengjie.config.ElAdminProperties;
|
||||
import net.dreamlu.mica.ip2region.core.Ip2regionSearcher;
|
||||
import net.dreamlu.mica.ip2region.core.IpInfo;
|
||||
import nl.basjes.parse.useragent.UserAgent;
|
||||
import nl.basjes.parse.useragent.UserAgentAnalyzer;
|
||||
import org.lionsoul.ip2region.DataBlock;
|
||||
import org.lionsoul.ip2region.DbConfig;
|
||||
import org.lionsoul.ip2region.DbSearcher;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.File;
|
||||
import java.net.Inet4Address;
|
||||
import java.net.InetAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.UnknownHostException;
|
||||
|
@ -41,42 +36,25 @@ import java.util.Enumeration;
|
|||
* @author Zheng Jie
|
||||
* 字符串工具类, 继承org.apache.commons.lang3.StringUtils类
|
||||
*/
|
||||
@Slf4j
|
||||
public class StringUtils extends org.apache.commons.lang3.StringUtils {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(StringUtils.class);
|
||||
private static boolean ipLocal = false;
|
||||
private static File file = null;
|
||||
private static DbConfig config;
|
||||
private static final char SEPARATOR = '_';
|
||||
private static final String UNKNOWN = "unknown";
|
||||
|
||||
private static final UserAgentAnalyzer userAgentAnalyzer = UserAgentAnalyzer
|
||||
/**
|
||||
* 注入bean
|
||||
*/
|
||||
private final static Ip2regionSearcher IP_SEARCHER = SpringContextHolder.getBean(Ip2regionSearcher.class);
|
||||
|
||||
|
||||
private static final UserAgentAnalyzer USER_AGENT_ANALYZER = UserAgentAnalyzer
|
||||
.newBuilder()
|
||||
.hideMatcherLoadStats()
|
||||
.withCache(10000)
|
||||
.withField(UserAgent.AGENT_NAME_VERSION)
|
||||
.build();
|
||||
|
||||
|
||||
static {
|
||||
SpringContextHolder.addCallBacks(() -> {
|
||||
StringUtils.ipLocal = SpringContextHolder.getProperties("ip.local-parsing", false, Boolean.class);
|
||||
if (ipLocal) {
|
||||
/*
|
||||
* 此文件为独享 ,不必关闭
|
||||
*/
|
||||
String path = "ip2region/ip2region.db";
|
||||
String name = "ip2region.db";
|
||||
try {
|
||||
config = new DbConfig();
|
||||
file = FileUtil.inputStreamToFile(new ClassPathResource(path).getInputStream(), name);
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 驼峰命名法工具
|
||||
*
|
||||
|
@ -196,7 +174,7 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
|
|||
* 根据ip获取详细地址
|
||||
*/
|
||||
public static String getCityInfo(String ip) {
|
||||
if (ipLocal) {
|
||||
if (ElAdminProperties.ipLocal) {
|
||||
return getLocalCityInfo(ip);
|
||||
} else {
|
||||
return getHttpCityInfo(ip);
|
||||
|
@ -216,27 +194,16 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
|
|||
* 根据ip获取详细地址
|
||||
*/
|
||||
public static String getLocalCityInfo(String ip) {
|
||||
try {
|
||||
DbSearcher dbSearcher = new DbSearcher(config, file.getPath());
|
||||
DataBlock dataBlock = dbSearcher.binarySearch(ip);
|
||||
String region = dataBlock.getRegion();
|
||||
String address = region.replace("0|", "");
|
||||
char symbol = '|';
|
||||
if (address.charAt(address.length() - 1) == symbol) {
|
||||
address = address.substring(0, address.length() - 1);
|
||||
}
|
||||
if (dataBlock!=null){
|
||||
dbSearcher.close();
|
||||
}
|
||||
return address.equals(ElAdminConstant.REGION) ? "内网IP" : address;
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
IpInfo ipInfo = IP_SEARCHER.memorySearch(ip);
|
||||
if(ipInfo != null){
|
||||
return ipInfo.getAddress();
|
||||
}
|
||||
return "";
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
public static String getBrowser(HttpServletRequest request) {
|
||||
UserAgent.ImmutableUserAgent userAgent = userAgentAnalyzer.parse(request.getHeader("User-Agent"));
|
||||
UserAgent.ImmutableUserAgent userAgent = USER_AGENT_ANALYZER.parse(request.getHeader("User-Agent"));
|
||||
return userAgent.get(UserAgent.AGENT_NAME_VERSION).getValue();
|
||||
}
|
||||
|
||||
|
|
Binary file not shown.
9
pom.xml
9
pom.xml
|
@ -139,12 +139,15 @@
|
|||
<artifactId>druid-spring-boot-starter</artifactId>
|
||||
<version>${druid.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- ip2region IP库 -->
|
||||
<dependency>
|
||||
<groupId>org.lionsoul</groupId>
|
||||
<artifactId>ip2region</artifactId>
|
||||
<version>1.7.2</version>
|
||||
<groupId>net.dreamlu</groupId>
|
||||
<artifactId>mica-ip2region</artifactId>
|
||||
<version>2.5.6</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!--lombok插件-->
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
|
|
Loading…
Reference in New Issue