我有一个Spring boot Application和一个属性文件 config.properties ,其中的值已加密(请参见下面的示例)
config.properties
myapp.property1={5AES123}SafeR70/wqqwwqwqwqwqsdaWQmNs+O2afeIU/1MHoCWvTgxUYA30C/rrei4\=
myapp.property2={5AES342}MareV70/PLNqsasasaa*ksueoHH+O2afeIU/1MHoCWvTgxUYJQ30C/7rei4\=
myapp.property3={5AES111}TutoV10/xdtghshI5CVULQ7uevr+O2afeIU/1MHoCWvTgxUYJQ30C/1rei4\=
我正在使用特殊的API(作为我应用程序上的POM依赖项添加)来解密这些值。
请在下面找到 PSEUDOCODE ,以更好地解释我的意图和最终希望得到的东西。
public static void main(String[] args) {
// 1. decrypt the properties values of the config.properties using my special API package.
List<MyPropDecrypted> myPropDecryptedLst = mySpecialAPIPack.decrpyt("config.properties");
// 2. get the spring context
myAppSpringContext = getSpringContext();
//3. add the decrypted properties to the spring context from step 2.
int idx = 0;
for (MyPropDecrypted myPropDecrypted : myPropDecryptedLst){
idx++;
myAppSpringContext.setProperty("myapp.property"+idx, myPropDecrypted.getDecrypteValue();
}
SpringApplication.run(Application.class, args);
}
我的问题是我如何以编程方式将那些解密的属性(使用我的特殊API)添加/注入到spring上下文中,以便像从属性文件中加载的属性一样使用它(@Value(“ $ {myapp.property1 }“))?
答案 0 :(得分:1)
您可以使用Spring Cloud Config并实现自定义EnvironmentRepository bean,如下所述:
自定义复合环境存储库 除了使用Spring Cloud中的一个环境存储库之外,您还可以提供自己的EnvironmentRepository bean,以将其作为复合环境的一部分包括在内。为此,您的bean必须实现EnvironmentRepository接口。如果要在复合环境中控制自定义EnvironmentRepository的优先级,则还应该实现Ordered接口并重写getOrdered方法。如果未实现Ordered接口,则EnvironmentRepository的优先级最低。
答案 1 :(得分:1)
我建议您创建这样的配置类:
@ConfigurationProperties
@Configuration
class MyConfig{
public Map<String,String > myapp;
public String getproperty1() {
return myapp.get("property1");
}
public String getProperty2() {
//
}
//getters and setter for all the three properties
}
现在更新您的PSEUDO代码以使用setter方法设置解密值。然后,您可以将MyConfig类插入其他任何类中并获取值。