【改进】APP访问统计增加地址
This commit is contained in:
@@ -46,9 +46,11 @@ dependencies {
|
|||||||
testImplementation 'org.springframework.boot:spring-boot-starter-test' // 测试框架
|
testImplementation 'org.springframework.boot:spring-boot-starter-test' // 测试框架
|
||||||
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' // thymeleaf模版引擎
|
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' // thymeleaf模版引擎
|
||||||
implementation 'com.aliyun.oss:aliyun-sdk-oss:3.18.3' // OSS SDK
|
implementation 'com.aliyun.oss:aliyun-sdk-oss:3.18.3' // OSS SDK
|
||||||
implementation 'cn.hutool:hutool-all:5.8.40' // hutool工具包
|
implementation 'cn.hutool:hutool-all:5.8.42' // hutool工具包
|
||||||
implementation 'io.springfox:springfox-swagger2:2.10.5'
|
implementation 'io.springfox:springfox-swagger2:2.10.5'
|
||||||
implementation 'com.github.xiaoymin:knife4j-openapi2-spring-boot-starter:4.4.0'
|
implementation 'com.github.xiaoymin:knife4j-openapi2-spring-boot-starter:4.4.0'
|
||||||
|
implementation 'org.lionsoul:ip2region:2.7.0'
|
||||||
|
implementation 'jakarta.servlet:jakarta.servlet-api:5.0.0'
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,6 +33,9 @@ public class AppAccessStatistics extends CommonEntity {
|
|||||||
@ApiModelProperty("访问IP")
|
@ApiModelProperty("访问IP")
|
||||||
private String accessIp;
|
private String accessIp;
|
||||||
|
|
||||||
|
@ApiModelProperty("访问地址")
|
||||||
|
private String accessAddress;
|
||||||
|
|
||||||
@ApiModelProperty("备注")
|
@ApiModelProperty("备注")
|
||||||
private String remark;
|
private String remark;
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import com.corewing.app.dto.AccessStatisticsRequest;
|
|||||||
import com.corewing.app.entity.AppAccessStatistics;
|
import com.corewing.app.entity.AppAccessStatistics;
|
||||||
import com.corewing.app.mapper.AppAccessStatisticsMapper;
|
import com.corewing.app.mapper.AppAccessStatisticsMapper;
|
||||||
import com.corewing.app.service.AppAccessStatisticsService;
|
import com.corewing.app.service.AppAccessStatisticsService;
|
||||||
|
import com.corewing.app.util.CommonIpAddressUtil;
|
||||||
import com.corewing.app.util.CommonServletUtil;
|
import com.corewing.app.util.CommonServletUtil;
|
||||||
import com.corewing.app.util.IpUtil;
|
import com.corewing.app.util.IpUtil;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
@@ -33,6 +34,7 @@ public class AppAccessStatisticsServiceImpl extends ServiceImpl<AppAccessStatist
|
|||||||
}
|
}
|
||||||
appAccessStatistics.setAccessDevice(accessStatisticsRequest.getAccessDevice());
|
appAccessStatistics.setAccessDevice(accessStatisticsRequest.getAccessDevice());
|
||||||
appAccessStatistics.setAccessIp(IpUtil.getClientIp(CommonServletUtil.getRequest()));
|
appAccessStatistics.setAccessIp(IpUtil.getClientIp(CommonServletUtil.getRequest()));
|
||||||
|
appAccessStatistics.setAccessAddress(CommonIpAddressUtil.getCityInfo(appAccessStatistics.getAccessIp()));
|
||||||
return save(appAccessStatistics);
|
return save(appAccessStatistics);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
94
src/main/java/com/corewing/app/util/CommonIpAddressUtil.java
Normal file
94
src/main/java/com/corewing/app/util/CommonIpAddressUtil.java
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
|
||||||
|
package com.corewing.app.util;
|
||||||
|
|
||||||
|
import cn.hutool.core.io.FileUtil;
|
||||||
|
import cn.hutool.core.net.Ipv4Util;
|
||||||
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
|
import cn.hutool.extra.servlet.JakartaServletUtil;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.lionsoul.ip2region.xdb.Searcher;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据ip地址定位工具类,离线方式
|
||||||
|
* 参考地址:<a href="https://gitee.com/lionsoul/ip2region/tree/master/binding/java">ip2region</a>
|
||||||
|
*
|
||||||
|
* @author xuyuxiang
|
||||||
|
* @date 2020/3/16 11:25
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public class CommonIpAddressUtil {
|
||||||
|
|
||||||
|
private static final String LOCAL_REMOTE_HOST = "0:0:0:0:0:0:0:1";
|
||||||
|
|
||||||
|
private static final String IP2REGION_DB_PATH = "/ip2region.xdb";
|
||||||
|
|
||||||
|
private static final Searcher searcher = initSearcher();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 私有构造函数防止实例化
|
||||||
|
*/
|
||||||
|
private CommonIpAddressUtil() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 初始化Searcher实例
|
||||||
|
*
|
||||||
|
* @return Searcher
|
||||||
|
*/
|
||||||
|
private static Searcher initSearcher() {
|
||||||
|
try {
|
||||||
|
final File dbFile = FileUtil.file(FileUtil.getTmpDir() + FileUtil.FILE_SEPARATOR + IP2REGION_DB_PATH);
|
||||||
|
if (!FileUtil.exist(dbFile)) {
|
||||||
|
try (InputStream inputStream = CommonIpAddressUtil.class.getResourceAsStream(IP2REGION_DB_PATH)) {
|
||||||
|
FileUtil.writeFromStream(inputStream, dbFile);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 1、从 dbPath 加载整个 xdb 到内存。
|
||||||
|
final byte[] cBuff = Searcher.loadContentFromFile(dbFile.getPath());
|
||||||
|
// 2、使用上述的 cBuff 创建一个完全基于内存的查询对象。
|
||||||
|
return Searcher.newWithBuffer(cBuff);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error(">>> CommonIpAddressUtil初始化异常:", e);
|
||||||
|
throw new RuntimeException("CommonIpAddressUtil初始化异常");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取客户端ip
|
||||||
|
*
|
||||||
|
* @author xuyuxiang
|
||||||
|
* @date 2020/3/19 9:32
|
||||||
|
*/
|
||||||
|
public static String getIp(HttpServletRequest request) {
|
||||||
|
if (ObjectUtil.isEmpty(request)) {
|
||||||
|
return Ipv4Util.LOCAL_IP;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
String remoteHost = JakartaServletUtil.getClientIP(request);
|
||||||
|
return LOCAL_REMOTE_HOST.equals(remoteHost) ? Ipv4Util.LOCAL_IP : remoteHost;
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error(">>> 获取客户端ip异常:", e);
|
||||||
|
return Ipv4Util.LOCAL_IP;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据IP地址离线获取城市
|
||||||
|
*
|
||||||
|
* @author xuyuxiang
|
||||||
|
* @date 2022/4/27 23:14
|
||||||
|
*/
|
||||||
|
public static String getCityInfo(String ip) {
|
||||||
|
try {
|
||||||
|
// 3、执行查询
|
||||||
|
String region = searcher.search(ip.trim());
|
||||||
|
return region.replace("0|", "").replace("|0", "");
|
||||||
|
} catch (Exception e) {
|
||||||
|
return "未知";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
BIN
src/main/resources/ip2region.xdb
Normal file
BIN
src/main/resources/ip2region.xdb
Normal file
Binary file not shown.
Reference in New Issue
Block a user