uri风控添加本地缓存
This commit is contained in:
@@ -31,6 +31,12 @@
|
|||||||
<artifactId>xtools-app-sys-api</artifactId>
|
<artifactId>xtools-app-sys-api</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!-- 项目模块 end -->
|
<!-- 项目模块 end -->
|
||||||
|
|
||||||
|
<!-- 本地缓存 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.github.ben-manes.caffeine</groupId>
|
||||||
|
<artifactId>caffeine</artifactId>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
@@ -1,17 +1,15 @@
|
|||||||
package xtools.app.sys.risk;
|
package xtools.app.sys.risk;
|
||||||
|
|
||||||
import jakarta.annotation.Resource;
|
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import xtools.app.sys.api.SysRiskApi;
|
import xtools.app.sys.api.SysRiskApi;
|
||||||
import xtools.app.sys.enums.SysRiskType;
|
import xtools.app.sys.enums.SysRiskType;
|
||||||
import xtools.app.sys.model.dto.resp.SysRiskResp;
|
import xtools.app.sys.model.dto.resp.SysRiskResp;
|
||||||
|
import xtools.app.sys.risk.utils.RiskCaffeineUtils;
|
||||||
import xtools.app.sys.risk.utils.UriRiskUtils;
|
import xtools.app.sys.risk.utils.UriRiskUtils;
|
||||||
import xtools.base.config.BaseParams;
|
import xtools.base.config.BaseParams;
|
||||||
import xtools.boot.cache.redis.base.RedisService;
|
|
||||||
import xtools.core.CollectionUtils;
|
import xtools.core.CollectionUtils;
|
||||||
import xtools.core.StringUtils;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -33,9 +31,6 @@ public class UriRisk implements BaseParams {
|
|||||||
|
|
||||||
private final SysRiskApi sysRiskApi;
|
private final SysRiskApi sysRiskApi;
|
||||||
|
|
||||||
@Resource
|
|
||||||
private RedisService redisService;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 初始化
|
* 初始化
|
||||||
*/
|
*/
|
||||||
@@ -60,13 +55,9 @@ public class UriRisk implements BaseParams {
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public boolean risk(String sysType, String uri) {
|
public boolean risk(String sysType, String uri) {
|
||||||
String cache = redisService.get(UriRiskUtils.getKey(sysType), String.class);
|
List<String> cache = RiskCaffeineUtils.getUriRisk(sysType);
|
||||||
if (StringUtils.isBlank(cache)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
String[] arr = cache.split(CP_COMMA);
|
|
||||||
// 匹配uri
|
// 匹配uri
|
||||||
for (String str : arr) {
|
for (String str : cache) {
|
||||||
if (uri.contains(str)) {
|
if (uri.contains(str)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,59 @@
|
|||||||
|
package xtools.app.sys.risk.utils;
|
||||||
|
|
||||||
|
import com.github.benmanes.caffeine.cache.Cache;
|
||||||
|
import com.github.benmanes.caffeine.cache.Caffeine;
|
||||||
|
import xtools.base.config.BaseParams;
|
||||||
|
import xtools.boot.cache.redis.utils.RedisUtils;
|
||||||
|
import xtools.core.ArrUtils;
|
||||||
|
import xtools.core.StringUtils;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>Title : RiskCaffeineUtils</p>
|
||||||
|
* <p>Description : RiskCaffeineUtils</p>
|
||||||
|
* <p>DevelopTools : Idea_x64_v2026.1</p>
|
||||||
|
* <p>DevelopSystem : Windows11</p>
|
||||||
|
* <p>Company : org.xujun</p>
|
||||||
|
*
|
||||||
|
* @author : XuJun
|
||||||
|
* @version : 1.0.0
|
||||||
|
* @date : 2026/4/25 14:28
|
||||||
|
*/
|
||||||
|
public class RiskCaffeineUtils implements BaseParams {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* URI风控缓存
|
||||||
|
*/
|
||||||
|
private static final Cache<String, List<String>> URI_RISK;
|
||||||
|
|
||||||
|
// 初始化
|
||||||
|
static {
|
||||||
|
URI_RISK = Caffeine.newBuilder().expireAfterWrite(CP_NUM20, TimeUnit.SECONDS).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取URI风控缓存
|
||||||
|
*
|
||||||
|
* @param sysType 系统类型
|
||||||
|
* @return URI风控缓存
|
||||||
|
*/
|
||||||
|
public static List<String> getUriRisk(String sysType) {
|
||||||
|
List<String> data = URI_RISK.getIfPresent(sysType);
|
||||||
|
if (Objects.nonNull(data)) {
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
String cache = RedisUtils.get().get(UriRiskUtils.getKey(sysType), String.class);
|
||||||
|
List<String> riskList;
|
||||||
|
if (StringUtils.isNotBlank(cache)) {
|
||||||
|
riskList = ArrUtils.toStringList(cache);
|
||||||
|
} else {
|
||||||
|
riskList = new ArrayList<>();
|
||||||
|
}
|
||||||
|
URI_RISK.put(sysType, riskList);
|
||||||
|
return riskList;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user