初始化仓库
This commit is contained in:
43
xtools-boot-ip/pom.xml
Normal file
43
xtools-boot-ip/pom.xml
Normal file
@@ -0,0 +1,43 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.xujun</groupId>
|
||||
<artifactId>xtools-boot</artifactId>
|
||||
<version>5.0.0</version>
|
||||
</parent>
|
||||
<artifactId>xtools-boot-ip</artifactId>
|
||||
|
||||
<!-- 依赖 -->
|
||||
<dependencies>
|
||||
<!-- xtools begin -->
|
||||
<!-- xtools-extend 模块 -->
|
||||
<!-- ip2region(本地IP查询工具) -->
|
||||
<dependency>
|
||||
<groupId>org.lionsoul</groupId>
|
||||
<artifactId>ip2region</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.xujun</groupId>
|
||||
<artifactId>xtools-extend</artifactId>
|
||||
</dependency>
|
||||
<!-- xtools end -->
|
||||
|
||||
<!-- xtools-boot begin -->
|
||||
<!-- xtools-boot-core -->
|
||||
<dependency>
|
||||
<groupId>org.xujun</groupId>
|
||||
<artifactId>xtools-boot-core</artifactId>
|
||||
</dependency>
|
||||
<!-- xtools-boot end -->
|
||||
|
||||
<!-- spring-boot -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,28 @@
|
||||
package xtools.boot.ip;
|
||||
|
||||
import org.springframework.context.annotation.Import;
|
||||
import xtools.boot.core.utils.ModuleLoadUtils;
|
||||
import xtools.boot.ip.selector.BootIpImportSelector;
|
||||
|
||||
/**
|
||||
* <p>Title : BootIpConfiguration</p>
|
||||
* <p>Description : BootIpConfiguration</p>
|
||||
* <p>DevelopTools : Idea_x64_v2026.1</p>
|
||||
* <p>DevelopSystem : macOS Sequoia 15.7.5</p>
|
||||
* <p>Company : org.xujun</p>
|
||||
*
|
||||
* @author : XuJun
|
||||
* @version : 5.0.0
|
||||
* @date : 2026/01/01 09:30
|
||||
*/
|
||||
@Import(BootIpImportSelector.class)
|
||||
public class BootIpConfiguration {
|
||||
|
||||
/**
|
||||
* 构造方法
|
||||
*/
|
||||
public BootIpConfiguration() {
|
||||
ModuleLoadUtils.loadSuccess(BootIpConfiguration.class);
|
||||
}
|
||||
|
||||
}
|
||||
49
xtools-boot-ip/src/main/java/xtools/boot/ip/init/InitIp.java
Normal file
49
xtools-boot-ip/src/main/java/xtools/boot/ip/init/InitIp.java
Normal file
@@ -0,0 +1,49 @@
|
||||
package xtools.boot.ip.init;
|
||||
|
||||
import org.jspecify.annotations.NonNull;
|
||||
import org.lionsoul.ip2region.xdb.Version;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.stereotype.Component;
|
||||
import xtools.base.config.BaseParams;
|
||||
import xtools.boot.ip.utils.IpUtils;
|
||||
import xtools.extend.IpLocalUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* <p>Title : InitIp</p>
|
||||
* <p>Description : InitIp</p>
|
||||
* <p>DevelopTools : Idea_x64_v2026.1</p>
|
||||
* <p>DevelopSystem : macOS Sequoia 15.7.5</p>
|
||||
* <p>Company : org.xujun</p>
|
||||
*
|
||||
* @author : XuJun
|
||||
* @version : 5.0.0
|
||||
* @date : 2026/2/16 10:21
|
||||
*/
|
||||
@Component
|
||||
@Order(BaseParams.CP_NUM50)
|
||||
public class InitIp implements ApplicationRunner {
|
||||
|
||||
private static final String IP_FILE = "ip/ip2region_v4.xdb";
|
||||
|
||||
@Override
|
||||
public void run(@NonNull ApplicationArguments args) throws Exception {
|
||||
init();
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化IP库
|
||||
*/
|
||||
private void init() throws IOException {
|
||||
ClassPathResource classPathResource = new ClassPathResource(IP_FILE);
|
||||
try (InputStream is = classPathResource.getInputStream()) {
|
||||
IpLocalUtils.init(is, Version.IPv4);
|
||||
IpUtils.init();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package xtools.boot.ip.selector;
|
||||
|
||||
import org.jspecify.annotations.NonNull;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
|
||||
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
|
||||
import org.springframework.core.type.AnnotationMetadata;
|
||||
|
||||
/**
|
||||
* <p>Title : BootIpImportSelector</p>
|
||||
* <p>Description : BootIpImportSelector</p>
|
||||
* <p>DevelopTools : Idea_x64_v2026.1</p>
|
||||
* <p>DevelopSystem : macOS Sequoia 15.7.5</p>
|
||||
* <p>Company : org.xujun</p>
|
||||
*
|
||||
* @author : XuJun
|
||||
* @version : 5.0.0
|
||||
* @date : 2026/01/01 09:30
|
||||
*/
|
||||
public class BootIpImportSelector implements ImportBeanDefinitionRegistrar {
|
||||
|
||||
/**
|
||||
* 根据给定的注释元数据,根据需要注册bean
|
||||
*
|
||||
* @param importingClassMetadata AnnotationMetadata
|
||||
* @param registry BeanDefinitionRegistry
|
||||
*/
|
||||
@Override
|
||||
public void registerBeanDefinitions(@NonNull AnnotationMetadata importingClassMetadata, @NonNull BeanDefinitionRegistry registry) {
|
||||
// 构建扫描对象
|
||||
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(registry, true);
|
||||
// 扫描包下路径
|
||||
scanner.scan("xtools.boot.ip");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package xtools.boot.ip.utils;
|
||||
|
||||
import xtools.base.config.BaseParams;
|
||||
import xtools.boot.api.exection.BizError;
|
||||
import xtools.core.StringUtils;
|
||||
import xtools.extend.IpLocalUtils;
|
||||
import xtools.extend.dto.IpAddrDto;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.StringJoiner;
|
||||
|
||||
/**
|
||||
* <p>Title : IpUtils</p>
|
||||
* <p>Description : IpUtils</p>
|
||||
* <p>DevelopTools : Idea_x64_v2026.1</p>
|
||||
* <p>DevelopSystem : macOS Sequoia 15.7.5</p>
|
||||
* <p>Company : org.xujun</p>
|
||||
*
|
||||
* @author : XuJun
|
||||
* @version : 5.0.0
|
||||
* @date : 2026/2/16 10:41
|
||||
*/
|
||||
public class IpUtils implements BaseParams {
|
||||
|
||||
/**
|
||||
* 是否初始化
|
||||
*/
|
||||
private static boolean init = false;
|
||||
|
||||
/**
|
||||
* 初始化
|
||||
*/
|
||||
public static void init() {
|
||||
init = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询IP对应地址
|
||||
*
|
||||
* @param ip IP
|
||||
* @return IP对应地址
|
||||
*/
|
||||
public static IpAddrDto search(String ip) {
|
||||
if (!init) {
|
||||
throw new BizError("请先初始化IpUtils");
|
||||
}
|
||||
return IpLocalUtils.search(ip, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取IP对应地址
|
||||
*
|
||||
* @param ip IP
|
||||
* @return IP对应地址
|
||||
*/
|
||||
public static String searchAddr(String ip) {
|
||||
return searchAddr(search(ip));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取IP对应地址
|
||||
*
|
||||
* @param addr 地址信息
|
||||
* @return IP对应地址
|
||||
*/
|
||||
public static String searchAddr(IpAddrDto addr) {
|
||||
if (Objects.isNull(addr)) {
|
||||
return "未知";
|
||||
}
|
||||
StringJoiner joiner = new StringJoiner(CP_COMMA);
|
||||
if (StringUtils.isNotBlank(addr.getCountry())) {
|
||||
joiner.add(addr.getCountry());
|
||||
}
|
||||
if (StringUtils.isNotBlank(addr.getProvince())) {
|
||||
joiner.add(addr.getProvince());
|
||||
}
|
||||
if (StringUtils.isNotBlank(addr.getCity())) {
|
||||
joiner.add(addr.getCity());
|
||||
}
|
||||
return joiner.toString();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
xtools.boot.ip.BootIpConfiguration
|
||||
BIN
xtools-boot-ip/src/main/resources/ip/ip2region_v4.xdb
Normal file
BIN
xtools-boot-ip/src/main/resources/ip/ip2region_v4.xdb
Normal file
Binary file not shown.
Reference in New Issue
Block a user