From 3fc3d52ba4bfedaac337e1529e00d2a01017da8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90li?= <15040126243@163.com> Date: Wed, 15 Mar 2023 17:59:57 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E4=BC=98=E5=8C=96=20!pr73=20=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 14 +++--- ruoyi-common/ruoyi-common-core/pom.xml | 12 ++--- .../common/core/utils/ip/AddressUtils.java | 6 +-- .../common/core/utils/ip/RegionUtils.java | 46 ++++++------------- 4 files changed, 27 insertions(+), 51 deletions(-) diff --git a/pom.xml b/pom.xml index 523b4741..fa1edcfe 100644 --- a/pom.xml +++ b/pom.xml @@ -337,6 +337,13 @@ ${tencent.sms.version} + + + org.lionsoul + ip2region + ${ip2region.version} + + org.yaml @@ -350,13 +357,6 @@ ${fastjson.version} - - - org.lionsoul - ip2region - ${ip2region.version} - - diff --git a/ruoyi-common/ruoyi-common-core/pom.xml b/ruoyi-common/ruoyi-common-core/pom.xml index 2f353ceb..f4b52efa 100644 --- a/ruoyi-common/ruoyi-common-core/pom.xml +++ b/ruoyi-common/ruoyi-common-core/pom.xml @@ -78,6 +78,12 @@ swagger-annotations + + + org.lionsoul + ip2region + + org.projectlombok lombok @@ -104,12 +110,6 @@ spring-boot-configuration-processor - - - org.lionsoul - ip2region - - diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/ip/AddressUtils.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/ip/AddressUtils.java index 84b25e60..ffef66f8 100644 --- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/ip/AddressUtils.java +++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/ip/AddressUtils.java @@ -20,18 +20,14 @@ public class AddressUtils { public static final String UNKNOWN = "XX XX"; public static String getRealAddressByIP(String ip) { - String address = UNKNOWN; if (StringUtils.isBlank(ip)) { - return address; + return UNKNOWN; } // 内网不查询 ip = "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : HtmlUtil.cleanHtmlTag(ip); if (NetUtil.isInnerIP(ip)) { return "内网IP"; } -// if (RuoYiConfig.isAddressEnabled()) { return RegionUtils.getCityInfo(ip); -// } - // return address; } } diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/ip/RegionUtils.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/ip/RegionUtils.java index 9c5756c7..4ff0f806 100644 --- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/ip/RegionUtils.java +++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/ip/RegionUtils.java @@ -1,16 +1,14 @@ package com.ruoyi.common.core.utils.ip; import cn.hutool.core.io.FileUtil; -import cn.hutool.core.net.Ipv4Util; +import cn.hutool.core.io.resource.ClassPathResource; import cn.hutool.core.util.ObjectUtil; -import cn.hutool.extra.servlet.ServletUtil; import com.ruoyi.common.core.exception.ServiceException; +import com.ruoyi.common.core.utils.file.FileUtils; import lombok.extern.slf4j.Slf4j; import org.lionsoul.ip2region.xdb.Searcher; -import javax.servlet.http.HttpServletRequest; import java.io.File; -import java.io.InputStream; /** * 根据ip地址定位工具类,离线方式 @@ -21,35 +19,17 @@ import java.io.InputStream; @Slf4j public class RegionUtils { - private static final String LOCAL_REMOTE_HOST = "0:0:0:0:0:0:0:1"; - - private static final Searcher searcher; - - /** - * 获取客户端ip - */ - public static String getIp(HttpServletRequest request) { - if (ObjectUtil.isEmpty(request)) { - return Ipv4Util.LOCAL_IP; - } else { - try { - String remoteHost = ServletUtil.getClientIP(request); - return LOCAL_REMOTE_HOST.equals(remoteHost) ? Ipv4Util.LOCAL_IP : remoteHost; - } catch (Exception e) { - return Ipv4Util.LOCAL_IP; - } - } - } + private static final Searcher SEARCHER; static { String fileName = "/ip2region.xdb"; - File existFile = FileUtil.file(FileUtil.getTmpDir() + FileUtil.FILE_SEPARATOR + fileName); - if (!FileUtil.exist(existFile)) { - InputStream resourceAsStream = RegionUtils.class.getResourceAsStream(fileName); - if (ObjectUtil.isEmpty(resourceAsStream)) { - throw new ServiceException(">>>>>>>> IpAddressUtil初始化失败,原因:IP地址库数据不存在!"); + File existFile = FileUtils.file(FileUtil.getTmpDir() + FileUtil.FILE_SEPARATOR + fileName); + if (!FileUtils.exist(existFile)) { + ClassPathResource fileStream = new ClassPathResource(fileName); + if (ObjectUtil.isEmpty(fileStream.getStream())) { + throw new ServiceException("RegionUtils初始化失败,原因:IP地址库数据不存在!"); } - FileUtil.writeFromStream(resourceAsStream, existFile); + FileUtils.writeFromStream(fileStream.getStream(), existFile); } String dbPath = existFile.getPath(); @@ -59,13 +39,13 @@ public class RegionUtils { try { cBuff = Searcher.loadContentFromFile(dbPath); } catch (Exception e) { - throw new ServiceException(">>>>>>>> IpAddressUtil初始化失败,原因:从ip2region.xdb文件加载内容失败!" + e.getMessage()); + throw new ServiceException("RegionUtils初始化失败,原因:从ip2region.xdb文件加载内容失败!" + e.getMessage()); } // 2、使用上述的 cBuff 创建一个完全基于内存的查询对象。 try { - searcher = Searcher.newWithBuffer(cBuff); + SEARCHER = Searcher.newWithBuffer(cBuff); } catch (Exception e) { - throw new ServiceException(">>>>>>>> IpAddressUtil初始化失败,原因:" + e.getMessage()); + throw new ServiceException("RegionUtils初始化失败,原因:" + e.getMessage()); } } @@ -76,7 +56,7 @@ public class RegionUtils { try { ip = ip.trim(); // 3、执行查询 - String region = searcher.search(ip); + String region = SEARCHER.search(ip); return region.replace("0|", "").replace("|0", ""); } catch (Exception e) { log.error("IP地址离线获取城市异常 {}", ip);