From 8adad9fef61d1198a84d5fc6b14c7f6617b1caaf Mon Sep 17 00:00:00 2001 From: yuhaibo Date: Tue, 13 Oct 2026 12:35:35 -0700 Subject: [PATCH] =?UTF-8?q?feat:=20kk.Picturespreview=E3=80=81kk.Getcorsfi?= =?UTF-8?q?le=E3=80=81kk.addTask=20=E6=94=AF=E6=8C=81=E9=80=9A=E8=BF=87?= =?UTF-8?q?=E7=8E=AF=E5=A2=83=E5=8F=98=E9=87=8F=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 配置文件中大多数配置项使用 ${KK_XXX:默认值} 的写法,可以通过环境变量调整, 这三项写成了固定值 true,容器化部署时只能修改或挂载配置文件。 改为 ${KK_PICTURESPREVIEW:true}、${KK_GETCORSFILE:true}、${KK_ADDTASK:true}, 与其它配置项保持一致,application.properties 与 test.properties 同步修改。 默认值不变,未设置环境变量时行为与之前相同。 新增 InterfaceSwitchEnvConfigTests,校验两份配置文件中这三项可通过环境变量 调整,且默认值仍为 true。 Signed-off-by: yuhaibo --- server/src/main/config/application.properties | 6 +- server/src/main/config/test.properties | 6 +- .../config/InterfaceSwitchEnvConfigTests.java | 87 +++++++++++++++++++ 3 files changed, 93 insertions(+), 6 deletions(-) create mode 100644 server/src/test/java/cn/keking/config/InterfaceSwitchEnvConfigTests.java diff --git a/server/src/main/config/application.properties b/server/src/main/config/application.properties index 9bc1bd9e7..2104c4434 100644 --- a/server/src/main/config/application.properties +++ b/server/src/main/config/application.properties @@ -423,14 +423,14 @@ delete.source.file = ${KK_DELETE_SOURCE_FILE:true} # 是否启用图片预览权限,默认为true(启用) # 设置为false可禁用所有图片预览功能 -kk.Picturespreview = true +kk.Picturespreview = ${KK_PICTURESPREVIEW:true} # 是否启用跨域文件获取权限,默认为true(启用) -kk.Getcorsfile = true +kk.Getcorsfile = ${KK_GETCORSFILE:true} # 是否启用添加异步任务权限,默认为true(启用) # 大文件转换通常使用异步任务处理 -kk.addTask = true +kk.addTask = ${KK_ADDTASK:true} # API密钥功能,默认为false(禁用) # 启用后需要提供密钥才能调用API diff --git a/server/src/main/config/test.properties b/server/src/main/config/test.properties index 45bf7bdf2..74161467a 100644 --- a/server/src/main/config/test.properties +++ b/server/src/main/config/test.properties @@ -419,14 +419,14 @@ delete.source.file = ${KK_DELETE_SOURCE_FILE:true} # 是否启用图片预览权限,默认为true(启用) # 设置为false可禁用所有图片预览功能 -kk.Picturespreview = true +kk.Picturespreview = ${KK_PICTURESPREVIEW:true} # 是否启用跨域文件获取权限,默认为true(启用) -kk.Getcorsfile = true +kk.Getcorsfile = ${KK_GETCORSFILE:true} # 是否启用添加异步任务权限,默认为true(启用) # 大文件转换通常使用异步任务处理 -kk.addTask = true +kk.addTask = ${KK_ADDTASK:true} # API密钥功能,默认为false(禁用) # 启用后需要提供密钥才能调用API diff --git a/server/src/test/java/cn/keking/config/InterfaceSwitchEnvConfigTests.java b/server/src/test/java/cn/keking/config/InterfaceSwitchEnvConfigTests.java new file mode 100644 index 000000000..f2ee96371 --- /dev/null +++ b/server/src/test/java/cn/keking/config/InterfaceSwitchEnvConfigTests.java @@ -0,0 +1,87 @@ +package cn.keking.config; + +import cn.keking.utils.ConfigUtils; +import org.junit.jupiter.api.Test; +import org.springframework.core.env.MapPropertySource; +import org.springframework.core.env.MutablePropertySources; +import org.springframework.core.env.PropertiesPropertySource; +import org.springframework.core.env.PropertySourcesPropertyResolver; + +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; +import java.util.Map; +import java.util.Properties; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +/** + * kk.Picturespreview、kk.Getcorsfile、kk.addTask 三个开关应与其它配置项一样, + * 使用 ${KK_XXX:默认值} 的写法,可以通过环境变量调整,未设置环境变量时保持默认值 true。 + */ +class InterfaceSwitchEnvConfigTests { + + private static final String[] CONFIG_FILES = {"/application.properties", "/test.properties"}; + + private static final Map SWITCH_ENV_NAMES = Map.of( + "kk.Picturespreview", "KK_PICTURESPREVIEW", + "kk.Getcorsfile", "KK_GETCORSFILE", + "kk.addTask", "KK_ADDTASK"); + + @Test + void shouldReadInterfaceSwitchesFromEnvironmentVariables() throws IOException { + for (String configFile : CONFIG_FILES) { + Properties properties = readProperties(configFile); + SWITCH_ENV_NAMES.forEach((key, envName) -> + assertEquals("false", resolve(properties, key, Map.of(envName, "false")), + () -> key + " in " + configFile + " must be configurable through " + envName)); + } + } + + @Test + void shouldKeepInterfaceSwitchesEnabledByDefault() throws IOException { + for (String configFile : CONFIG_FILES) { + Properties properties = readProperties(configFile); + SWITCH_ENV_NAMES.forEach((key, envName) -> + assertEquals("true", resolve(properties, key, Map.of()), + () -> key + " in " + configFile + " must stay enabled by default")); + } + } + + @Test + void shouldKeepInterfaceSwitchesEnabledByDefaultWhenConfigIsReloaded() throws IOException { + for (String configFile : CONFIG_FILES) { + // 与 ConfigRefreshComponent 重新加载配置文件时的处理方式一致 + Properties properties = readProperties(configFile); + ConfigUtils.restorePropertiesFromEnvFormat(properties); + SWITCH_ENV_NAMES.forEach((key, envName) -> { + String expected = System.getenv(envName) == null ? "true" : System.getenv(envName); + assertEquals(expected, properties.getProperty(key).trim(), + () -> key + " in " + configFile + " must stay enabled by default after a reload"); + }); + } + } + + /** + * 按 Spring 的占位符规则解析配置项,environment 的优先级高于配置文件,相当于系统环境变量。 + */ + private static String resolve(Properties properties, String key, Map environment) { + MutablePropertySources propertySources = new MutablePropertySources(); + propertySources.addLast(new MapPropertySource("environment", environment)); + propertySources.addLast(new PropertiesPropertySource("config", properties)); + String value = new PropertySourcesPropertyResolver(propertySources).getProperty(key); + assertNotNull(value, () -> key + " is missing"); + return value.trim(); + } + + private Properties readProperties(String resourcePath) throws IOException { + Properties properties = new Properties(); + try (InputStream inputStream = getClass().getResourceAsStream(resourcePath)) { + assertNotNull(inputStream, () -> resourcePath + " is missing"); + properties.load(new InputStreamReader(inputStream, StandardCharsets.UTF_8)); + } + return properties; + } +}