feat(安全): 内部令牌支持过渡期新旧两把并行接受

换 AIIMAGE_INTERNAL_TOKEN 会让没升级的老客户端立刻全部失败(这把令牌有 4 个客户端字面量
点位并已分发到用户机器)。照仓内 shop-credential-key-previous 的惯例加 internal-token-previous:
逗号分隔的旧值与当前值并入同一接受集合,置空即关窗。

原先 8 处各自持有 @Value 字段并重复实现比较,其中 2 处用的是 equals 而非常量时间比较;
判断收进 AdminAuthSupport.isTrustedSuppliedToken 一处,controller 改为委派。
This commit is contained in:
2026-10-03 01:49:13 +08:00
parent 005cda9379
commit 4f03ea1808
13 changed files with 191 additions and 371 deletions
@@ -66,11 +66,7 @@ public class DedupeTotalDataController {
private final AdminAuthSupport adminAuthSupport;
private final PermissionMenuService permissionMenuService;
@Value("${aiimage.security.internal-token:}")
private String internalToken;
@Value("${aiimage.security.internal-token-file:}")
private String internalTokenFile;
@GetMapping
@Operation(summary = "分页查询总数据", description = "分页查询数据去重总数据,支持按值模糊搜索。")
@@ -268,50 +264,12 @@ public class DedupeTotalDataController {
}
}
private boolean isTrustedInternalRequest(String suppliedToken) {
String expectedToken = resolveExpectedInternalToken();
return !expectedToken.isBlank() && suppliedToken != null && !suppliedToken.isBlank()
&& MessageDigest.isEqual(
expectedToken.getBytes(StandardCharsets.UTF_8),
suppliedToken.trim().getBytes(StandardCharsets.UTF_8));
}
private String resolveExpectedInternalToken() {
if (internalToken != null && !internalToken.isBlank()) {
return internalToken.trim();
}
Path path = resolveInternalTokenFile();
if (path == null || !Files.isRegularFile(path)) {
return "";
}
try {
return Files.readString(path, StandardCharsets.UTF_8).trim();
} catch (Exception ignored) {
return "";
}
}
private Path resolveInternalTokenFile() {
String configuredPath = internalTokenFile == null ? "" : internalTokenFile.trim();
if (!configuredPath.isEmpty()) {
if (configuredPath.equals("~") || configuredPath.startsWith("~/") || configuredPath.startsWith("~\\")) {
String userHome = System.getProperty("user.home", "").trim();
if (userHome.isEmpty()) {
return null;
}
configuredPath = configuredPath.length() == 1
? userHome
: Path.of(userHome, configuredPath.substring(2)).toString();
}
Path configuredTokenPath = Path.of(configuredPath);
return configuredTokenPath.isAbsolute() ? configuredTokenPath.normalize() : null;
}
String userHome = System.getProperty("user.home", "").trim();
return userHome.isEmpty()
? null
: Path.of(userHome, ".aiimage", "internal-token").toAbsolutePath().normalize();
}
private record RequestOperator(Long id, boolean superAdmin) {
}
private boolean isTrustedInternalRequest(String suppliedToken) {
return adminAuthSupport.isTrustedSuppliedToken(suppliedToken);
}
}