mirror of
http://192.168.1.205:9980/cf_devdept2/cf_imes_server.git
synced 2026-08-13 05:12:07 +08:00
1、新增ip服务查询接口,对接阿里云第三方服务,服务封装、ip模块旧引用清理、新增ip服务开关和配置;2、新增组织地理信息字段;3、新增登录对地理信息的校验;
This commit is contained in:
+13
@@ -66,4 +66,17 @@ public class StrUtils {
|
||||
.collect(Collectors.joining("\n"));
|
||||
}
|
||||
|
||||
/**
|
||||
* a字符串是否包含b
|
||||
*
|
||||
* @param a
|
||||
* @param b
|
||||
* @return
|
||||
*/
|
||||
public static boolean contains(String a, String b) {
|
||||
if (CharSequenceUtil.isEmpty(a) || CharSequenceUtil.isEmpty(b)) {
|
||||
return false;
|
||||
}
|
||||
return a.contains(b);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,12 +25,6 @@
|
||||
<artifactId>cf-common</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- IP地址检索 -->
|
||||
<dependency>
|
||||
<groupId>org.lionsoul</groupId>
|
||||
<artifactId>ip2region</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
@@ -48,6 +42,16 @@
|
||||
<artifactId>cf-spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>fastjson</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package com.cf.imes.framework.ip.config;
|
||||
|
||||
import com.cf.imes.framework.ip.core.property.IPQueryProperties;
|
||||
import com.cf.imes.framework.ip.core.service.IPQueryService;
|
||||
import com.cf.imes.framework.ip.core.service.imp.IPQueryServiceImpl;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
/**
|
||||
* ip自动注册配置
|
||||
*
|
||||
* @author 晨丰科技
|
||||
*/
|
||||
@AutoConfiguration
|
||||
@EnableConfigurationProperties(IPQueryProperties.class)
|
||||
public class ChenfengIPAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
public IPQueryService ipQueryService() {
|
||||
return new IPQueryServiceImpl();
|
||||
}
|
||||
}
|
||||
-55
@@ -1,55 +0,0 @@
|
||||
package com.cf.imes.framework.ip.core;
|
||||
|
||||
import com.cf.imes.framework.ip.core.enums.AreaTypeEnum;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 区域节点,包括国家、省份、城市、地区等信息
|
||||
*
|
||||
* 数据可见 resources/area.csv 文件
|
||||
*
|
||||
* @author 晨丰科技
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class Area {
|
||||
|
||||
/**
|
||||
* 编号 - 全球,即根目录
|
||||
*/
|
||||
public static final Integer ID_GLOBAL = 0;
|
||||
/**
|
||||
* 编号 - 中国
|
||||
*/
|
||||
public static final Integer ID_CHINA = 1;
|
||||
|
||||
/**
|
||||
* 编号
|
||||
*/
|
||||
private Integer id;
|
||||
/**
|
||||
* 名字
|
||||
*/
|
||||
private String name;
|
||||
/**
|
||||
* 类型
|
||||
*
|
||||
* 枚举 {@link AreaTypeEnum}
|
||||
*/
|
||||
private Integer type;
|
||||
|
||||
/**
|
||||
* 父节点
|
||||
*/
|
||||
private Area parent;
|
||||
/**
|
||||
* 子节点
|
||||
*/
|
||||
private List<Area> children;
|
||||
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.cf.imes.framework.ip.core.enums;
|
||||
|
||||
import com.cf.imes.framework.common.exception.ErrorCode;
|
||||
|
||||
/**
|
||||
* ip查询 错误码枚举类
|
||||
*
|
||||
* 使用 1-008-000-000 段
|
||||
*/
|
||||
public class ErrorCodeConstants {
|
||||
|
||||
public static final ErrorCode IP_QUERY_ERROR = new ErrorCode(1_008_000_000, "IP归属地查询异常,请联系客服处理");
|
||||
}
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
package com.cf.imes.framework.ip.core.property;
|
||||
|
||||
import com.cf.imes.framework.common.util.encrypt.AesUtils;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
|
||||
/**
|
||||
* ip查询服务配置
|
||||
* @author Gqr
|
||||
* @since 2024/10/30 9:59
|
||||
*/
|
||||
@ConfigurationProperties(prefix = "chenfeng.ipquery")
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class IPQueryProperties {
|
||||
|
||||
@Value("${chenfeng.encrypt.publicKey:}")
|
||||
private String publicKey;
|
||||
|
||||
/**
|
||||
* 接口地址
|
||||
*/
|
||||
private String apiUrl;
|
||||
|
||||
/**
|
||||
* 接口认证编码
|
||||
*/
|
||||
private String appCode;
|
||||
|
||||
public String getAppCode() {
|
||||
return appCode;
|
||||
}
|
||||
|
||||
public IPQueryProperties setAppCode(String appCode) {
|
||||
this.appCode = AesUtils.decrypt(appCode, publicKey);
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getApiUrl() {
|
||||
return apiUrl;
|
||||
}
|
||||
|
||||
public void setApiUrl(String apiUrl) {
|
||||
this.apiUrl = apiUrl;
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package com.cf.imes.framework.ip.core.service;
|
||||
|
||||
import com.cf.imes.framework.ip.core.service.dto.IPQueryDataRespDTO;
|
||||
|
||||
/**
|
||||
* ip查询服务
|
||||
*
|
||||
* @author Gqr
|
||||
* @since 2024/10/30 10:54
|
||||
*/
|
||||
public interface IPQueryService {
|
||||
/**
|
||||
* 查询ip归属地
|
||||
*
|
||||
* @param ip
|
||||
* @return
|
||||
*/
|
||||
IPQueryDataRespDTO querySource(String ip);
|
||||
}
|
||||
+83
@@ -0,0 +1,83 @@
|
||||
package com.cf.imes.framework.ip.core.service.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* ip来源查询明细
|
||||
*
|
||||
* @author Gqr
|
||||
* @since 2024/10/30 11:00
|
||||
*/
|
||||
@Data
|
||||
public class IPQueryDataRespDTO implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 3403763102169461000L;
|
||||
|
||||
/**
|
||||
* 国家
|
||||
*/
|
||||
private String country;
|
||||
|
||||
/**
|
||||
* 国家编号
|
||||
*/
|
||||
private String countryCode;
|
||||
|
||||
/**
|
||||
* 省份
|
||||
*/
|
||||
private String prov;
|
||||
|
||||
/**
|
||||
* 城市
|
||||
*/
|
||||
private String city;
|
||||
|
||||
/**
|
||||
* 城市编号
|
||||
*/
|
||||
private String cityCode;
|
||||
|
||||
/**
|
||||
* 城市编号缩写
|
||||
*/
|
||||
private String cityShortCode;
|
||||
|
||||
/**
|
||||
* 区域
|
||||
*/
|
||||
private String area;
|
||||
|
||||
/**
|
||||
* 邮编
|
||||
*/
|
||||
private String postCode;
|
||||
|
||||
/**
|
||||
* 区域编码
|
||||
*/
|
||||
private String areaCode;
|
||||
|
||||
/**
|
||||
* 运营商
|
||||
*/
|
||||
private String isp;
|
||||
|
||||
/**
|
||||
* 经度
|
||||
*/
|
||||
private String lng;
|
||||
|
||||
/**
|
||||
* 纬度
|
||||
*/
|
||||
private String lat;
|
||||
|
||||
/**
|
||||
* 大区
|
||||
*/
|
||||
private String bigArea;
|
||||
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package com.cf.imes.framework.ip.core.service.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* ip来源查询结果
|
||||
*
|
||||
* @author Gqr
|
||||
* @since 2024/10/30 10:59
|
||||
*/
|
||||
@Data
|
||||
public class IPQueryRespDTO implements Serializable {
|
||||
private static final long serialVersionUID = -5526001196829386416L;
|
||||
|
||||
/**
|
||||
* 响应码,200:正常、400:ip参数不正确
|
||||
*/
|
||||
private int ret;
|
||||
|
||||
/**
|
||||
* ip来源信息
|
||||
*/
|
||||
private IPQueryDataRespDTO data;
|
||||
}
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
package com.cf.imes.framework.ip.core.service.imp;
|
||||
|
||||
import cn.hutool.core.text.CharSequenceUtil;
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import cn.hutool.http.HttpResponse;
|
||||
import cn.hutool.http.HttpStatus;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.cf.imes.framework.common.exception.ErrorCode;
|
||||
import com.cf.imes.framework.common.exception.ServiceException;
|
||||
import com.cf.imes.framework.common.exception.util.ServiceExceptionUtil;
|
||||
import com.cf.imes.framework.ip.core.enums.ErrorCodeConstants;
|
||||
import com.cf.imes.framework.ip.core.property.IPQueryProperties;
|
||||
import com.cf.imes.framework.ip.core.service.IPQueryService;
|
||||
import com.cf.imes.framework.ip.core.service.dto.IPQueryDataRespDTO;
|
||||
import com.cf.imes.framework.ip.core.service.dto.IPQueryRespDTO;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* @author Gqr
|
||||
* @since 2024/10/30 11:02
|
||||
*/
|
||||
@Slf4j
|
||||
public class IPQueryServiceImpl implements IPQueryService {
|
||||
|
||||
@Resource
|
||||
private IPQueryProperties ipQueryProperties;
|
||||
|
||||
@Override
|
||||
public IPQueryDataRespDTO querySource(String ip) {
|
||||
ErrorCode queryError = ErrorCodeConstants.IP_QUERY_ERROR;
|
||||
try {
|
||||
String apiUrl = ipQueryProperties.getApiUrl();
|
||||
String appCode = ipQueryProperties.getAppCode();
|
||||
if (CharSequenceUtil.isEmpty(apiUrl)) {
|
||||
log.error("[IPQueryService][querySource]request apiUrl为空");
|
||||
throw ServiceExceptionUtil.exception(queryError);
|
||||
}
|
||||
if (CharSequenceUtil.isEmpty(appCode)) {
|
||||
log.error("[IPQueryService][querySource]request appCode为空");
|
||||
throw ServiceExceptionUtil.exception(queryError);
|
||||
}
|
||||
log.info("[IPQueryService][querySource]request url:{}", apiUrl);
|
||||
log.info("[IPQueryService][querySource]request param, appCode: {}, ip: {}", appCode, ip);
|
||||
HttpResponse execute = HttpRequest.get(apiUrl).header("Authorization", "APPCODE " + appCode).form("ip", ip).execute();
|
||||
log.info("[IPQueryService][querySource]request status:{}, response: {}", execute.getStatus(), execute.body());
|
||||
String respBody = execute.body();
|
||||
IPQueryRespDTO ipQueryRespDTO = JSON.parseObject(execute.body(), IPQueryRespDTO.class);
|
||||
if (HttpStatus.HTTP_OK == ipQueryRespDTO.getRet()) {
|
||||
return ipQueryRespDTO.getData();
|
||||
} else {
|
||||
ServiceException serviceException = ServiceExceptionUtil.exception(queryError);
|
||||
log.error(serviceException.getMessage() + ",状态【{}】,异常:{}", ipQueryRespDTO.getRet(), respBody);
|
||||
throw serviceException;
|
||||
}
|
||||
} catch (ServiceException se) {
|
||||
throw se;
|
||||
} catch (Exception e) {
|
||||
log.error(queryError.getMsg(), e);
|
||||
throw new ServiceException(queryError);
|
||||
}
|
||||
}
|
||||
}
|
||||
-162
@@ -1,162 +0,0 @@
|
||||
package com.cf.imes.framework.ip.core.utils;
|
||||
|
||||
import cn.hutool.core.io.resource.ResourceUtil;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.text.csv.CsvRow;
|
||||
import cn.hutool.core.text.csv.CsvUtil;
|
||||
import com.cf.imes.framework.common.util.object.ObjectUtils;
|
||||
import com.cf.imes.framework.ip.core.Area;
|
||||
import com.cf.imes.framework.ip.core.enums.AreaTypeEnum;
|
||||
import lombok.NonNull;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
import static com.cf.imes.framework.common.util.collection.CollectionUtils.convertList;
|
||||
|
||||
/**
|
||||
* 区域工具类
|
||||
*
|
||||
* @author 晨丰科技
|
||||
*/
|
||||
@Slf4j
|
||||
public class AreaUtils {
|
||||
|
||||
/**
|
||||
* 初始化 SEARCHER
|
||||
*/
|
||||
@SuppressWarnings("InstantiationOfUtilityClass")
|
||||
private final static AreaUtils INSTANCE = new AreaUtils();
|
||||
|
||||
/**
|
||||
* Area 内存缓存,提升访问速度
|
||||
*/
|
||||
private static Map<Integer, Area> areas;
|
||||
|
||||
private AreaUtils() {
|
||||
long now = System.currentTimeMillis();
|
||||
areas = new HashMap<>();
|
||||
areas.put(Area.ID_GLOBAL, new Area(Area.ID_GLOBAL, "全球", 0,
|
||||
null, new ArrayList<>()));
|
||||
// 从 csv 中加载数据
|
||||
List<CsvRow> rows = CsvUtil.getReader().read(ResourceUtil.getUtf8Reader("area.csv")).getRows();
|
||||
rows.remove(0); // 删除 header
|
||||
for (CsvRow row : rows) {
|
||||
// 创建 Area 对象
|
||||
Area area = new Area(Integer.valueOf(row.get(0)), row.get(1), Integer.valueOf(row.get(2)),
|
||||
null, new ArrayList<>());
|
||||
// 添加到 areas 中
|
||||
areas.put(area.getId(), area);
|
||||
}
|
||||
|
||||
// 构建父子关系:因为 Area 中没有 parentId 字段,所以需要重复读取
|
||||
for (CsvRow row : rows) {
|
||||
Area area = areas.get(Integer.valueOf(row.get(0))); // 自己
|
||||
Area parent = areas.get(Integer.valueOf(row.get(3))); // 父
|
||||
Assert.isTrue(area != parent, "{}:父子节点相同", area.getName());
|
||||
area.setParent(parent);
|
||||
parent.getChildren().add(area);
|
||||
}
|
||||
log.info("启动加载 AreaUtils 成功,耗时 ({}) 毫秒", System.currentTimeMillis() - now);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得指定编号对应的区域
|
||||
*
|
||||
* @param id 区域编号
|
||||
* @return 区域
|
||||
*/
|
||||
public static Area getArea(Integer id) {
|
||||
return areas.get(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化区域
|
||||
*
|
||||
* @param id 区域编号
|
||||
* @return 格式化后的区域
|
||||
*/
|
||||
public static String format(Integer id) {
|
||||
return format(id, " ");
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化区域
|
||||
*
|
||||
* 例如说:
|
||||
* 1. id = “静安区”时:上海 上海市 静安区
|
||||
* 2. id = “上海市”时:上海 上海市
|
||||
* 3. id = “上海”时:上海
|
||||
* 4. id = “美国”时:美国
|
||||
* 当区域在中国时,默认不显示中国
|
||||
*
|
||||
* @param id 区域编号
|
||||
* @param separator 分隔符
|
||||
* @return 格式化后的区域
|
||||
*/
|
||||
public static String format(Integer id, String separator) {
|
||||
// 获得区域
|
||||
Area area = areas.get(id);
|
||||
if (area == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 格式化
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < AreaTypeEnum.values().length; i++) { // 避免死循环
|
||||
sb.insert(0, area.getName());
|
||||
// “递归”父节点
|
||||
area = area.getParent();
|
||||
if (area == null
|
||||
|| ObjectUtils.equalsAny(area.getId(), Area.ID_GLOBAL, Area.ID_CHINA)) { // 跳过父节点为中国的情况
|
||||
break;
|
||||
}
|
||||
sb.insert(0, separator);
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定类型的区域列表
|
||||
*
|
||||
* @param type 区域类型
|
||||
* @param func 转换函数
|
||||
* @param <T> 结果类型
|
||||
* @return 区域列表
|
||||
*/
|
||||
public static <T> List<T> getByType(AreaTypeEnum type, Function<Area, T> func) {
|
||||
return convertList(areas.values(), func, area -> type.getType().equals(area.getType()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据区域编号、上级区域类型,获取上级区域编号
|
||||
*
|
||||
* @param id 区域编号
|
||||
* @param type 区域类型
|
||||
* @return 上级区域编号
|
||||
*/
|
||||
public static Integer getParentIdByType(Integer id, @NonNull AreaTypeEnum type) {
|
||||
for (int i = 0; i < Byte.MAX_VALUE; i++) {
|
||||
Area area = AreaUtils.getArea(id);
|
||||
if (area == null) {
|
||||
return null;
|
||||
}
|
||||
// 情况一:匹配到,返回它
|
||||
if (type.getType().equals(area.getType())) {
|
||||
return area.getId();
|
||||
}
|
||||
// 情况二:找到根节点,返回空
|
||||
if (area.getParent() == null || area.getParent().getId() == null) {
|
||||
return null;
|
||||
}
|
||||
// 其它:继续向上查找
|
||||
id = area.getParent().getId();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
-87
@@ -1,87 +0,0 @@
|
||||
package com.cf.imes.framework.ip.core.utils;
|
||||
|
||||
import cn.hutool.core.io.resource.ResourceUtil;
|
||||
import com.cf.imes.framework.ip.core.Area;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.lionsoul.ip2region.xdb.Searcher;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* IP 工具类
|
||||
*
|
||||
* IP 数据源来自 ip2region.xdb 精简版,基于 <a href="https://gitee.com/zhijiantianya/ip2region"/> 项目
|
||||
*
|
||||
* @author wanglhup
|
||||
*/
|
||||
@Slf4j
|
||||
public class IPUtils {
|
||||
|
||||
/**
|
||||
* 初始化 SEARCHER
|
||||
*/
|
||||
@SuppressWarnings("InstantiationOfUtilityClass")
|
||||
private final static IPUtils INSTANCE = new IPUtils();
|
||||
|
||||
/**
|
||||
* IP 查询器,启动加载到内存中
|
||||
*/
|
||||
private static Searcher SEARCHER;
|
||||
|
||||
/**
|
||||
* 私有化构造
|
||||
*/
|
||||
private IPUtils() {
|
||||
try {
|
||||
long now = System.currentTimeMillis();
|
||||
byte[] bytes = ResourceUtil.readBytes("ip2region.xdb");
|
||||
SEARCHER = Searcher.newWithBuffer(bytes);
|
||||
log.info("启动加载 IPUtils 成功,耗时 ({}) 毫秒", System.currentTimeMillis() - now);
|
||||
} catch (IOException e) {
|
||||
log.error("启动加载 IPUtils 失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询 IP 对应的地区编号
|
||||
*
|
||||
* @param ip IP 地址,格式为 127.0.0.1
|
||||
* @return 地区id
|
||||
*/
|
||||
@SneakyThrows
|
||||
public static Integer getAreaId(String ip) {
|
||||
return Integer.parseInt(SEARCHER.search(ip.trim()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询 IP 对应的地区编号
|
||||
*
|
||||
* @param ip IP 地址的时间戳,格式参考{@link Searcher#checkIP(String)} 的返回
|
||||
* @return 地区编号
|
||||
*/
|
||||
@SneakyThrows
|
||||
public static Integer getAreaId(long ip) {
|
||||
return Integer.parseInt(SEARCHER.search(ip));
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询 IP 对应的地区
|
||||
*
|
||||
* @param ip IP 地址,格式为 127.0.0.1
|
||||
* @return 地区
|
||||
*/
|
||||
public static Area getArea(String ip) {
|
||||
return AreaUtils.getArea(getAreaId(ip));
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询 IP 对应的地区
|
||||
*
|
||||
* @param ip IP 地址的时间戳,格式参考{@link Searcher#checkIP(String)} 的返回
|
||||
* @return 地区
|
||||
*/
|
||||
public static Area getArea(long ip) {
|
||||
return AreaUtils.getArea(getAreaId(ip));
|
||||
}
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
com.cf.imes.framework.ip.config.ChenfengIPAutoConfiguration
|
||||
File diff suppressed because it is too large
Load Diff
Binary file not shown.
-36
@@ -1,36 +0,0 @@
|
||||
package com.cf.imes.framework.ip.core.utils;
|
||||
|
||||
|
||||
import com.cf.imes.framework.ip.core.Area;
|
||||
import com.cf.imes.framework.ip.core.enums.AreaTypeEnum;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
/**
|
||||
* {@link AreaUtils} 的单元测试
|
||||
*
|
||||
* @author 晨丰科技
|
||||
*/
|
||||
public class AreaUtilsTest {
|
||||
|
||||
@Test
|
||||
public void testGetArea() {
|
||||
// 调用:北京
|
||||
Area area = AreaUtils.getArea(110100);
|
||||
// 断言
|
||||
assertEquals(area.getId(), 110100);
|
||||
assertEquals(area.getName(), "北京市");
|
||||
assertEquals(area.getType(), AreaTypeEnum.CITY.getType());
|
||||
assertEquals(area.getParent().getId(), 110000);
|
||||
assertEquals(area.getChildren().size(), 16);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFormat() {
|
||||
assertEquals(AreaUtils.format(110105), "北京 北京市 朝阳区");
|
||||
assertEquals(AreaUtils.format(1), "中国");
|
||||
assertEquals(AreaUtils.format(2), "蒙古");
|
||||
}
|
||||
|
||||
}
|
||||
-47
@@ -1,47 +0,0 @@
|
||||
package com.cf.imes.framework.ip.core.utils;
|
||||
|
||||
import com.cf.imes.framework.ip.core.Area;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.lionsoul.ip2region.xdb.Searcher;
|
||||
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
/**
|
||||
* {@link IPUtils} 的单元测试
|
||||
*
|
||||
* @author wanglhup
|
||||
*/
|
||||
public class IPUtilsTest {
|
||||
|
||||
@Test
|
||||
public void testGetAreaId_string() {
|
||||
// 120.202.4.0|120.202.4.255|420600
|
||||
Integer areaId = IPUtils.getAreaId("120.202.4.50");
|
||||
assertEquals(420600, areaId);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAreaId_long() throws Exception {
|
||||
// 120.203.123.0|120.203.133.255|360900
|
||||
long ip = Searcher.checkIP("120.203.123.250");
|
||||
Integer areaId = IPUtils.getAreaId(ip);
|
||||
assertEquals(360900, areaId);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetArea_string() {
|
||||
// 120.202.4.0|120.202.4.255|420600
|
||||
Area area = IPUtils.getArea("120.202.4.50");
|
||||
assertEquals("襄阳市", area.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetArea_long() throws Exception {
|
||||
// 120.203.123.0|120.203.133.255|360900
|
||||
long ip = Searcher.checkIP("120.203.123.252");
|
||||
Area area = IPUtils.getArea(ip);
|
||||
assertEquals("宜春市", area.getName());
|
||||
}
|
||||
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package com.cf.imes.framework.ip.service;
|
||||
|
||||
import com.cf.imes.framework.common.exception.ServiceException;
|
||||
import com.cf.imes.framework.ip.config.ChenfengIPAutoConfiguration;
|
||||
import com.cf.imes.framework.ip.core.service.IPQueryService;
|
||||
import com.cf.imes.framework.ip.core.service.dto.IPQueryDataRespDTO;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import static com.cf.imes.framework.test.core.util.RandomUtils.randomString;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
/**
|
||||
* {@link IPQueryService} 的单元测试
|
||||
*
|
||||
* @author gaoqr
|
||||
*/
|
||||
@SpringBootTest(classes = {ChenfengIPAutoConfiguration.class}, webEnvironment = SpringBootTest.WebEnvironment.NONE)
|
||||
@ActiveProfiles("unit-test") // 设置使用 application-unit-test 配置文件
|
||||
public class IPQueryServiceTest {
|
||||
@Resource
|
||||
private IPQueryService ipQueryService;
|
||||
|
||||
|
||||
@Test
|
||||
public void testQuerySourceFail() {
|
||||
assertThrows(ServiceException.class, () -> ipQueryService.querySource(randomString()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testQuerySourceSuccess() {
|
||||
IPQueryDataRespDTO ipQueryDataRespDTO = ipQueryService.querySource("220.250.48.74");
|
||||
assertNotNull(ipQueryDataRespDTO);
|
||||
assertEquals(ipQueryDataRespDTO.getProv(), "福建");
|
||||
assertEquals(ipQueryDataRespDTO.getCity(), "福州");
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
# 晨丰配置项,设置当前项目所有自定义的配置
|
||||
chenfeng:
|
||||
encrypt:
|
||||
publicKey: cfimes
|
||||
ipquery:
|
||||
apiUrl: https://ipquery.market.alicloudapi.com/query
|
||||
appCode: zAE0h8wOBZbwIwDob1tXJfJsdxpQ2QYzIgie7oSozCLHXsdD8hhkot6UBs55Vhm35KYrqGj+t0W4IOSr
|
||||
+2
@@ -17,6 +17,8 @@ public class ErrorCodeConstants {
|
||||
public static final ErrorCode AUTH_TOKEN_EXPIRED = new ErrorCode(1_002_000_006, "Token 已经过期");
|
||||
public static final ErrorCode AUTH_MOBILE_NOT_EXISTS = new ErrorCode(1_002_000_007, "手机号不存在");
|
||||
public static final ErrorCode AUTH_MOBILE_NO_CHANGE = new ErrorCode(1_002_000_008, "手机号未发生改变,无需修改");
|
||||
public static final ErrorCode AUTH_LOGIN_GEO_UNCORRECT = new ErrorCode(1_002_000_009, "登录失败,请前往机构登记地址登录系统");
|
||||
public static final ErrorCode AUTH_LOGIN_ORG_GEO_EMPTY = new ErrorCode(1_002_000_010, "当前组织地址未登记,请联系组织管理员");
|
||||
|
||||
// ========== 菜单模块 1-002-001-000 ==========
|
||||
public static final ErrorCode MENU_NAME_DUPLICATE = new ErrorCode(1_002_001_000, "已经存在该名字的菜单");
|
||||
|
||||
+2
-2
@@ -54,7 +54,6 @@ import java.util.Set;
|
||||
import static com.cf.imes.framework.common.pojo.CommonResult.success;
|
||||
import static com.cf.imes.framework.common.util.collection.CollectionUtils.convertSet;
|
||||
import static com.cf.imes.framework.common.util.servlet.ServletUtils.getClientIP;
|
||||
import static com.cf.imes.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
|
||||
|
||||
@Tag(name = "管理后台 - 认证")
|
||||
@RestController
|
||||
@@ -86,7 +85,8 @@ public class AuthController {
|
||||
@PermitAll
|
||||
@Operation(summary = "使用账号密码登录")
|
||||
@OperateLog(enable = false) // 避免 Post 请求被记录操作日志
|
||||
public CommonResult<AuthLoginRespVO> login(@Valid @RequestBody AuthLoginReqVO reqVO) {
|
||||
public CommonResult<AuthLoginRespVO> login(@Valid @RequestBody AuthLoginReqVO reqVO, HttpServletRequest request) {
|
||||
reqVO.setIp(getClientIP(request));
|
||||
return success(authService.login(reqVO));
|
||||
}
|
||||
|
||||
|
||||
+3
@@ -51,6 +51,9 @@ public class AuthLoginReqVO {
|
||||
@Schema(description = "state", requiredMode = Schema.RequiredMode.REQUIRED, example = "9b2ffbc1-7425-4155-9894-9d5c08541d62")
|
||||
private String socialState;
|
||||
|
||||
@Schema(hidden = true)
|
||||
private String ip;
|
||||
|
||||
/**
|
||||
* 开启验证码的 Group
|
||||
*/
|
||||
|
||||
-5
@@ -1,5 +0,0 @@
|
||||
### 获得地区树
|
||||
GET {{baseUrl}}/system/area/tree
|
||||
Authorization: Bearer {{token}}
|
||||
organ-id: {{adminTenentId}}
|
||||
|
||||
-50
@@ -1,50 +0,0 @@
|
||||
package com.cf.imes.module.system.controller.admin.ip;
|
||||
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import com.cf.imes.framework.common.pojo.CommonResult;
|
||||
import com.cf.imes.framework.common.util.object.BeanUtils;
|
||||
import com.cf.imes.framework.ip.core.Area;
|
||||
import com.cf.imes.framework.ip.core.utils.AreaUtils;
|
||||
import com.cf.imes.framework.ip.core.utils.IPUtils;
|
||||
import com.cf.imes.module.system.controller.admin.ip.vo.AreaNodeRespVO;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static com.cf.imes.framework.common.pojo.CommonResult.success;
|
||||
|
||||
@Tag(name = "管理后台 - 地区")
|
||||
@RestController
|
||||
@RequestMapping("/system/area")
|
||||
@Validated
|
||||
public class AreaController {
|
||||
|
||||
@GetMapping("/tree")
|
||||
@Operation(summary = "获得地区树")
|
||||
public CommonResult<List<AreaNodeRespVO>> getAreaTree() {
|
||||
Area area = AreaUtils.getArea(Area.ID_CHINA);
|
||||
Assert.notNull(area, "获取不到中国");
|
||||
return success(BeanUtils.toBean(area.getChildren(), AreaNodeRespVO.class));
|
||||
}
|
||||
|
||||
@GetMapping("/get-by-ip")
|
||||
@Operation(summary = "获得 IP 对应的地区名")
|
||||
@Parameter(name = "ip", description = "IP", required = true)
|
||||
public CommonResult<String> getAreaByIp(@RequestParam("ip") String ip) {
|
||||
// 获得城市
|
||||
Area area = IPUtils.getArea(ip);
|
||||
if (area == null) {
|
||||
return success("未知");
|
||||
}
|
||||
// 格式化返回
|
||||
return success(AreaUtils.format(area.getId()));
|
||||
}
|
||||
|
||||
}
|
||||
-23
@@ -1,23 +0,0 @@
|
||||
package com.cf.imes.module.system.controller.admin.ip.vo;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Schema(description = "管理后台 - 地区节点 Response VO")
|
||||
@Data
|
||||
public class AreaNodeRespVO {
|
||||
|
||||
@Schema(description = "编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "110000")
|
||||
private Integer id;
|
||||
|
||||
@Schema(description = "名字", requiredMode = Schema.RequiredMode.REQUIRED, example = "北京")
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* 子节点
|
||||
*/
|
||||
private List<AreaNodeRespVO> children;
|
||||
|
||||
}
|
||||
+24
@@ -80,4 +80,28 @@ public class OrganSaveReqVO {
|
||||
@Size(max = 200, message = "备注长度不能超过200个字符")
|
||||
private String remark;
|
||||
|
||||
|
||||
@Schema(description = "省")
|
||||
@Size(max = 64, message = "省名称长度不能超过64个字符")
|
||||
private String province;
|
||||
|
||||
@Schema(description = "市")
|
||||
@Size(max = 64, message = "市名称长度不能超过64个字符")
|
||||
private String city;
|
||||
|
||||
@Schema(description = "区")
|
||||
@Size(max = 64, message = "区名称长度不能超过64个字符")
|
||||
private String county;
|
||||
|
||||
@Schema(description = "地区编码")
|
||||
@Size(max = 64, message = "地区编码长度不能超过64个字符")
|
||||
private String areaCode;
|
||||
|
||||
@Schema(description = "经度")
|
||||
@Size(max = 64, message = "经度长度不能超过64个字符")
|
||||
private String longitude;
|
||||
|
||||
@Schema(description = "纬度")
|
||||
@Size(max = 64, message = "纬度长度不能超过64个字符")
|
||||
private String latitude;
|
||||
}
|
||||
|
||||
-34
@@ -1,34 +0,0 @@
|
||||
package com.cf.imes.module.system.controller.app.ip;
|
||||
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import com.cf.imes.framework.common.pojo.CommonResult;
|
||||
import com.cf.imes.framework.common.util.object.BeanUtils;
|
||||
import com.cf.imes.framework.ip.core.Area;
|
||||
import com.cf.imes.framework.ip.core.utils.AreaUtils;
|
||||
import com.cf.imes.module.system.controller.app.ip.vo.AppAreaNodeRespVO;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static com.cf.imes.framework.common.pojo.CommonResult.success;
|
||||
|
||||
@Tag(name = "用户 App - 地区")
|
||||
@RestController
|
||||
@RequestMapping("/system/area")
|
||||
@Validated
|
||||
public class AppAreaController {
|
||||
|
||||
@GetMapping("/tree")
|
||||
@Operation(summary = "获得地区树")
|
||||
public CommonResult<List<AppAreaNodeRespVO>> getAreaTree() {
|
||||
Area area = AreaUtils.getArea(Area.ID_CHINA);
|
||||
Assert.notNull(area, "获取不到中国");
|
||||
return success(BeanUtils.toBean(area.getChildren(), AppAreaNodeRespVO.class));
|
||||
}
|
||||
|
||||
}
|
||||
-23
@@ -1,23 +0,0 @@
|
||||
package com.cf.imes.module.system.controller.app.ip.vo;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Schema(description = "用户 App - 地区节点 Response VO")
|
||||
@Data
|
||||
public class AppAreaNodeRespVO {
|
||||
|
||||
@Schema(description = "编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "110000")
|
||||
private Integer id;
|
||||
|
||||
@Schema(description = "名字", requiredMode = Schema.RequiredMode.REQUIRED, example = "北京")
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* 子节点
|
||||
*/
|
||||
private List<AppAreaNodeRespVO> children;
|
||||
|
||||
}
|
||||
-20
@@ -1,20 +0,0 @@
|
||||
package com.cf.imes.module.system.convert.ip;
|
||||
|
||||
import com.cf.imes.framework.ip.core.Area;
|
||||
import com.cf.imes.module.system.controller.admin.ip.vo.AreaNodeRespVO;
|
||||
import com.cf.imes.module.system.controller.app.ip.vo.AppAreaNodeRespVO;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
public interface AreaConvert {
|
||||
|
||||
AreaConvert INSTANCE = Mappers.getMapper(AreaConvert.class);
|
||||
|
||||
List<AreaNodeRespVO> convertList(List<Area> list);
|
||||
|
||||
List<AppAreaNodeRespVO> convertList3(List<Area> list);
|
||||
|
||||
}
|
||||
+30
@@ -112,4 +112,34 @@ public class OrganizationDO extends BaseDO {
|
||||
* 备注
|
||||
*/
|
||||
private String remark;
|
||||
|
||||
/**
|
||||
* 省份
|
||||
*/
|
||||
private String province;
|
||||
|
||||
/**
|
||||
* 市
|
||||
*/
|
||||
private String city;
|
||||
|
||||
/**
|
||||
* 区县
|
||||
*/
|
||||
private String county;
|
||||
|
||||
/**
|
||||
* 地区代码
|
||||
*/
|
||||
private String areaCode;
|
||||
|
||||
/**
|
||||
* 经度
|
||||
*/
|
||||
private String longitude;
|
||||
|
||||
/**
|
||||
* 纬度
|
||||
*/
|
||||
private String latitude;
|
||||
}
|
||||
|
||||
-38
@@ -1,38 +0,0 @@
|
||||
package com.cf.imes.module.system.framework.operatelog.core;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.cf.imes.framework.ip.core.utils.AreaUtils;
|
||||
import com.mzt.logapi.service.IParseFunction;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 地名的 {@link IParseFunction} 实现类
|
||||
*
|
||||
* @author HUIHUI
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class AreaParseFunction implements IParseFunction {
|
||||
|
||||
public static final String NAME = "getArea";
|
||||
|
||||
@Override
|
||||
public boolean executeBefore() {
|
||||
return true; // 先转换值后对比
|
||||
}
|
||||
|
||||
@Override
|
||||
public String functionName() {
|
||||
return NAME;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String apply(Object value) {
|
||||
if (StrUtil.isEmptyIfStr(value)) {
|
||||
return "";
|
||||
}
|
||||
return AreaUtils.format(Integer.parseInt(value.toString()));
|
||||
}
|
||||
|
||||
}
|
||||
+50
-1
@@ -6,7 +6,10 @@ import com.cf.imes.framework.common.enums.UserTypeEnum;
|
||||
import com.cf.imes.framework.common.exception.util.ServiceExceptionUtil;
|
||||
import com.cf.imes.framework.common.util.monitor.TracerUtils;
|
||||
import com.cf.imes.framework.common.util.servlet.ServletUtils;
|
||||
import com.cf.imes.framework.common.util.string.StrUtils;
|
||||
import com.cf.imes.framework.common.util.validation.ValidationUtils;
|
||||
import com.cf.imes.framework.ip.core.service.IPQueryService;
|
||||
import com.cf.imes.framework.ip.core.service.dto.IPQueryDataRespDTO;
|
||||
import com.cf.imes.module.system.api.logger.dto.LoginLogCreateReqDTO;
|
||||
import com.cf.imes.module.system.api.sms.SmsCodeApi;
|
||||
import com.cf.imes.module.system.api.social.dto.SocialUserBindReqDTO;
|
||||
@@ -83,12 +86,21 @@ public class AdminAuthServiceImpl implements AdminAuthService {
|
||||
@Resource
|
||||
private SmsCodeService smsCodeService;
|
||||
|
||||
@Resource
|
||||
private IPQueryService ipQueryService;
|
||||
|
||||
/**
|
||||
* 验证码的开关,默认为 true
|
||||
*/
|
||||
@Value("${chenfeng.captcha.enable:true}")
|
||||
private Boolean captchaEnable;
|
||||
|
||||
/**
|
||||
* 登录ip地址位置校验,默认为 false不开启
|
||||
*/
|
||||
@Value("${chenfeng.ipquery.enable:false}")
|
||||
private boolean ipQueryEnable;
|
||||
|
||||
@Override
|
||||
public AdminUserDO authenticate(AuthLoginReqVO reqVO) {
|
||||
String username = reqVO.getUsername();
|
||||
@@ -102,7 +114,11 @@ public class AdminAuthServiceImpl implements AdminAuthService {
|
||||
throw ServiceExceptionUtil.exception(ErrorCodeConstants.AUTH_LOGIN_BAD_CREDENTIALS);
|
||||
}
|
||||
// 校验机构有效性
|
||||
organService.validOrgan(user.getOrganId());
|
||||
OrganizationDO organizationDO = organService.validOrgan(user.getOrganId());
|
||||
// 校验机构地理位置
|
||||
if (ipQueryEnable && !checkGeo(organizationDO, ServletUtils.getClientIP())) {
|
||||
throw ServiceExceptionUtil.exception(ErrorCodeConstants.AUTH_LOGIN_GEO_UNCORRECT);
|
||||
}
|
||||
// 校验密码
|
||||
if (!userService.isPasswordMatch(password, user.getPassword())) {
|
||||
createLoginLog(user.getId(), username, logTypeEnum, LoginResultEnum.BAD_CREDENTIALS);
|
||||
@@ -116,6 +132,39 @@ public class AdminAuthServiceImpl implements AdminAuthService {
|
||||
return user;
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验地理位置
|
||||
* 省市正确或地区代码对上了就校验通过
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private boolean checkGeo(OrganizationDO organizationDO, String ip) {
|
||||
// 检查组织的地理信息
|
||||
if (ObjectUtil.isAllEmpty(organizationDO.getProvince(), organizationDO.getCity(), organizationDO.getAreaCode())) {
|
||||
throw ServiceExceptionUtil.exception(ErrorCodeConstants.AUTH_LOGIN_ORG_GEO_EMPTY);
|
||||
}
|
||||
|
||||
IPQueryDataRespDTO ipQueryDataRespDTO = ipQueryService.querySource(ip);
|
||||
|
||||
// 省名称和市名称包含了ip定位服务返回的省和市即位置正确
|
||||
boolean provinceAndCityMatch = false;
|
||||
if (StrUtils.contains(organizationDO.getProvince(), ipQueryDataRespDTO.getProv()) && StrUtils.contains(organizationDO.getCity(), ipQueryDataRespDTO.getCity())) {
|
||||
provinceAndCityMatch = true;
|
||||
}
|
||||
|
||||
// 行政区编码等于ip定位服务返回的编码即位置正确
|
||||
boolean areaCodeMatch = false;
|
||||
if (ObjectUtil.equal(organizationDO.getAreaCode(), ipQueryDataRespDTO.getPostCode())) {
|
||||
areaCodeMatch = true;
|
||||
}
|
||||
|
||||
if (provinceAndCityMatch || areaCodeMatch) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public AuthLoginRespVO login(AuthLoginReqVO reqVO) {
|
||||
// 校验验证码
|
||||
|
||||
Reference in New Issue
Block a user