Commit 0b5aa560b392071f019f52796662fdc21f01f919
1 parent
7f131a38
ip库引入,查询工具类
Showing
4 changed files
with
160 additions
and
0 deletions
yudao-framework/yudao-spring-boot-starter-biz-ip/pom.xml
| ... | ... | @@ -20,12 +20,34 @@ |
| 20 | 20 | </description> |
| 21 | 21 | <url>https://github.com/YunaiV/ruoyi-vue-pro</url> |
| 22 | 22 | |
| 23 | + <properties> | |
| 24 | + <ip2region.version>2.6.6</ip2region.version> | |
| 25 | + </properties> | |
| 26 | + | |
| 23 | 27 | <dependencies> |
| 24 | 28 | <dependency> |
| 25 | 29 | <groupId>cn.iocoder.boot</groupId> |
| 26 | 30 | <artifactId>yudao-common</artifactId> |
| 27 | 31 | </dependency> |
| 28 | 32 | |
| 33 | + <!-- IP地址检索 --> | |
| 34 | + <dependency> | |
| 35 | + <groupId>org.lionsoul</groupId> | |
| 36 | + <artifactId>ip2region</artifactId> | |
| 37 | + <version>${ip2region.version}</version> | |
| 38 | + </dependency> | |
| 39 | + | |
| 40 | + <dependency> | |
| 41 | + <groupId>org.projectlombok</groupId> | |
| 42 | + <artifactId>lombok</artifactId> | |
| 43 | + </dependency> | |
| 44 | + | |
| 45 | + <dependency> | |
| 46 | + <groupId>org.slf4j</groupId> | |
| 47 | + <artifactId>slf4j-api</artifactId> | |
| 48 | + <scope>provided</scope> <!-- 设置为 provided,只有工具类需要使用到 --> | |
| 49 | + </dependency> | |
| 50 | + | |
| 29 | 51 | <!-- Test 测试相关 --> |
| 30 | 52 | <dependency> |
| 31 | 53 | <groupId>cn.iocoder.boot</groupId> | ... | ... |
yudao-framework/yudao-spring-boot-starter-biz-ip/src/main/java/cn/iocoder/yudao/framework/ip/core/utils/IPUtils.java
| 1 | 1 | package cn.iocoder.yudao.framework.ip.core.utils; |
| 2 | 2 | |
| 3 | +import cn.iocoder.yudao.framework.common.exception.ServiceException; | |
| 4 | +import cn.iocoder.yudao.framework.ip.core.Area; | |
| 5 | +import lombok.extern.slf4j.Slf4j; | |
| 6 | +import org.lionsoul.ip2region.xdb.Searcher; | |
| 7 | + | |
| 8 | +import java.io.IOException; | |
| 9 | + | |
| 10 | +import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR; | |
| 11 | + | |
| 3 | 12 | /** |
| 4 | 13 | * IP 工具类 |
| 14 | + * <p> | |
| 15 | + * 依赖于ip2region.xdb精简版,来源于<a href="https://gitee.com/zhijiantianya/ip2region"/> | |
| 16 | + * region精简为城市编码 | |
| 5 | 17 | * |
| 6 | 18 | * @author 芋道源码 |
| 7 | 19 | */ |
| 20 | +@Slf4j | |
| 8 | 21 | public class IPUtils { |
| 9 | 22 | |
| 23 | + /** | |
| 24 | + * 根据ip搜索 | |
| 25 | + * 启动加载到内存中 | |
| 26 | + */ | |
| 27 | + private static Searcher SEARCHER; | |
| 28 | + | |
| 29 | + /** | |
| 30 | + * 初始化SEARCHER | |
| 31 | + */ | |
| 32 | + private final static IPUtils INSTANCE = new IPUtils(); | |
| 33 | + | |
| 34 | + | |
| 35 | + /** | |
| 36 | + * 私有化构造 | |
| 37 | + */ | |
| 38 | + private IPUtils() { | |
| 39 | + String dbPath = "ip2region.xdb"; | |
| 40 | + dbPath = IPUtils.class.getClassLoader().getResource(dbPath).getPath(); | |
| 41 | + try { | |
| 42 | + SEARCHER = Searcher.newWithBuffer(Searcher.loadContentFromFile(dbPath)); | |
| 43 | + } catch (IOException e) { | |
| 44 | + // 加载xdb文件异常,不影响启动 | |
| 45 | + log.error("启动加载IP SEARCH异常", e); | |
| 46 | + SEARCHER = null; | |
| 47 | + } | |
| 48 | + } | |
| 49 | + | |
| 50 | + | |
| 51 | + /** | |
| 52 | + * 查询IP对应的地区ID,格式应为127.0.0.1 | |
| 53 | + * @param ip ip地址 | |
| 54 | + * @return 地区id | |
| 55 | + */ | |
| 56 | + public static Integer getAreaId(String ip) { | |
| 57 | + try { | |
| 58 | + return Integer.parseInt(SEARCHER.search(ip)); | |
| 59 | + } catch (Exception e) { | |
| 60 | + throw new ServiceException(INTERNAL_SERVER_ERROR); | |
| 61 | + } | |
| 62 | + } | |
| 63 | + | |
| 64 | + /** | |
| 65 | + * 查询IP对应的地区ID,格式参考{@link Searcher#checkIP(String)} 的返回 | |
| 66 | + * @param ip ip地址 | |
| 67 | + * @return 地区id | |
| 68 | + */ | |
| 69 | + public static Integer getAreaId(long ip) { | |
| 70 | + try { | |
| 71 | + return Integer.parseInt(SEARCHER.search(ip)); | |
| 72 | + } catch (Exception e) { | |
| 73 | + throw new ServiceException(INTERNAL_SERVER_ERROR); | |
| 74 | + } | |
| 75 | + } | |
| 76 | + | |
| 77 | + /** | |
| 78 | + * 查询IP对应的地区,格式应为127.0.0.1 | |
| 79 | + * @param ip ip地址 | |
| 80 | + * @return 地区 | |
| 81 | + */ | |
| 82 | + public static Area getArea(String ip) { | |
| 83 | + return AreaUtils.getArea(getAreaId(ip)); | |
| 84 | + } | |
| 10 | 85 | |
| 86 | + /** | |
| 87 | + * 查询IP对应的地区,格式参考{@link Searcher#checkIP(String)} 的返回 | |
| 88 | + * @param ip ip地址 | |
| 89 | + * @return 地区 | |
| 90 | + */ | |
| 91 | + public static Area getArea(long ip) { | |
| 92 | + return AreaUtils.getArea(getAreaId(ip)); | |
| 93 | + } | |
| 11 | 94 | } | ... | ... |
yudao-framework/yudao-spring-boot-starter-biz-ip/src/main/resources/ip2region.xdb
0 → 100644
No preview for this file type
yudao-framework/yudao-spring-boot-starter-biz-ip/src/test/java/cn/iocoder/yudao/framework/ip/core/utils/IPUtilsTest.java
0 → 100644
| 1 | +package cn.iocoder.yudao.framework.ip.core.utils; | |
| 2 | + | |
| 3 | +import cn.iocoder.yudao.framework.ip.core.Area; | |
| 4 | +import org.junit.jupiter.api.Test; | |
| 5 | +import org.lionsoul.ip2region.xdb.Searcher; | |
| 6 | + | |
| 7 | + | |
| 8 | +import static org.junit.jupiter.api.Assertions.assertEquals; | |
| 9 | + | |
| 10 | +/** | |
| 11 | + * {@link IPUtils} 的单元测试 | |
| 12 | + */ | |
| 13 | +class IPUtilsTest { | |
| 14 | + | |
| 15 | + @Test | |
| 16 | + void getAreaId() { | |
| 17 | + // 120.202.4.0|120.202.4.255|420600 | |
| 18 | + Integer areaId = IPUtils.getAreaId("120.202.4.50"); | |
| 19 | + assertEquals(420600, areaId); | |
| 20 | + } | |
| 21 | + | |
| 22 | + @Test | |
| 23 | + void testGetAreaId() { | |
| 24 | + // 120.203.123.0|120.203.133.255|360900 | |
| 25 | + long ip = 0L; | |
| 26 | + try { | |
| 27 | + ip = Searcher.checkIP("120.203.123.250"); | |
| 28 | + } catch (Exception e) { | |
| 29 | + // ignore | |
| 30 | + } | |
| 31 | + Integer areaId = IPUtils.getAreaId(ip); | |
| 32 | + assertEquals(360900, areaId); | |
| 33 | + } | |
| 34 | + | |
| 35 | + @Test | |
| 36 | + void getArea() { | |
| 37 | + // 120.202.4.0|120.202.4.255|420600 | |
| 38 | + Area area = IPUtils.getArea("120.202.4.50"); | |
| 39 | + assertEquals("襄阳市", area.getName()); | |
| 40 | + } | |
| 41 | + | |
| 42 | + @Test | |
| 43 | + void testGetArea() { | |
| 44 | + // 120.203.123.0|120.203.133.255|360900 | |
| 45 | + long ip = 0L; | |
| 46 | + try { | |
| 47 | + ip = Searcher.checkIP("120.203.123.252"); | |
| 48 | + } catch (Exception e) { | |
| 49 | + // ignore | |
| 50 | + } | |
| 51 | + Area area = IPUtils.getArea(ip); | |
| 52 | + assertEquals("宜春市", area.getName()); | |
| 53 | + | |
| 54 | + } | |
| 55 | +} | |
| 0 | 56 | \ No newline at end of file | ... | ... |