2013-12-17 116 views
1

我使用spring-PropertySourcesPlaceholderConfigurer公开的最新属性加载机制,并尝试解密加密属性。将PropertyPlaceholderConfigurer我可以覆盖resolvePlaceholder,并将解密机制添加到它。弹簧属性加载 - 动态解密加密属性

但是,我不知道如何在PropertySourcesPlaceholderConfigurer中做到这一点。已经尝试覆盖processProperties,并传入我的自定义ConfigurablePropertyResolver [与getProperty Overriden]但这不起作用。

MutablePropertySources propertySources = new MutablePropertySources(); 

try { 
    propertySources.addFirst(getExtendPortsProperties()); 
    propertySources.addFirst(new ResourcePropertySource("classpath:/X.properties")); 
    propertySources.addFirst(new ResourcePropertySource("classpath:/Y.properties")); 
} catch (IOException e) { 
    throw new RuntimeException("failed to read property source", e); 
} 

这就是我读取属性的方法。然后,

PropertySourcesPlaceholderConfigurer result = new PropertySourcesPlaceholderConfigurer(); 
result.setPropertySources(firePropertiesSources); 

这就是我设置的属性。现在

,在这些道具的属性之一是加密的,我需要指示春天解密与实际值替换占位符之前。[我需要指定自定义实现]

将是巨大的如果有人能帮助我。

回答

0

而不是让你自己,你看看jasypt?它具有您需要的确切功能。看看http://www.jasypt.org/spring31.html

+0

谢谢大卫。这符合我的目的。 – karansardana

+1

我已经将它添加到了我的项目中,而且它似乎在做它的工作;但是,如果有例外,它不会给我正确的信息。例如 - ,如果pwd加密不正确,jasypt应该给我一个有意义的 - Encyption ***异常,但它不会。我正在使用“EncryptablePropertiesPropertySource”进行解密。我将所有文件添加为EncryptablePropertiesPropertySource,然后使用PropertySourcesPlaceholderConfigurer。任何指针? – karansardana