+ * 依赖于ip2region.xdb精简版,来源于 + * 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)); + } } diff --git a/yudao-framework/yudao-spring-boot-starter-biz-ip/src/main/resources/ip2region.xdb b/yudao-framework/yudao-spring-boot-starter-biz-ip/src/main/resources/ip2region.xdb new file mode 100644 index 000000000..58596a591 Binary files /dev/null and b/yudao-framework/yudao-spring-boot-starter-biz-ip/src/main/resources/ip2region.xdb differ diff --git a/yudao-framework/yudao-spring-boot-starter-biz-ip/src/test/java/cn/iocoder/yudao/framework/ip/core/utils/IPUtilsTest.java b/yudao-framework/yudao-spring-boot-starter-biz-ip/src/test/java/cn/iocoder/yudao/framework/ip/core/utils/IPUtilsTest.java new file mode 100644 index 000000000..e8871419b --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-biz-ip/src/test/java/cn/iocoder/yudao/framework/ip/core/utils/IPUtilsTest.java @@ -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()); + + } +} \ No newline at end of file