fix: 修复快照风险规则串站问题
buildSnapshot() 原先仅按 siteType 查询风险规则,导致同类型站点共享同一套规则。 改为优先按 siteId 查找站点专属规则,无则 fallback 到 siteType 兜底。 同时用 selectOne+limit 1 替代 selectList 优化性能,并增加 debug 日志。
This commit is contained in:
@@ -100,23 +100,37 @@ public class SiteSnapshotServiceImpl implements SiteSnapshotService {
|
||||
snapshot.put("pagesSource", pages.containsKey("source") ? pages.get("source") : "custom");
|
||||
}
|
||||
|
||||
// 3. 风险规则
|
||||
if (siteType != null) {
|
||||
var rules = riskRuleMapper.selectList(
|
||||
// 3. 风险规则(优先按站点专属规则查找,无则 fallback 到站点类型规则)
|
||||
RiskRule matchedRule = null;
|
||||
// 3a. 按站点 ID 查找专属规则
|
||||
if (siteId != null) {
|
||||
matchedRule = riskRuleMapper.selectOne(
|
||||
new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<RiskRule>()
|
||||
.eq(RiskRule::getSiteId, siteId)
|
||||
.eq(RiskRule::getDeleted, 0)
|
||||
.orderByDesc(RiskRule::getId)
|
||||
.last("limit 1"));
|
||||
}
|
||||
// 3b. 无专属规则时 fallback 到站点类型规则
|
||||
if (matchedRule == null && siteType != null) {
|
||||
matchedRule = riskRuleMapper.selectOne(
|
||||
new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<RiskRule>()
|
||||
.eq(RiskRule::getSiteType, siteType)
|
||||
.eq(RiskRule::getDeleted, 0)
|
||||
.orderByDesc(RiskRule::getId));
|
||||
if (!rules.isEmpty()) {
|
||||
var latestRule = rules.get(0);
|
||||
.orderByDesc(RiskRule::getId)
|
||||
.last("limit 1"));
|
||||
}
|
||||
if (matchedRule != null) {
|
||||
try {
|
||||
List<Map<String, Object>> rulesList = om.readValue(latestRule.getRulesJson(),
|
||||
List<Map<String, Object>> rulesList = om.readValue(matchedRule.getRulesJson(),
|
||||
new TypeReference<List<Map<String, Object>>>() {});
|
||||
snapshot.put("riskRules", rulesList);
|
||||
snapshot.put("riskRulesVersion", latestRule.getVersion());
|
||||
snapshot.put("riskRulesVersion", matchedRule.getVersion());
|
||||
log.debug("快照风险规则: siteId={} ruleSource={} ruleVersion={}", siteId,
|
||||
matchedRule.getSiteId() != null && matchedRule.getSiteId().equals(siteId) ? "site" : "siteType",
|
||||
matchedRule.getVersion());
|
||||
} catch (Exception ignored) {}
|
||||
}
|
||||
}
|
||||
|
||||
// 只要有任一配置(脚本/页面/规则)即视为有效快照
|
||||
return (!snapshot.containsKey("script") && !snapshot.containsKey("pages") && !snapshot.containsKey("riskRules"))
|
||||
|
||||
Reference in New Issue
Block a user