add
This commit is contained in:
@ -20,5 +20,9 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.lionsoul</groupId>
|
||||
<artifactId>ip2region</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
@ -0,0 +1,42 @@
|
||||
package com.qiaoba.common.web.config;
|
||||
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import com.qiaoba.common.web.utils.IpUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.lionsoul.ip2region.xdb.Searcher;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.PreDestroy;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* 离线IP设置
|
||||
*
|
||||
* @author ailanyin
|
||||
* @version 1.0
|
||||
* @since 2023/5/25 16:32
|
||||
*/
|
||||
@Slf4j
|
||||
@Configuration
|
||||
public class IpConfig {
|
||||
|
||||
@PostConstruct
|
||||
public void dbToCache() {
|
||||
try {
|
||||
ClassPathResource classPathResource = new ClassPathResource("ip2region.xdb");
|
||||
InputStream inputStream = classPathResource.getInputStream();
|
||||
IpUtil.setSearcher(Searcher.newWithBuffer(IoUtil.read(inputStream).toByteArray()));
|
||||
log.info("加载IP离线库到成功");
|
||||
} catch (Exception e) {
|
||||
log.error("加载IP离线库到内存失败, 请联系管理员!");
|
||||
}
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
public void close() {
|
||||
IpUtil.closeSearcher();
|
||||
log.info("IP离线库已关闭");
|
||||
}
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
package com.qiaoba.common.web.utils;
|
||||
|
||||
import cn.hutool.extra.servlet.ServletUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.lionsoul.ip2region.xdb.Searcher;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* IP工具类
|
||||
*
|
||||
* @author ailanyin
|
||||
* @version 1.0
|
||||
* @since 2023/5/25 15:49
|
||||
*/
|
||||
@Slf4j
|
||||
public class IpUtil {
|
||||
|
||||
private static Searcher searcher;
|
||||
|
||||
public static void setSearcher(Searcher searcher) {
|
||||
IpUtil.searcher = searcher;
|
||||
}
|
||||
|
||||
public static String getIp(HttpServletRequest request) {
|
||||
return ServletUtil.getClientIP(request);
|
||||
}
|
||||
|
||||
public static String getIpAddr(String ip) {
|
||||
try {
|
||||
return searcher.search(ip);
|
||||
} catch (Exception e) {
|
||||
return "error ip";
|
||||
}
|
||||
}
|
||||
|
||||
public static void closeSearcher() {
|
||||
try {
|
||||
searcher.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.qiaoba.common.web.advice.ExceptionAdvice,\
|
||||
com.qiaoba.common.web.config.IpConfig,\
|
||||
com.qiaoba.common.web.config.GlobalCorsConfig
|
||||
|
||||
|
||||
|
Binary file not shown.
Reference in New Issue
Block a user