解决定位不准确的问题

This commit is contained in:
zy 2024-03-21 20:56:00 +08:00
parent 7297da3b09
commit cbc30defb9
5 changed files with 136 additions and 44 deletions

View File

@ -34,6 +34,7 @@
<velocity.version>2.3</velocity.version>
<log4j2.version>2.17.1</log4j2.version>
<jwt.version>0.9.1</jwt.version>
<ip2region.version>1.7.2</ip2region.version>
</properties>
<!-- 依赖声明 -->
@ -178,6 +179,13 @@
<version>${kaptcha.version}</version>
</dependency>
<!-- 离线IP地址定位库 -->
<dependency>
<groupId>org.lionsoul</groupId>
<artifactId>ip2region</artifactId>
<version>${ip2region.version}</version>
</dependency>
<!-- 定时任务-->
<dependency>
<groupId>cn.source</groupId>

Binary file not shown.

View File

@ -144,6 +144,12 @@
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- 离线IP地址定位库 -->
<dependency>
<groupId>org.lionsoul</groupId>
<artifactId>ip2region</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,99 @@
package cn.source.common.utils;
import org.apache.commons.io.FileUtils;
import org.lionsoul.ip2region.DataBlock;
import org.lionsoul.ip2region.DbConfig;
import org.lionsoul.ip2region.DbSearcher;
import org.lionsoul.ip2region.Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import java.io.File;
import java.io.InputStream;
import java.lang.reflect.Method;
/**
* 根据ip离线查询地址
*
* @author ruoyi
*/
public class RegionUtil {
private static final Logger log = LoggerFactory.getLogger(RegionUtil.class);
private static final String JAVA_TEMP_DIR = "java.io.tmpdir";
static DbConfig config = null;
static DbSearcher searcher = null;
/** 初始化IP库 */
static {
try {
// 因为jar无法读取文件,复制创建临时文件
String dbPath = RegionUtil.class.getResource("/ip2region/ip2region.db").getPath();
File file = new File(dbPath);
if (!file.exists()) {
String tmpDir = System.getProperties().getProperty(JAVA_TEMP_DIR);
dbPath = tmpDir + "ip2region.db";
file = new File(dbPath);
ClassPathResource cpr =
new ClassPathResource("ip2region" + File.separator + "ip2region.db");
InputStream resourceAsStream = cpr.getInputStream();
if (resourceAsStream != null) {
FileUtils.copyInputStreamToFile(resourceAsStream, file);
}
}
config = new DbConfig();
searcher = new DbSearcher(config, dbPath);
log.info("bean [{}]", config);
log.info("bean [{}]", searcher);
} catch (Exception e) {
log.error("init ip region error:{}", e);
}
}
/**
* 解析IP
*
* @param ip
* @return
*/
public static String getRegion(String ip) {
try {
// db
if (searcher == null || StringUtils.isEmpty(ip)) {
log.error("DbSearcher is null");
return StringUtils.EMPTY;
}
long startTime = System.currentTimeMillis();
// 查询算法
int algorithm = DbSearcher.MEMORY_ALGORITYM;
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;
}
DataBlock dataBlock = null;
if (Util.isIpAddress(ip) == false) {
log.warn("warning: Invalid ip address");
}
dataBlock = (DataBlock) method.invoke(searcher, ip);
String result = dataBlock.getRegion();
long endTime = System.currentTimeMillis();
log.debug("region use time[{}] result[{}]", endTime - startTime, result);
return result;
} catch (Exception e) {
log.error("error:{}", e);
}
return StringUtils.EMPTY;
}
}

View File

@ -1,11 +1,8 @@
package cn.source.common.utils.ip;
import cn.source.common.config.RuoYiConfig;
import cn.source.common.constant.Constants;
import cn.source.common.utils.RegionUtil;
import cn.source.common.utils.StringUtils;
import cn.source.common.utils.http.HttpUtils;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -14,67 +11,49 @@ import org.slf4j.LoggerFactory;
*
* @author ruoyi
*/
public class AddressUtils
{
public class AddressUtils {
private static final Logger log = LoggerFactory.getLogger(AddressUtils.class);
// IP地址查询
public static final String IP_URL = "http://whois.pconline.com.cn/ipJson.jsp";
// 未知地址
public static final String UNKNOWN = "XX XX";
public static String getRealAddressByIP(String ip)
{
public static String getRealAddressByIP(String ip) {
String address = UNKNOWN;
// 内网不查询
if (IpUtils.internalIp(ip))
{
if (IpUtils.internalIp(ip)) {
return "内网IP";
}
if (RuoYiConfig.isAddressEnabled())
{
try
{
String rspStr = HttpUtils.sendGet(IP_URL, "ip=" + ip + "&json=true", Constants.GBK);
if (StringUtils.isEmpty(rspStr))
{
if (RuoYiConfig.isAddressEnabled()) {
try {
String rspStr = RegionUtil.getRegion(ip);
if (StringUtils.isEmpty(rspStr)) {
log.error("获取地理位置异常 {}", ip);
return UNKNOWN;
}
JSONObject obj = JSON.parseObject(rspStr);
String region = obj.getString("pro");
String city = obj.getString("city");
String[] obj = rspStr.split("\\|");
String region = obj[2];
String city = obj[3];
return String.format("%s %s", region, city);
}
catch (Exception e)
{
log.error("获取地理位置异常 {}", ip);
} catch (Exception e) {
log.error("获取地理位置异常 {}", e);
}
}
return address;
}
public static String getRealCityByIP(String ip)
{
public static String getRealCityByIP(String ip) {
String city = UNKNOWN;
// 内网不查询
if (RuoYiConfig.isAddressEnabled())
{
try
{
String rspStr = HttpUtils.sendGet(IP_URL, "ip=" + ip + "&json=true", Constants.GBK);
if (StringUtils.isEmpty(rspStr))
{
log.error("获取城市位置异常 {}", ip);
if (RuoYiConfig.isAddressEnabled()) {
try {
String rspStr = RegionUtil.getRegion(ip);
if (StringUtils.isEmpty(rspStr)) {
log.error("获取地理位置异常 {}", ip);
return UNKNOWN;
}
JSONObject obj = JSON.parseObject(rspStr);
String region = obj.getString("pro");
city = obj.getString("city");
}
catch (Exception e)
{
String[] obj = rspStr.split("\\|");
String region = obj[2] + "";
city = obj[3] + "";
} catch (Exception e) {
log.error("获取城市位置异常 {}", ip);
}
}