perf(用户密钥): 探测与告警按密钥值收敛,共用同一把 key 的账号不再各付一次提取费

实测 314 条配置只有 33 种密钥(一把平均挂 9.5 个账号):定时巡检按「模块+密文」分组后一轮只
真实探测一次;后台「立即检测该用户」的 90s 新鲜期缓存键去掉 uid、改为按密钥值指纹共享,
管理员连点 N 个共用 key 的账号只付 1 次费。管理员侧告警按密钥组合并成一条(受众仍按数据权限
过滤,正文不列 uid——同 key 账号常分属不同主管),单账号组保持原文案与原去重键。
This commit is contained in:
2026-10-02 09:21:25 +08:00
parent 5ad0f5f685
commit a4391de6fa
5 changed files with 418 additions and 52 deletions
@@ -11,6 +11,7 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
@@ -73,6 +74,37 @@ public class NotificationDispatchService {
return pushed;
}
/**
* 一条事件发给「能看到任一所列用户」的管理员:同一 dedupeKeyBase 下每个管理员只落一条。
* 用于一把密钥被多个账号共用时的告警收敛——按主体逐个调用 {@code pushToAdmins} 会让同一个
* 管理员收到 N 条同内容通知,这里改成每个管理员最多一条,同时保留数据权限边界。
*/
public int pushToAdminsAnyOf(AdminAudience audience, String scene, String level, String title, String content,
String dedupeKeyBase, Collection<Long> subjectUserIds) {
int pushed = 0;
int filtered = 0;
for (AdminUserEntity admin : audience.admins()) {
boolean visible = false;
for (Long subjectUserId : subjectUserIds) {
if (audience.canReceive(admin.getId(), subjectUserId)) {
visible = true;
break;
}
}
if (!visible) {
filtered++;
continue;
}
if (notificationService.pushOrRefresh(admin.getId(), NotificationService.AUDIENCE_ADMIN,
scene, level, title, content, dedupeKeyBase + ":" + admin.getId())) {
pushed++;
}
}
log.info("[notification] 管理员分发(合并)完成 scene={} 涉及用户数={} 受众={} 权限过滤={} 落库={}",
scene, subjectUserIds.size(), audience.admins().size(), filtered, pushed);
return pushed;
}
/**
* 「任务成功,但有一部分行没跑出来」通知:整任务没失败、铃铛里的 task_failed 只覆盖终态失败,
* 这类部分缺失过去对用户完全不可见(品牌检测的查询失败行只躺在结果文件的一张子表里)。
@@ -114,6 +114,7 @@ public class AdminUserApiSecretController {
userSecretProperties.getCheckMaxRows(), userSecretProperties.getCheckBudgetMinutes());
return ApiResponse.success("巡检完成", Map.of(
"checked", summary.checked(),
"probed", summary.probed(),
"passed", summary.passed(),
"failed", summary.failed(),
"errors", summary.errors(),
@@ -11,9 +11,9 @@ import java.time.Duration;
/**
* 用户密钥连通性巡检:默认每隔一天 04:30(Asia/Shanghai)跑一轮
* (代理模块每次巡检真实提取一次,按次计费;2026-09-14 成本审查后由
* 每日降为隔日),双实例经 Redis 分布式锁互斥;单轮受条数与时间预算
* 约束,超出顺延下一轮。
* (代理模块每次探测都是真实提取、按次计费,故同一把密钥被多个账号配置时一轮只探测一次;
* 2026-09-14 成本审查后巡检频次由每日降为隔日),双实例经 Redis 分布式锁互斥;
* 单轮受条数与时间预算约束,超出顺延下一轮。
*/
@Service
@Slf4j
@@ -42,8 +42,9 @@ public class UserApiSecretCheckScheduler {
properties.getCheckMaxRows(), properties.getCheckBudgetMinutes());
UserApiSecretService.CheckSummary summary = userApiSecretService.checkAllForScheduler(
properties.getCheckMaxRows(), properties.getCheckBudgetMinutes());
log.info("[user-secret] 每日巡检完成 checked={} passed={} failed={} errors={} skipped={}",
summary.checked(), summary.passed(), summary.failed(), summary.errors(), summary.skipped());
log.info("[user-secret] 每日巡检完成 checked={} probed={} passed={} failed={} errors={} skipped={}",
summary.checked(), summary.probed(), summary.passed(), summary.failed(),
summary.errors(), summary.skipped());
} catch (Exception ex) {
log.warn("[user-secret] 每日巡检异常终止 err={}", ex.getMessage(), ex);
}
@@ -282,14 +282,14 @@ public class UserApiSecretService implements UserSecretCleanupPort {
} else if (module == UserSecretModule.PROXY) {
validateProxyValue(plainKey);
}
UserApiSecretCheckService.CheckOutcome outcome = reuseFreshOutcome(userId, module, plainKey);
UserApiSecretCheckService.CheckOutcome outcome = reuseFreshOutcome(module, plainKey);
if (outcome != null) {
log.info("[user-secret] 新鲜期内重复检测相同配置,复用上次结果(代理按次计费,避免连点重复提取)userId={} module={}",
userId, module.key());
} else {
outcome = checkService.probe(module, plainKey);
if (UserApiSecretCheckService.STATUS_PASSED.equals(outcome.status())) {
rememberFreshOutcome(userId, module, plainKey, outcome);
rememberFreshOutcome(module, plainKey, outcome);
}
}
UserApiSecretCheckResultVo vo = toCheckResult(module, outcome);
@@ -522,17 +522,24 @@ public class UserApiSecretService implements UserSecretCleanupPort {
}
/**
* 定时巡检:遍历全部密钥逐条探测并更新状态。
* 单轮受 maxRows 与时间预算约束,超出部分顺延下一轮;单条异常不影响整轮。
* 定时巡检:按「模块 + 密文」分组探测,同一把密钥被多个账号配置时一轮只真实探测一次,
* 结论回写组内全部配置行。代理探测每次都是真实提取(按次计费)、LLM 检测每次都在烧用户额度,
* 逐账号重复探测同一把密钥纯属浪费(2026-10-01 实测:314 条配置实为 33 种密钥)。
* 单轮受 maxRows(按配置行计)与时间预算约束,超出部分顺延下一轮;单条异常不影响整轮。
*/
public CheckSummary checkAllForScheduler(int maxRows, int budgetMinutes) {
long deadline = System.currentTimeMillis() + Duration.ofMinutes(Math.max(1, budgetMinutes)).toMillis();
int checked = 0;
int probed = 0;
int passed = 0;
int failed = 0;
int errors = 0;
int skipped = 0;
int notifySent = 0;
// 密文可作分组键的前提:ShopCredentialCryptoService 用 AES/ECB 无 IV,同明文必同密文
Map<String, UserApiSecretCheckService.CheckOutcome> outcomeBySecret = new HashMap<>();
// 管理员告警按密钥分组收敛:一把 key 挂了,组内 N 个账号只推一条(用户侧仍逐账号通知)
Map<String, SecretAlertGroup> adminAlertGroups = new LinkedHashMap<>();
// 管理员受众整轮复用:轮内管理员/分组几乎不变,避免每条异常都重新解析
NotificationDispatchService.AdminAudience adminAudience =
userSecretProperties.isNotifyEnabled() ? notificationDispatchService.prepareAdminAudience() : null;
@@ -557,20 +564,25 @@ public class UserApiSecretService implements UserSecretCleanupPort {
continue;
}
try {
UserApiSecretCheckService.CheckOutcome outcome;
String plainKey = normalize(cryptoService.decrypt(row.getSecretValue()));
if (plainKey.isEmpty()) {
outcome = new UserApiSecretCheckService.CheckOutcome(
UserApiSecretCheckService.STATUS_FAILED,
UserApiSecretCheckService.CODE_INVALID_KEY,
"配置内容为空,请重新配置", null, false);
} else {
outcome = checkService.probe(module.get(), plainKey);
String groupKey = inspectionGroupKey(row);
UserApiSecretCheckService.CheckOutcome outcome = outcomeBySecret.get(groupKey);
if (outcome == null) {
String plainKey = normalize(cryptoService.decrypt(row.getSecretValue()));
outcome = plainKey.isEmpty()
? new UserApiSecretCheckService.CheckOutcome(
UserApiSecretCheckService.STATUS_FAILED,
UserApiSecretCheckService.CODE_INVALID_KEY,
"配置内容为空,请重新配置", null, false)
: checkService.probe(module.get(), plainKey);
outcomeBySecret.put(groupKey, outcome);
probed++;
sleepQuietly(200L);
}
applyCheckOutcome(row.getUserId(), module.get().key(), outcome);
checked++;
if (adminAudience != null) {
notifySent += notifySecretAlert(adminAudience, row.getUserId(), module.get(), outcome);
notifySent += notifySecretUserAlert(row.getUserId(), module.get(), outcome);
collectAdminAlert(adminAlertGroups, groupKey, row.getUserId(), module.get(), outcome);
}
if (UserApiSecretCheckService.STATUS_PASSED.equals(outcome.status())) {
passed++;
@@ -584,7 +596,6 @@ public class UserApiSecretService implements UserSecretCleanupPort {
log.warn("[user-secret] 巡检单条失败 id={} userId={} module={} err={}",
row.getId(), row.getUserId(), row.getModuleKey(), ex.getMessage());
}
sleepQuietly(200L);
}
if (checked >= maxRows || System.currentTimeMillis() >= deadline) {
Long remaining = userApiSecretMapper.selectCount(new LambdaQueryWrapper<UserApiSecretEntity>()
@@ -593,12 +604,21 @@ public class UserApiSecretService implements UserSecretCleanupPort {
break;
}
}
CheckSummary summary = new CheckSummary(checked, passed, failed, errors, skipped);
log.info("[user-secret] 巡检结束 checked={} passed={} failed={} errors={} skipped={} 通知={}",
summary.checked(), summary.passed(), summary.failed(), summary.errors(), summary.skipped(), notifySent);
int adminNotifySent = dispatchAdminAlerts(adminAudience, adminAlertGroups);
CheckSummary summary = new CheckSummary(checked, probed, passed, failed, errors, skipped);
log.info("[user-secret] 巡检结束 checked={} 真实探测={} 复用={} passed={} failed={} errors={} skipped={} "
+ "用户通知={} 管理员告警组={} 管理员通知={}",
summary.checked(), summary.probed(), summary.checked() - summary.probed(),
summary.passed(), summary.failed(), summary.errors(), summary.skipped(),
notifySent, adminAlertGroups.size(), adminNotifySent);
return summary;
}
/** 巡检分组键:同模块 + 同密文视为同一把密钥。密文确定性加密,无需解密即可分组。 */
private String inspectionGroupKey(UserApiSecretEntity row) {
return row.getModuleKey() + '|' + row.getSecretValue();
}
/**
* 巡检告警判定:欠费(insufficient_balance)→ error 级 secret_balance;
* 密钥无效/被拒(invalid_key/forbidden)→ warning 级 secret_invalid;
@@ -619,10 +639,12 @@ public class UserApiSecretService implements UserSecretCleanupPort {
return null;
}
/** 巡检发现欠费/失效:推用户通知 + 按数据权限推管理员通知;同一模块每天最多各一条。返回落库条数。 */
private int notifySecretAlert(NotificationDispatchService.AdminAudience adminAudience,
Long userId, UserSecretModule module,
UserApiSecretCheckService.CheckOutcome outcome) {
/**
* 巡检发现欠费/失效:推该账号的用户通知(同一模块每天最多一条)。
* 管理员侧不在此逐条推,由 {@link #collectAdminAlert} + {@link #dispatchAdminAlerts} 按密钥合并后一次发。
*/
private int notifySecretUserAlert(Long userId, UserSecretModule module,
UserApiSecretCheckService.CheckOutcome outcome) {
SecretAlertDecision decision = decideSecretAlert(outcome);
if (decision == null) {
return 0;
@@ -639,19 +661,80 @@ public class UserApiSecretService implements UserSecretCleanupPort {
int pushed = notificationDispatchService.pushToUser(userId, decision.scene(), decision.level(),
userTitle, userContent,
decision.scene() + ":" + userId + ":" + module.key() + ":" + day) ? 1 : 0;
String username = notificationDispatchService.displayNameOf(userId);
String adminContent = "用户 " + username + "(uid=" + userId + ")的" + moduleLabel
+ (balance ? "对应服务商余额不足" : "检测失败")
+ (reason.isEmpty() ? "" : ":" + reason);
pushed += notificationDispatchService.pushToAdmins(adminAudience, decision.scene(), decision.level(),
"用户密钥异常:" + username, adminContent,
decision.scene() + "_admin:" + userId + ":" + module.key() + ":" + day, userId);
log.info("[user-secret] 巡检通知已发 userId={} module={} scene={} level={} 条数={}",
log.info("[user-secret] 巡检用户通知已发 userId={} module={} scene={} level={} 条数={}",
userId, module.key(), decision.scene(), decision.level(), pushed);
return pushed;
}
/** 把本条异常按密钥归组:同「模块 + 密文」的 N 个账号只留一条待发管理员通知。 */
private void collectAdminAlert(Map<String, SecretAlertGroup> groups, String groupKey, Long userId,
UserSecretModule module, UserApiSecretCheckService.CheckOutcome outcome) {
SecretAlertDecision decision = decideSecretAlert(outcome);
if (decision == null || userId == null) {
return;
}
groups.computeIfAbsent(groupKey, key -> new SecretAlertGroup(module, decision, outcome)).userIds.add(userId);
}
/**
* 管理员告警下发:一把密钥一条。多账号共用时正文只报「受影响账号数 + 原因」,不逐个点名——
* 同一把 key 的账号往往分属不同主管,列出他人 uid 等于把归属关系泄露给对方,
* 具体账号可在后台「用户密钥」页按状态筛出;单账号组保持原来的点名文案。
* 受众仍按数据权限过滤:只看得到组内任一号的管理员才收得到。
*/
private int dispatchAdminAlerts(NotificationDispatchService.AdminAudience audience,
Map<String, SecretAlertGroup> groups) {
if (audience == null || groups.isEmpty()) {
return 0;
}
String day = LocalDate.now().format(DateTimeFormatter.BASIC_ISO_DATE);
int pushed = 0;
for (Map.Entry<String, SecretAlertGroup> entry : groups.entrySet()) {
SecretAlertGroup group = entry.getValue();
boolean balance = NotificationService.SCENE_SECRET_BALANCE.equals(group.decision.scene());
String reason = group.outcome.message() == null ? "" : group.outcome.message().trim();
String what = group.module.label() + (balance ? "对应服务商余额不足" : "检测失败");
String title;
String content;
String dedupeBase;
if (group.userIds.size() == 1) {
Long userId = group.userIds.iterator().next();
String username = notificationDispatchService.displayNameOf(userId);
title = "用户密钥异常:" + username;
content = "用户 " + username + "(uid=" + userId + ")的" + what
+ (reason.isEmpty() ? "" : ":" + reason);
dedupeBase = group.decision.scene() + "_admin:" + userId + ":" + group.module.key();
} else {
int affected = group.userIds.size();
title = "用户密钥异常:" + what + "(" + affected + " 个账号共用同一把)";
content = group.module.label() + "有 " + affected + " 个账号共用同一把配置,"
+ (balance ? "对应服务商余额不足" : "检测失败")
+ (reason.isEmpty() ? "" : ":" + reason)
+ "。具体账号请到后台「用户密钥」页按状态筛选。";
dedupeBase = group.decision.scene() + "_admin:group:" + sha256Hex(entry.getKey())
+ ":" + group.module.key();
}
pushed += notificationDispatchService.pushToAdminsAnyOf(audience, group.decision.scene(),
group.decision.level(), title, content, dedupeBase + ":" + day, group.userIds);
}
return pushed;
}
/** 一把密钥(同模块同密文)的管理员告警分组。 */
private static final class SecretAlertGroup {
private final UserSecretModule module;
private final SecretAlertDecision decision;
private final UserApiSecretCheckService.CheckOutcome outcome;
private final Set<Long> userIds = new LinkedHashSet<>();
private SecretAlertGroup(UserSecretModule module, SecretAlertDecision decision,
UserApiSecretCheckService.CheckOutcome outcome) {
this.module = module;
this.decision = decision;
this.outcome = outcome;
}
}
/** 巡检告警判定结果(场景 + 级别)。 */
record SecretAlertDecision(String scene, String level) {
}
@@ -801,16 +884,28 @@ public class UserApiSecretService implements UserSecretCleanupPort {
* 只在 Redis 暂存里做比对,不落库、不下发。
*/
private String fingerprint(Long userId, UserSecretModule module, String plainValue) {
String hash = sha256Hex(userId + "|" + module.key() + "|" + plainValue);
if (hash.isEmpty()) {
log.warn("[user-secret] 生成检测指纹失败 userId={} module={}", userId, module.key());
}
return hash;
}
/** 新鲜期指纹:只按「模块 + 明文」,不含 uid —— 同一把密钥在任意账号上的检测结论是同一个。 */
private String sharedFingerprint(UserSecretModule module, String plainValue) {
return sha256Hex(module.key() + "|" + plainValue);
}
private String sha256Hex(String raw) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] bytes = digest.digest((userId + "|" + module.key() + "|" + plainValue).getBytes(StandardCharsets.UTF_8));
byte[] bytes = digest.digest(raw.getBytes(StandardCharsets.UTF_8));
StringBuilder hex = new StringBuilder(bytes.length * 2);
for (byte value : bytes) {
hex.append(Character.forDigit((value >> 4) & 0xF, 16)).append(Character.forDigit(value & 0xF, 16));
}
return hex.toString();
} catch (Exception ex) {
log.warn("[user-secret] 生成检测指纹失败 userId={} module={} err={}", userId, module.key(), ex.getMessage());
return "";
}
}
@@ -835,19 +930,23 @@ public class UserApiSecretService implements UserSecretCleanupPort {
}
}
/** 「重复检测」新鲜期键:按登录用户 + 模块隔离。 */
private String freshCheckKey(Long userId, UserSecretModule module) {
return FRESH_CHECK_KEY_PREFIX + ":" + userId + ":" + module.key();
/**
* 「重复检测」新鲜期键:按「模块 + 配置值指纹」隔离,不按 uid。
* 一把密钥平均被 9.5 个账号共用(2026-10-01 实测 314 条配置=33 种密钥),而代理探测每次都是真实提取计费,
* 按 uid 隔离时管理员连点 N 个共用同 key 的账号就要付 N 次;结论本身只取决于密钥,跨账号复用才不重复扣费。
*/
private String freshCheckKey(UserSecretModule module, String sharedHash) {
return FRESH_CHECK_KEY_PREFIX + ":" + module.key() + ":" + sharedHash;
}
/** 读取新鲜期结果:同值且窗口内返回上次 outcome,否则 null(走真实探测)。 */
private UserApiSecretCheckService.CheckOutcome reuseFreshOutcome(Long userId, UserSecretModule module, String plainValue) {
String hash = fingerprint(userId, module, plainValue);
private UserApiSecretCheckService.CheckOutcome reuseFreshOutcome(UserSecretModule module, String plainValue) {
String hash = sharedFingerprint(module, plainValue);
if (hash.isEmpty()) {
return null;
}
try {
String raw = stringRedisTemplate.opsForValue().get(freshCheckKey(userId, module));
String raw = stringRedisTemplate.opsForValue().get(freshCheckKey(module, hash));
if (raw == null || raw.isBlank()) {
return null;
}
@@ -858,16 +957,16 @@ public class UserApiSecretService implements UserSecretCleanupPort {
return new UserApiSecretCheckService.CheckOutcome(
record.status(), record.code(), record.message(), record.latencyMs(), false);
} catch (Exception ex) {
log.warn("[user-secret] 读取检测新鲜期结果失败(按未命中处理)userId={} module={} err={}",
userId, module.key(), ex.getMessage());
log.warn("[user-secret] 读取检测新鲜期结果失败(按未命中处理)module={} err={}",
module.key(), ex.getMessage());
return null;
}
}
/** 暂存「通过」的检测结果供新鲜期内重复检测复用;失败不暂存(允许立即重试)。 */
private void rememberFreshOutcome(Long userId, UserSecretModule module, String plainValue,
private void rememberFreshOutcome(UserSecretModule module, String plainValue,
UserApiSecretCheckService.CheckOutcome outcome) {
String hash = fingerprint(userId, module, plainValue);
String hash = sharedFingerprint(module, plainValue);
if (hash.isEmpty()) {
return;
}
@@ -875,10 +974,10 @@ public class UserApiSecretService implements UserSecretCleanupPort {
String payload = objectMapper.writeValueAsString(new PendingVerification(
hash, outcome.status(), outcome.code(),
truncate(outcome.message(), MESSAGE_MAX_LENGTH), outcome.latencyMs()));
stringRedisTemplate.opsForValue().set(freshCheckKey(userId, module), payload, FRESH_CHECK_TTL);
stringRedisTemplate.opsForValue().set(freshCheckKey(module, hash), payload, FRESH_CHECK_TTL);
} catch (Exception ex) {
log.warn("[user-secret] 暂存检测新鲜期结果失败(不影响本次检测)userId={} module={} err={}",
userId, module.key(), ex.getMessage());
log.warn("[user-secret] 暂存检测新鲜期结果失败(不影响本次检测)module={} err={}",
module.key(), ex.getMessage());
}
}
@@ -1136,6 +1235,10 @@ public class UserApiSecretService implements UserSecretCleanupPort {
}
/** 巡检统计。 */
public record CheckSummary(int checked, int passed, int failed, int errors, int skipped) {
/**
* 巡检汇总:checked 为更新过状态的配置条数,probed 为真实对外探测次数
* (同一把密钥被多个账号共用时 probed &lt; checked)。
*/
public record CheckSummary(int checked, int probed, int passed, int failed, int errors, int skipped) {
}
}