ruoyi集成qqwry实现本地ip地址解析
ruoyi使用的是http://whois.pconline.com.cn/ipJson.jsp在线请求然后获取结果,识别精度真不太行,偶尔解析成不了
在ruoyi-common/ip/AddressUtils里
添加qqwry-starter
<dependency>
<groupId>com.github.hiwepy</groupId>
<artifactId>qqwry-spring-boot-starter</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
加上个Config 主要是用于输出qqwry版本 可选
package com.ruoyi.framework.config;
import com.github.hiwepy.qqwry.spring.boot.ext.IPZone;
import com.github.hiwepy.qqwry.spring.boot.ext.QQWry;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@Slf4j
@Component
public class QQwryConfig {
@Resource
private QQWry qqWry;
@Value("${qqwry.location}")
private String location;
@PostConstruct
public void log() {
String version = parseDatabaseVersion();
log.info("qqwry加载成功. 路径: {} 版本: [{}]", location, version);
}
private String parseDatabaseVersion() {
Pattern dbVerPattern = Pattern.compile("(\\d+)年(\\d+)月(\\d+)日.*");
IPZone ipz = qqWry.findIP("255.255.255.255");
Matcher m = dbVerPattern.matcher(ipz.getSubInfo());
if (!m.matches() || m.groupCount() != 3) {
return "0.0.0";
}
return String.format("%s.%s.%s", m.group(1), m.group(2), m.group(3));
}
}
在application-xx.yml配置
qqwry:
external: true # 是否需要自定义路径 否则会读取qqwry-starter下的 版本是2021 挺老了
location: classpath:ip/QQWry.dat #这里填写自定义路径 放在classpath下或者外部都可以
注意一下路径大小写, windows对大小写不敏感, qqwry和QQWry都可以读取, 但是linux对大小写敏感,需要填写原文件名。路径填写错误的话会报错BeanNotDefine
剩下的就是修改Util
有三个调用的地方, 替换一下就行了