diff --git a/build.gradle b/build.gradle index 17b90da..a57072b 100644 --- a/build.gradle +++ b/build.gradle @@ -46,9 +46,11 @@ dependencies { testImplementation 'org.springframework.boot:spring-boot-starter-test' // 测试框架 implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' // thymeleaf模版引擎 implementation 'com.aliyun.oss:aliyun-sdk-oss:3.18.3' // OSS SDK - implementation 'cn.hutool:hutool-all:5.8.40' // hutool工具包 + implementation 'cn.hutool:hutool-all:5.8.42' // hutool工具包 implementation 'io.springfox:springfox-swagger2:2.10.5' implementation 'com.github.xiaoymin:knife4j-openapi2-spring-boot-starter:4.4.0' + implementation 'org.lionsoul:ip2region:2.7.0' + implementation 'jakarta.servlet:jakarta.servlet-api:5.0.0' } diff --git a/src/main/java/com/corewing/app/entity/AppAccessStatistics.java b/src/main/java/com/corewing/app/entity/AppAccessStatistics.java index c803fe7..456b856 100644 --- a/src/main/java/com/corewing/app/entity/AppAccessStatistics.java +++ b/src/main/java/com/corewing/app/entity/AppAccessStatistics.java @@ -33,6 +33,9 @@ public class AppAccessStatistics extends CommonEntity { @ApiModelProperty("访问IP") private String accessIp; + @ApiModelProperty("访问地址") + private String accessAddress; + @ApiModelProperty("备注") private String remark; diff --git a/src/main/java/com/corewing/app/service/impl/AppAccessStatisticsServiceImpl.java b/src/main/java/com/corewing/app/service/impl/AppAccessStatisticsServiceImpl.java index ad03a22..819e644 100644 --- a/src/main/java/com/corewing/app/service/impl/AppAccessStatisticsServiceImpl.java +++ b/src/main/java/com/corewing/app/service/impl/AppAccessStatisticsServiceImpl.java @@ -7,6 +7,7 @@ import com.corewing.app.dto.AccessStatisticsRequest; import com.corewing.app.entity.AppAccessStatistics; import com.corewing.app.mapper.AppAccessStatisticsMapper; import com.corewing.app.service.AppAccessStatisticsService; +import com.corewing.app.util.CommonIpAddressUtil; import com.corewing.app.util.CommonServletUtil; import com.corewing.app.util.IpUtil; import org.springframework.stereotype.Service; @@ -33,6 +34,7 @@ public class AppAccessStatisticsServiceImpl extends ServiceImplip2region + * + * @author xuyuxiang + * @date 2020/3/16 11:25 + */ +@Slf4j +public class CommonIpAddressUtil { + + private static final String LOCAL_REMOTE_HOST = "0:0:0:0:0:0:0:1"; + + private static final String IP2REGION_DB_PATH = "/ip2region.xdb"; + + private static final Searcher searcher = initSearcher(); + + /** + * 私有构造函数防止实例化 + */ + private CommonIpAddressUtil() { + } + + /** + * 初始化Searcher实例 + * + * @return Searcher + */ + private static Searcher initSearcher() { + try { + final File dbFile = FileUtil.file(FileUtil.getTmpDir() + FileUtil.FILE_SEPARATOR + IP2REGION_DB_PATH); + if (!FileUtil.exist(dbFile)) { + try (InputStream inputStream = CommonIpAddressUtil.class.getResourceAsStream(IP2REGION_DB_PATH)) { + FileUtil.writeFromStream(inputStream, dbFile); + } + } + // 1、从 dbPath 加载整个 xdb 到内存。 + final byte[] cBuff = Searcher.loadContentFromFile(dbFile.getPath()); + // 2、使用上述的 cBuff 创建一个完全基于内存的查询对象。 + return Searcher.newWithBuffer(cBuff); + } catch (Exception e) { + log.error(">>> CommonIpAddressUtil初始化异常:", e); + throw new RuntimeException("CommonIpAddressUtil初始化异常"); + } + } + + /** + * 获取客户端ip + * + * @author xuyuxiang + * @date 2020/3/19 9:32 + */ + public static String getIp(HttpServletRequest request) { + if (ObjectUtil.isEmpty(request)) { + return Ipv4Util.LOCAL_IP; + } + try { + String remoteHost = JakartaServletUtil.getClientIP(request); + return LOCAL_REMOTE_HOST.equals(remoteHost) ? Ipv4Util.LOCAL_IP : remoteHost; + } catch (Exception e) { + log.error(">>> 获取客户端ip异常:", e); + return Ipv4Util.LOCAL_IP; + } + } + + /** + * 根据IP地址离线获取城市 + * + * @author xuyuxiang + * @date 2022/4/27 23:14 + */ + public static String getCityInfo(String ip) { + try { + // 3、执行查询 + String region = searcher.search(ip.trim()); + return region.replace("0|", "").replace("|0", ""); + } catch (Exception e) { + return "未知"; + } + } +} diff --git a/src/main/resources/ip2region.xdb b/src/main/resources/ip2region.xdb new file mode 100644 index 0000000..31f96a1 Binary files /dev/null and b/src/main/resources/ip2region.xdb differ