refactor: 优化部分配置及工具类使用,适配最新 ContiNew Starter

IpUtils 已经提取到 ContiNew Starter
This commit is contained in:
2023-11-26 14:36:52 +08:00
parent f28fbd14fa
commit feef427d41
14 changed files with 24 additions and 203 deletions

View File

@@ -34,12 +34,6 @@
<artifactId>continew-starter-data-mybatis-plus</artifactId>
</dependency>
<!-- ContiNew Starter 缓存模块 - Redisson -->
<dependency>
<groupId>top.charles7c.continew</groupId>
<artifactId>continew-starter-cache-redisson</artifactId>
</dependency>
<!-- ContiNew Starter 验证码模块 - 图形验证码 -->
<dependency>
<groupId>top.charles7c.continew</groupId>
@@ -93,11 +87,5 @@
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
</dependency>
<!-- 第三方封装 Ip2region离线 IP 数据管理框架和定位库支持亿级别的数据段10 微秒级别的查询性能,提供了许多主流编程语言的 xdb 数据管理引擎的实现) -->
<dependency>
<groupId>net.dreamlu</groupId>
<artifactId>mica-ip2region</artifactId>
</dependency>
</dependencies>
</project>

View File

@@ -1,44 +0,0 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.cnadmin.common.config.properties;
import lombok.Data;
import org.springframework.stereotype.Component;
import cn.hutool.core.convert.Convert;
import cn.hutool.extra.spring.SpringUtil;
/**
* 项目配置属性
*
* @author Charles7c
* @since 2022/12/11 19:26
*/
@Data
@Component
public class ProjectProperties extends top.charles7c.continew.starter.core.autoconfigure.ProjectProperties {
/**
* 是否本地解析 IP 归属地
*/
public static final boolean IP_ADDR_LOCAL_PARSE_ENABLED;
static {
IP_ADDR_LOCAL_PARSE_ENABLED = Convert.toBool(SpringUtil.getProperty("project.ipAddrLocalParseEnabled"));
}
}

View File

@@ -1,111 +0,0 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.cnadmin.common.util;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import cn.hutool.core.net.NetUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.hutool.http.HtmlUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import top.charles7c.cnadmin.common.config.properties.ProjectProperties;
import net.dreamlu.mica.ip2region.core.Ip2regionSearcher;
import net.dreamlu.mica.ip2region.core.IpInfo;
/**
* IP 工具类
*
* @author Charles7c
* @since 2022/12/23 20:00
*/
@Slf4j
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class IpUtils {
/**
* 太平洋网开放 API查询 IP 归属地
*/
private static final String IP_URL = "http://whois.pconline.com.cn/ipJson.jsp?ip=%s&json=true";
/**
* 根据 IP 获取归属地信息
*
* @param ip
* IP 地址
* @return 归属地信息
*/
public static String getCityInfo(String ip) {
if (ProjectProperties.IP_ADDR_LOCAL_PARSE_ENABLED) {
return getLocalCityInfo(ip);
} else {
return getHttpCityInfo(ip);
}
}
/**
* 根据 IP 获取归属地信息(网络解析)
*
* @param ip
* IP 地址
* @return 归属地信息
*/
public static String getHttpCityInfo(String ip) {
if (isInnerIp(ip)) {
return "内网IP";
}
String api = String.format(IP_URL, ip);
JSONObject object = JSONUtil.parseObj(HttpUtil.get(api));
return object.get("addr", String.class);
}
/**
* 根据 IP 获取归属地信息(本地解析)
*
* @param ip
* IP 地址
* @return 归属地信息
*/
public static String getLocalCityInfo(String ip) {
if (isInnerIp(ip)) {
return "内网IP";
}
Ip2regionSearcher ip2regionSearcher = SpringUtil.getBean(Ip2regionSearcher.class);
IpInfo ipInfo = ip2regionSearcher.memorySearch(ip);
if (null != ipInfo) {
return ipInfo.getAddress();
}
return null;
}
/**
* 是否为内网 IPv4
*
* @param ip
* IP 地址
* @return 是否为内网 IP
*/
public static boolean isInnerIp(String ip) {
ip = "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : HtmlUtil.cleanHtmlTag(ip);
return NetUtil.isInnerIP(ip);
}
}

View File

@@ -33,10 +33,10 @@ import top.charles7c.cnadmin.common.constant.CacheConstants;
import top.charles7c.cnadmin.common.model.dto.LogContext;
import top.charles7c.cnadmin.common.model.dto.LoginUser;
import top.charles7c.cnadmin.common.service.CommonUserService;
import top.charles7c.cnadmin.common.util.IpUtils;
import top.charles7c.cnadmin.common.util.ServletUtils;
import top.charles7c.cnadmin.common.util.holder.LogContextHolder;
import top.charles7c.continew.starter.core.util.ExceptionUtils;
import top.charles7c.continew.starter.core.util.IpUtils;
/**
* 登录助手