离线定位fix
This commit is contained in:
@@ -33,7 +33,11 @@ public class GlobalExceptionHandler {
|
|||||||
*/
|
*/
|
||||||
@ExceptionHandler(ServiceException.class)
|
@ExceptionHandler(ServiceException.class)
|
||||||
public R handleServiceException(ServiceException e, HttpServletRequest request) {
|
public R handleServiceException(ServiceException e, HttpServletRequest request) {
|
||||||
|
if ("token异常".equals(e.getMessage())) {
|
||||||
|
log.error("用户token异常");
|
||||||
|
} else {
|
||||||
log.error(e.getMessage(), e);
|
log.error(e.getMessage(), e);
|
||||||
|
}
|
||||||
Integer code = e.getCode();
|
Integer code = e.getCode();
|
||||||
return StringUtils.isNotNull(code) ? R.error(code, e.getMessage()) : R.error(e.getMessage());
|
return StringUtils.isNotNull(code) ? R.error(code, e.getMessage()) : R.error(e.getMessage());
|
||||||
}
|
}
|
||||||
@@ -105,7 +109,12 @@ public class GlobalExceptionHandler {
|
|||||||
*/
|
*/
|
||||||
@ExceptionHandler(CustomException.class)
|
@ExceptionHandler(CustomException.class)
|
||||||
public R customException(CustomException e) {
|
public R customException(CustomException e) {
|
||||||
|
if ("token异常".equals(e.getMessage())) {
|
||||||
|
log.error("用户token异常");
|
||||||
|
} else {
|
||||||
log.error(e.getMessage(), e);
|
log.error(e.getMessage(), e);
|
||||||
|
}
|
||||||
|
|
||||||
return R.error(e.getMessage());
|
return R.error(e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -81,8 +81,16 @@ public class JwtAuthenticationTokenFilter extends OncePerRequestFilter {
|
|||||||
@Override
|
@Override
|
||||||
protected void doFilterInternal(@NotNull HttpServletRequest httpServletRequest, @NotNull HttpServletResponse httpServletResponse, @NotNull FilterChain filterChain) throws ServletException, IOException {
|
protected void doFilterInternal(@NotNull HttpServletRequest httpServletRequest, @NotNull HttpServletResponse httpServletResponse, @NotNull FilterChain filterChain) throws ServletException, IOException {
|
||||||
log.info("url = {}", httpServletRequest.getRequestURI());
|
log.info("url = {}", httpServletRequest.getRequestURI());
|
||||||
|
|
||||||
|
// //微信支付回调接口,不需要验证
|
||||||
|
if ("/wx/pay/jsCallback".equals(httpServletRequest.getServletPath())) {
|
||||||
|
filterChain.doFilter(httpServletRequest, httpServletResponse);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// 微信公众号的请求,必须存在tenantkey,否则抛出异常
|
// 微信公众号的请求,必须存在tenantkey,否则抛出异常
|
||||||
if (httpServletRequest.getServletPath().startsWith("/wx/")) {
|
if (httpServletRequest.getServletPath().startsWith("/wx/")) {
|
||||||
|
|
||||||
String tenantKey = httpServletRequest.getHeader("tenantkey");
|
String tenantKey = httpServletRequest.getHeader("tenantkey");
|
||||||
if (StrUtil.isBlank(tenantKey)) {
|
if (StrUtil.isBlank(tenantKey)) {
|
||||||
resolver.resolveException(httpServletRequest, httpServletResponse, null, new CustomException("tenantkey不能为空"));
|
resolver.resolveException(httpServletRequest, httpServletResponse, null, new CustomException("tenantkey不能为空"));
|
||||||
|
|||||||
BIN
play-admin/src/main/resources/ip2region/ip2region.db
Normal file
BIN
play-admin/src/main/resources/ip2region/ip2region.db
Normal file
Binary file not shown.
@@ -104,6 +104,12 @@
|
|||||||
<artifactId>gson</artifactId>
|
<artifactId>gson</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.lionsoul</groupId>
|
||||||
|
<artifactId>ip2region</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.alibaba</groupId>
|
<groupId>com.alibaba</groupId>
|
||||||
<artifactId>transmittable-thread-local</artifactId>
|
<artifactId>transmittable-thread-local</artifactId>
|
||||||
|
|||||||
@@ -0,0 +1,106 @@
|
|||||||
|
package com.starry.common.utils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author admin
|
||||||
|
* @since 2024/7/25 下午3:15
|
||||||
|
**/
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.apache.commons.io.FileUtils;
|
||||||
|
import org.lionsoul.ip2region.DataBlock;
|
||||||
|
import org.lionsoul.ip2region.DbConfig;
|
||||||
|
import org.lionsoul.ip2region.DbSearcher;
|
||||||
|
import org.lionsoul.ip2region.Util;
|
||||||
|
import org.springframework.core.io.ClassPathResource;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据ip离线查询地址
|
||||||
|
*
|
||||||
|
* @author admin
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public class RegionUtils {
|
||||||
|
|
||||||
|
private static final String JAVA_TEMP_DIR = "java.io.tmpdir";
|
||||||
|
|
||||||
|
static DbConfig config = null;
|
||||||
|
static DbSearcher searcher = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 初始化IP库
|
||||||
|
*/
|
||||||
|
static {
|
||||||
|
try {
|
||||||
|
// 因为jar无法读取文件,复制创建临时文件
|
||||||
|
String dbPath = Objects.requireNonNull(RegionUtils.class.getResource("/ip2region/ip2region.db")).getPath();
|
||||||
|
File file = new File(dbPath);
|
||||||
|
if (!file.exists()) {
|
||||||
|
String tmpDir = System.getProperties().getProperty(JAVA_TEMP_DIR);
|
||||||
|
dbPath = tmpDir + "ip2region.db";
|
||||||
|
file = new File(dbPath);
|
||||||
|
ClassPathResource cpr = new ClassPathResource("ip2region" + File.separator + "ip2region.db");
|
||||||
|
InputStream resourceAsStream = cpr.getInputStream();
|
||||||
|
FileUtils.copyInputStreamToFile(resourceAsStream, file);
|
||||||
|
}
|
||||||
|
config = new DbConfig();
|
||||||
|
searcher = new DbSearcher(config, dbPath);
|
||||||
|
log.info("bean [{}]", config);
|
||||||
|
log.info("bean [{}]", searcher);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("init ip region error:", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解析IP
|
||||||
|
*
|
||||||
|
* @param ip
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static String getRegion(String ip) {
|
||||||
|
try {
|
||||||
|
// db
|
||||||
|
if (searcher == null || StringUtils.isEmpty(ip)) {
|
||||||
|
log.error("DbSearcher is null");
|
||||||
|
return StringUtils.EMPTY;
|
||||||
|
}
|
||||||
|
long startTime = System.currentTimeMillis();
|
||||||
|
// 查询算法
|
||||||
|
int algorithm = DbSearcher.MEMORY_ALGORITYM;
|
||||||
|
Method method = null;
|
||||||
|
switch (algorithm) {
|
||||||
|
case DbSearcher.BTREE_ALGORITHM:
|
||||||
|
method = searcher.getClass().getMethod("btreeSearch", String.class);
|
||||||
|
break;
|
||||||
|
case DbSearcher.BINARY_ALGORITHM:
|
||||||
|
method = searcher.getClass().getMethod("binarySearch", String.class);
|
||||||
|
break;
|
||||||
|
case DbSearcher.MEMORY_ALGORITYM:
|
||||||
|
method = searcher.getClass().getMethod("memorySearch", String.class);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return StringUtils.EMPTY;
|
||||||
|
}
|
||||||
|
|
||||||
|
DataBlock dataBlock = null;
|
||||||
|
if (!Util.isIpAddress(ip)) {
|
||||||
|
log.warn("warning: Invalid ip address");
|
||||||
|
}
|
||||||
|
dataBlock = (DataBlock) method.invoke(searcher, ip);
|
||||||
|
String result = dataBlock.getRegion();
|
||||||
|
long endTime = System.currentTimeMillis();
|
||||||
|
log.debug("region use time[{}] result[{}]", endTime - startTime, result);
|
||||||
|
return result;
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("error:", e);
|
||||||
|
}
|
||||||
|
return StringUtils.EMPTY;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,9 +1,7 @@
|
|||||||
package com.starry.common.utils.ip;
|
package com.starry.common.utils.ip;
|
||||||
|
|
||||||
|
|
||||||
import com.alibaba.fastjson2.JSON;
|
import com.starry.common.utils.RegionUtils;
|
||||||
import com.alibaba.fastjson2.JSONObject;
|
|
||||||
import com.starry.common.utils.HttpUtils;
|
|
||||||
import com.starry.common.utils.StringUtils;
|
import com.starry.common.utils.StringUtils;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
@@ -27,21 +25,20 @@ public class AddressUtils {
|
|||||||
public static String getRealAddressByIp(String ip) {
|
public static String getRealAddressByIp(String ip) {
|
||||||
if ("127.0.0.1".equals(ip)) {
|
if ("127.0.0.1".equals(ip)) {
|
||||||
return "内网IP";
|
return "内网IP";
|
||||||
} else {
|
}
|
||||||
try {
|
try {
|
||||||
String rspStr = HttpUtils.sendGet(IP_URL, "ip=" + ip + "&json=true", "GBK");
|
String rspStr = RegionUtils.getRegion(ip);
|
||||||
if (StringUtils.isEmpty(rspStr)) {
|
if (StringUtils.isEmpty(rspStr)) {
|
||||||
log.error("获取地理位置异常 {}", ip);
|
log.error("获取地理位置异常 {}", ip);
|
||||||
return UNKNOWN;
|
return UNKNOWN;
|
||||||
}
|
}
|
||||||
JSONObject obj = JSON.parseObject(rspStr);
|
String[] obj = rspStr.split("\\|");
|
||||||
String region = obj.getString("pro");
|
String region = obj[2];
|
||||||
String city = obj.getString("city");
|
String city = obj[3];
|
||||||
return String.format("%s %s", region, city);
|
return String.format("%s %s", region, city);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("获取地理位置异常 {}", ip);
|
log.error("获取地理位置异常", e);
|
||||||
}
|
}
|
||||||
return UNKNOWN;
|
return UNKNOWN;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
8
pom.xml
8
pom.xml
@@ -228,6 +228,14 @@
|
|||||||
<version>3.4.0</version>
|
<version>3.4.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- https://mvnrepository.com/artifact/org.lionsoul/ip2region -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.lionsoul</groupId>
|
||||||
|
<artifactId>ip2region</artifactId>
|
||||||
|
<version>1.7.2</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|||||||
Reference in New Issue
Block a user