feat(安全): 内部令牌支持过渡期新旧两把并行接受
换 AIIMAGE_INTERNAL_TOKEN 会让没升级的老客户端立刻全部失败(这把令牌有 4 个客户端字面量 点位并已分发到用户机器)。照仓内 shop-credential-key-previous 的惯例加 internal-token-previous: 逗号分隔的旧值与当前值并入同一接受集合,置空即关窗。 原先 8 处各自持有 @Value 字段并重复实现比较,其中 2 处用的是 equals 而非常量时间比较; 判断收进 AdminAuthSupport.isTrustedSuppliedToken 一处,controller 改为委派。
This commit is contained in:
+3
-45
@@ -43,11 +43,7 @@ import com.nanri.aiimage.common.exception.BusinessException;
|
||||
@Tag(name = "菜单权限管理", description = "统一维护软件端与后台管理端的菜单权限配置")
|
||||
public class PermissionMenuController {
|
||||
|
||||
@Value("${aiimage.security.internal-token:}")
|
||||
private String internalToken;
|
||||
|
||||
@Value("${aiimage.security.internal-token-file:}")
|
||||
private String internalTokenFile;
|
||||
|
||||
private final AdminAuthSupport adminAuthSupport;
|
||||
private final PermissionMenuService permissionMenuService;
|
||||
@@ -229,47 +225,9 @@ public class PermissionMenuController {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
private boolean isTrustedInternalRequest(HttpServletRequest request, String suppliedToken) {
|
||||
String expectedToken = resolveExpectedInternalToken();
|
||||
if (expectedToken.isBlank() || suppliedToken == null || suppliedToken.isBlank()) {
|
||||
return false;
|
||||
}
|
||||
return expectedToken.equals(suppliedToken);
|
||||
}
|
||||
|
||||
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();
|
||||
return adminAuthSupport.isTrustedSuppliedToken(suppliedToken);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user