feat(安全): 内部令牌支持过渡期新旧两把并行接受
换 AIIMAGE_INTERNAL_TOKEN 会让没升级的老客户端立刻全部失败(这把令牌有 4 个客户端字面量 点位并已分发到用户机器)。照仓内 shop-credential-key-previous 的惯例加 internal-token-previous: 逗号分隔的旧值与当前值并入同一接受集合,置空即关窗。 原先 8 处各自持有 @Value 字段并重复实现比较,其中 2 处用的是 equals 而非常量时间比较; 判断收进 AdminAuthSupport.isTrustedSuppliedToken 一处,controller 改为委派。
This commit is contained in:
+56
-21
@@ -1,5 +1,7 @@
|
||||
package com.nanri.aiimage.common.security;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.common.security.JwtService;
|
||||
import com.nanri.aiimage.common.security.DeviceSessionPolicy;
|
||||
@@ -35,6 +37,10 @@ public class AdminAuthSupport {
|
||||
@Value("${aiimage.security.internal-token-file:}")
|
||||
private String internalTokenFile;
|
||||
|
||||
/** 轮换过渡期用:逗号分隔的旧令牌,新旧同时接受;旧客户端铺完(或确认无残留)后置空。 */
|
||||
@Value("${aiimage.security.internal-token-previous:}")
|
||||
private String internalTokenPrevious;
|
||||
|
||||
/** 解析当前请求的用户;token 缺失或无效抛 401。 */
|
||||
public AdminUserEntity requireUser(HttpServletRequest request) {
|
||||
String token = resolveToken(request);
|
||||
@@ -170,15 +176,58 @@ public class AdminAuthSupport {
|
||||
}
|
||||
|
||||
private boolean isTrustedInternalRequest(HttpServletRequest request) {
|
||||
String suppliedToken = request.getHeader("X-Internal-Token");
|
||||
String expectedToken = resolveExpectedInternalToken();
|
||||
if (expectedToken.isBlank() || suppliedToken == null || suppliedToken.isBlank()) {
|
||||
return isTrustedSuppliedToken(request.getHeader("X-Internal-Token"));
|
||||
}
|
||||
|
||||
/**
|
||||
* 只判断令牌本身:命中当前配置值(配置为空时退到令牌文件)、或过渡期旧值之一即可信。
|
||||
* 一个期望值都没配时一律拒绝,免得属性漂成空串就把内部端点变成匿名可达。
|
||||
* 各 controller 请复用这里,别再各自复制一份比较逻辑 —— 轮换时漏一处就是半条链路 401。
|
||||
*/
|
||||
public boolean isTrustedSuppliedToken(String suppliedToken) {
|
||||
if (suppliedToken == null || suppliedToken.isBlank()) {
|
||||
return false;
|
||||
}
|
||||
// 常量时间比较:逐字节 equals 可被计时侧信道逐位试探出内部令牌
|
||||
return java.security.MessageDigest.isEqual(
|
||||
expectedToken.getBytes(java.nio.charset.StandardCharsets.UTF_8),
|
||||
suppliedToken.trim().getBytes(java.nio.charset.StandardCharsets.UTF_8));
|
||||
byte[] supplied = suppliedToken.trim().getBytes(StandardCharsets.UTF_8);
|
||||
for (String expected : acceptedInternalTokens()) {
|
||||
// 常量时间比较:逐字节 equals 可被计时侧信道逐位试探出内部令牌
|
||||
if (java.security.MessageDigest.isEqual(
|
||||
expected.getBytes(StandardCharsets.UTF_8), supplied)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/** 当前值优先;当前值为空才用令牌文件;过渡期旧值(逗号分隔)始终并入,重复值剔除。 */
|
||||
private List<String> acceptedInternalTokens() {
|
||||
List<String> accepted = new ArrayList<>();
|
||||
String configured = internalToken == null ? "" : internalToken.trim();
|
||||
if (configured.isEmpty()) {
|
||||
configured = readInternalTokenFile().trim();
|
||||
}
|
||||
if (!configured.isEmpty()) {
|
||||
accepted.add(configured);
|
||||
}
|
||||
for (String previous : (internalTokenPrevious == null ? "" : internalTokenPrevious).split(",")) {
|
||||
String old = previous.trim();
|
||||
if (!old.isEmpty() && !old.equals(configured) && !accepted.contains(old)) {
|
||||
accepted.add(old);
|
||||
}
|
||||
}
|
||||
return accepted;
|
||||
}
|
||||
|
||||
private String readInternalTokenFile() {
|
||||
Path path = resolveInternalTokenFile();
|
||||
if (path == null || !Files.isRegularFile(path)) {
|
||||
return "";
|
||||
}
|
||||
try {
|
||||
return Files.readString(path, StandardCharsets.UTF_8).trim();
|
||||
} catch (Exception ignored) {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -191,20 +240,6 @@ public class AdminAuthSupport {
|
||||
return isTrustedInternalRequest(request);
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
Reference in New Issue
Block a user