diff --git a/pom.xml b/pom.xml
index 6a03abd4..523b4741 100644
--- a/pom.xml
+++ b/pom.xml
@@ -41,6 +41,8 @@
7.14.0
8.14.0
1.72
+
+ 2.7.0
1.33
@@ -348,6 +350,13 @@
${fastjson.version}
+
+
+ org.lionsoul
+ ip2region
+ ${ip2region.version}
+
+
diff --git a/ruoyi-api/ruoyi-api-system/src/main/java/com/ruoyi/system/api/domain/SysOperLog.java b/ruoyi-api/ruoyi-api-system/src/main/java/com/ruoyi/system/api/domain/SysOperLog.java
index 698622d6..6dcf2b4e 100644
--- a/ruoyi-api/ruoyi-api-system/src/main/java/com/ruoyi/system/api/domain/SysOperLog.java
+++ b/ruoyi-api/ruoyi-api-system/src/main/java/com/ruoyi/system/api/domain/SysOperLog.java
@@ -98,6 +98,12 @@ public class SysOperLog implements Serializable {
@ExcelProperty(value = "操作地址")
private String operIp;
+ /**
+ * 操作地点
+ */
+ @ExcelProperty(value = "操作地点")
+ private String operLocation;
+
/**
* 请求参数
*/
diff --git a/ruoyi-common/ruoyi-common-core/pom.xml b/ruoyi-common/ruoyi-common-core/pom.xml
index 69b52132..2f353ceb 100644
--- a/ruoyi-common/ruoyi-common-core/pom.xml
+++ b/ruoyi-common/ruoyi-common-core/pom.xml
@@ -104,6 +104,12 @@
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 9317e78e..84b25e60 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
@@ -1,11 +1,7 @@
package com.ruoyi.common.core.utils.ip;
-import cn.hutool.core.lang.Dict;
import cn.hutool.core.net.NetUtil;
import cn.hutool.http.HtmlUtil;
-import cn.hutool.http.HttpUtil;
-import com.ruoyi.common.core.constant.Constants;
-import com.ruoyi.common.core.utils.JsonUtils;
import com.ruoyi.common.core.utils.StringUtils;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
@@ -20,9 +16,6 @@ import lombok.extern.slf4j.Slf4j;
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class AddressUtils {
- // IP地址查询
- public static final String IP_URL = "http://whois.pconline.com.cn/ipJson.jsp";
-
// 未知地址
public static final String UNKNOWN = "XX XX";
@@ -37,23 +30,8 @@ public class AddressUtils {
return "内网IP";
}
// if (RuoYiConfig.isAddressEnabled()) {
- try {
- String rspStr = HttpUtil.createGet(IP_URL)
- .body("ip=" + ip + "&json=true", Constants.GBK)
- .execute()
- .body();
- if (StringUtils.isEmpty(rspStr)) {
- log.error("获取地理位置异常 {}", ip);
- return UNKNOWN;
- }
- Dict obj = JsonUtils.parseMap(rspStr);
- String region = obj.getStr("pro");
- String city = obj.getStr("city");
- return String.format("%s %s", region, city);
- } catch (Exception e) {
- log.error("获取地理位置异常 {}", ip);
- }
+ return RegionUtils.getCityInfo(ip);
// }
- return address;
+ // 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
new file mode 100644
index 00000000..9c5756c7
--- /dev/null
+++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/ip/RegionUtils.java
@@ -0,0 +1,86 @@
+package com.ruoyi.common.core.utils.ip;
+
+import cn.hutool.core.io.FileUtil;
+import cn.hutool.core.net.Ipv4Util;
+import cn.hutool.core.util.ObjectUtil;
+import cn.hutool.extra.servlet.ServletUtil;
+import com.ruoyi.common.core.exception.ServiceException;
+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地址定位工具类,离线方式
+ * 参考地址:集成 ip2region 实现离线IP地址定位库
+ *
+ * @author lishuyan
+ */
+@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;
+ }
+ }
+ }
+
+ 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地址库数据不存在!");
+ }
+ FileUtil.writeFromStream(resourceAsStream, existFile);
+ }
+
+ String dbPath = existFile.getPath();
+
+ // 1、从 dbPath 加载整个 xdb 到内存。
+ byte[] cBuff;
+ try {
+ cBuff = Searcher.loadContentFromFile(dbPath);
+ } catch (Exception e) {
+ throw new ServiceException(">>>>>>>> IpAddressUtil初始化失败,原因:从ip2region.xdb文件加载内容失败!" + e.getMessage());
+ }
+ // 2、使用上述的 cBuff 创建一个完全基于内存的查询对象。
+ try {
+ searcher = Searcher.newWithBuffer(cBuff);
+ } catch (Exception e) {
+ throw new ServiceException(">>>>>>>> IpAddressUtil初始化失败,原因:" + e.getMessage());
+ }
+ }
+
+ /**
+ * 根据IP地址离线获取城市
+ */
+ public static String getCityInfo(String ip) {
+ try {
+ ip = ip.trim();
+ // 3、执行查询
+ String region = searcher.search(ip);
+ return region.replace("0|", "").replace("|0", "");
+ } catch (Exception e) {
+ log.error("IP地址离线获取城市异常 {}", ip);
+ return "未知";
+ }
+ }
+}
diff --git a/ruoyi-common/ruoyi-common-core/src/main/resources/ip2region.xdb b/ruoyi-common/ruoyi-common-core/src/main/resources/ip2region.xdb
new file mode 100644
index 00000000..31f96a1f
Binary files /dev/null and b/ruoyi-common/ruoyi-common-core/src/main/resources/ip2region.xdb differ
diff --git a/ruoyi-common/ruoyi-common-log/src/main/java/com/ruoyi/common/log/aspect/LogAspect.java b/ruoyi-common/ruoyi-common-log/src/main/java/com/ruoyi/common/log/aspect/LogAspect.java
index fb77ca7f..fdd26b92 100644
--- a/ruoyi-common/ruoyi-common-log/src/main/java/com/ruoyi/common/log/aspect/LogAspect.java
+++ b/ruoyi-common/ruoyi-common-log/src/main/java/com/ruoyi/common/log/aspect/LogAspect.java
@@ -7,6 +7,7 @@ import com.ruoyi.common.core.utils.JsonUtils;
import com.ruoyi.common.core.utils.ServletUtils;
import com.ruoyi.common.core.utils.SpringUtils;
import com.ruoyi.common.core.utils.StringUtils;
+import com.ruoyi.common.core.utils.ip.AddressUtils;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessStatus;
import com.ruoyi.common.log.event.OperLogEvent;
@@ -70,6 +71,7 @@ public class LogAspect {
// 请求的地址
String ip = ServletUtils.getClientIP();
operLog.setOperIp(ip);
+ operLog.setOperLocation(AddressUtils.getRealAddressByIP(ip));
operLog.setOperUrl(StringUtils.substring(ServletUtils.getRequest().getRequestURI(), 0, 255));
operLog.setOperName(LoginHelper.getUsername());