mirror of
https://gitee.com/dromara/RuoYi-Cloud-Plus.git
synced 2025-09-03 11:05:58 +00:00
feat: 集成ip2region 实现离线IP地址定位库
This commit is contained in:
2
pom.xml
2
pom.xml
@@ -42,7 +42,7 @@
|
||||
<skywalking-toolkit.version>8.14.0</skywalking-toolkit.version>
|
||||
<bouncycastle.version>1.72</bouncycastle.version>
|
||||
<!-- 离线IP地址定位库 -->
|
||||
<ip2region.version>2.6.6</ip2region.version>
|
||||
<ip2region.version>2.7.0</ip2region.version>
|
||||
|
||||
<!-- 临时修复 snakeyaml 漏洞 -->
|
||||
<snakeyaml.version>1.33</snakeyaml.version>
|
||||
|
@@ -9,7 +9,7 @@ import cn.hutool.http.useragent.UserAgentUtil;
|
||||
import com.ruoyi.common.core.constant.CacheConstants;
|
||||
import com.ruoyi.common.core.enums.UserType;
|
||||
import com.ruoyi.common.core.utils.ServletUtils;
|
||||
import com.ruoyi.common.core.utils.ip.IpAddressUtil;
|
||||
import com.ruoyi.common.core.utils.ip.AddressUtils;
|
||||
import com.ruoyi.common.redis.utils.RedisUtils;
|
||||
import com.ruoyi.common.satoken.utils.LoginHelper;
|
||||
import com.ruoyi.system.api.domain.SysUserOnline;
|
||||
@@ -18,7 +18,6 @@ import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.time.Duration;
|
||||
|
||||
/**
|
||||
@@ -40,13 +39,12 @@ public class UserActionListener implements SaTokenListener {
|
||||
public void doLogin(String loginType, Object loginId, String tokenValue, SaLoginModel loginModel) {
|
||||
UserType userType = UserType.getUserType(loginId.toString());
|
||||
if (userType == UserType.SYS_USER) {
|
||||
HttpServletRequest request = ServletUtils.getRequest();
|
||||
UserAgent userAgent = UserAgentUtil.parse(request.getHeader("User-Agent"));
|
||||
String ip = IpAddressUtil.getIp(request);
|
||||
UserAgent userAgent = UserAgentUtil.parse(ServletUtils.getRequest().getHeader("User-Agent"));
|
||||
String ip = ServletUtils.getClientIP();
|
||||
LoginUser user = LoginHelper.getLoginUser();
|
||||
SysUserOnline userOnline = new SysUserOnline();
|
||||
userOnline.setIpaddr(ip);
|
||||
userOnline.setLoginLocation(IpAddressUtil.getCityInfo(ip));
|
||||
userOnline.setLoginLocation(AddressUtils.getRealAddressByIP(ip));
|
||||
userOnline.setBrowser(userAgent.getBrowser().getName());
|
||||
userOnline.setOs(userAgent.getOs().getName());
|
||||
userOnline.setLoginTime(System.currentTimeMillis());
|
||||
|
@@ -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;
|
||||
}
|
||||
}
|
||||
|
@@ -19,7 +19,7 @@ import java.io.InputStream;
|
||||
* @author lishuyan
|
||||
*/
|
||||
@Slf4j
|
||||
public class IpAddressUtil {
|
||||
public class RegionUtils {
|
||||
|
||||
private static final String LOCAL_REMOTE_HOST = "0:0:0:0:0:0:0:1";
|
||||
|
||||
@@ -45,7 +45,7 @@ public class IpAddressUtil {
|
||||
String fileName = "/ip2region.xdb";
|
||||
File existFile = FileUtil.file(FileUtil.getTmpDir() + FileUtil.FILE_SEPARATOR + fileName);
|
||||
if (!FileUtil.exist(existFile)) {
|
||||
InputStream resourceAsStream = IpAddressUtil.class.getResourceAsStream(fileName);
|
||||
InputStream resourceAsStream = RegionUtils.class.getResourceAsStream(fileName);
|
||||
if (ObjectUtil.isEmpty(resourceAsStream)) {
|
||||
throw new ServiceException(">>>>>>>> IpAddressUtil初始化失败,原因:IP地址库数据不存在!");
|
||||
}
|
@@ -7,7 +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.IpAddressUtil;
|
||||
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;
|
||||
@@ -69,9 +69,9 @@ public class LogAspect {
|
||||
OperLogEvent operLog = new OperLogEvent();
|
||||
operLog.setStatus(BusinessStatus.SUCCESS.ordinal());
|
||||
// 请求的地址
|
||||
String ip = IpAddressUtil.getIp(ServletUtils.getRequest());
|
||||
String ip = ServletUtils.getClientIP();
|
||||
operLog.setOperIp(ip);
|
||||
operLog.setOperLocation(IpAddressUtil.getCityInfo(ip));
|
||||
operLog.setOperLocation(AddressUtils.getRealAddressByIP(ip));
|
||||
operLog.setOperUrl(StringUtils.substring(ServletUtils.getRequest().getRequestURI(), 0, 255));
|
||||
operLog.setOperName(LoginHelper.getUsername());
|
||||
|
||||
|
Reference in New Issue
Block a user