ip库引入,查询工具类

This commit is contained in:
dark 2022-12-11 22:20:43 +08:00
parent 7f131a3853
commit 0b5aa560b3
4 changed files with 160 additions and 0 deletions

View File

@ -20,12 +20,34 @@
</description>
<url>https://github.com/YunaiV/ruoyi-vue-pro</url>
<properties>
<ip2region.version>2.6.6</ip2region.version>
</properties>
<dependencies>
<dependency>
<groupId>cn.iocoder.boot</groupId>
<artifactId>yudao-common</artifactId>
</dependency>
<!-- IP地址检索 -->
<dependency>
<groupId>org.lionsoul</groupId>
<artifactId>ip2region</artifactId>
<version>${ip2region.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<scope>provided</scope> <!-- 设置为 provided只有工具类需要使用到 -->
</dependency>
<!-- Test 测试相关 -->
<dependency>
<groupId>cn.iocoder.boot</groupId>

View File

@ -1,11 +1,94 @@
package cn.iocoder.yudao.framework.ip.core.utils;
import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.framework.ip.core.Area;
import lombok.extern.slf4j.Slf4j;
import org.lionsoul.ip2region.xdb.Searcher;
import java.io.IOException;
import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR;
/**
* IP 工具类
* <p>
* 依赖于ip2region.xdb精简版来源于<a href="https://gitee.com/zhijiantianya/ip2region"/>
* region精简为城市编码
*
* @author 芋道源码
*/
@Slf4j
public class IPUtils {
/**
* 根据ip搜索
* 启动加载到内存中
*/
private static Searcher SEARCHER;
/**
* 初始化SEARCHER
*/
private final static IPUtils INSTANCE = new IPUtils();
/**
* 私有化构造
*/
private IPUtils() {
String dbPath = "ip2region.xdb";
dbPath = IPUtils.class.getClassLoader().getResource(dbPath).getPath();
try {
SEARCHER = Searcher.newWithBuffer(Searcher.loadContentFromFile(dbPath));
} catch (IOException e) {
// 加载xdb文件异常,不影响启动
log.error("启动加载IP SEARCH异常", e);
SEARCHER = null;
}
}
/**
* 查询IP对应的地区ID格式应为127.0.0.1
* @param ip ip地址
* @return 地区id
*/
public static Integer getAreaId(String ip) {
try {
return Integer.parseInt(SEARCHER.search(ip));
} catch (Exception e) {
throw new ServiceException(INTERNAL_SERVER_ERROR);
}
}
/**
* 查询IP对应的地区ID格式参考{@link Searcher#checkIP(String)} 的返回
* @param ip ip地址
* @return 地区id
*/
public static Integer getAreaId(long ip) {
try {
return Integer.parseInt(SEARCHER.search(ip));
} catch (Exception e) {
throw new ServiceException(INTERNAL_SERVER_ERROR);
}
}
/**
* 查询IP对应的地区格式应为127.0.0.1
* @param ip ip地址
* @return 地区
*/
public static Area getArea(String ip) {
return AreaUtils.getArea(getAreaId(ip));
}
/**
* 查询IP对应的地区格式参考{@link Searcher#checkIP(String)} 的返回
* @param ip ip地址
* @return 地区
*/
public static Area getArea(long ip) {
return AreaUtils.getArea(getAreaId(ip));
}
}

View File

@ -0,0 +1,55 @@
package cn.iocoder.yudao.framework.ip.core.utils;
import cn.iocoder.yudao.framework.ip.core.Area;
import org.junit.jupiter.api.Test;
import org.lionsoul.ip2region.xdb.Searcher;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* {@link IPUtils} 的单元测试
*/
class IPUtilsTest {
@Test
void getAreaId() {
// 120.202.4.0|120.202.4.255|420600
Integer areaId = IPUtils.getAreaId("120.202.4.50");
assertEquals(420600, areaId);
}
@Test
void testGetAreaId() {
// 120.203.123.0|120.203.133.255|360900
long ip = 0L;
try {
ip = Searcher.checkIP("120.203.123.250");
} catch (Exception e) {
// ignore
}
Integer areaId = IPUtils.getAreaId(ip);
assertEquals(360900, areaId);
}
@Test
void getArea() {
// 120.202.4.0|120.202.4.255|420600
Area area = IPUtils.getArea("120.202.4.50");
assertEquals("襄阳市", area.getName());
}
@Test
void testGetArea() {
// 120.203.123.0|120.203.133.255|360900
long ip = 0L;
try {
ip = Searcher.checkIP("120.203.123.252");
} catch (Exception e) {
// ignore
}
Area area = IPUtils.getArea(ip);
assertEquals("宜春市", area.getName());
}
}