新增:数据源预热;加密配置解密优化

This commit is contained in:
yangyang01000846
2026-01-23 17:00:35 +08:00
parent ee5a8367bc
commit 4e9b534485
3 changed files with 96 additions and 2 deletions

View File

@@ -1,13 +1,19 @@
package com.sdm.outbridge.config; //// common模块com.xxx.common.config.CommonConfig
import com.sdm.common.utils.AESUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.EnumerablePropertySource;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.io.support.DefaultPropertySourceFactory;
import org.springframework.core.io.support.EncodedResource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 加载common模块的自定义配置文件
@@ -25,7 +31,48 @@ public class CommonConfig {
}
List<org.springframework.core.env.PropertySource<?>> sources = new YamlPropertySourceLoader()
.load(resource.getResource().getFilename(), resource.getResource());
return sources.get(0);
org.springframework.core.env.PropertySource<?> originalSource = sources.get(0);
org.springframework.core.env.PropertySource<?> decryptedSource =
decryptPropertySource(originalSource);
return decryptedSource;
}
}
private static org.springframework.core.env.PropertySource<?> decryptPropertySource(org.springframework.core.env.PropertySource<?> source) {
// 只处理可枚举的属性源yml加载后的源都是EnumerablePropertySource
if (!(source instanceof EnumerablePropertySource)) {
return source;
}
EnumerablePropertySource<?> enumerableSource = (EnumerablePropertySource<?>) source;
Map<String, Object> decryptedProperties = new HashMap<>();
// 遍历所有配置项
for (String propertyName : enumerableSource.getPropertyNames()) {
Object value = enumerableSource.getProperty(propertyName);
if (value != null && value instanceof String) {
String strValue = (String) value;
if (strValue.startsWith("ENC(") && strValue.endsWith(")")) {
// 解密并替换值
String spdmEnkey = StringUtils.isBlank(System.getProperty("spdm.enkey"))?
System.getenv("spdm.enkey"):System.getProperty("spdm.enkey");
String encryptedValue = strValue.substring(4, strValue.length() - 1);
try {
decryptedProperties.put(propertyName, AESUtil.decodeNew(encryptedValue, spdmEnkey));
} catch (Exception e) {
System.out.println("利元亨现场配置解密异常:"+e.getMessage());
throw new RuntimeException(e);
}
} else {
// 非加密值,直接保留
decryptedProperties.put(propertyName, strValue);
}
} else {
// 非字符串类型,直接保留
decryptedProperties.put(propertyName, value);
}
}
// 生成新的属性源(名称和原始源一致,确保覆盖)
return new MapPropertySource(source.getName(), decryptedProperties);
}
}