首先配置文件中
<!-- 加密时候使用 -->
<bean id="propertyConfig" class="sunwin.yog.dao.DecodePropertyConfigurer">
<property name="locations">
<list>
<value>classpath:config.properties</value>
</list>
</property>
</bean>
实现自定义DecodePropertyConfigurer类
/**
* 扩展 配置文件数据库用户名,密码解密
*/
public class DecodePropertyConfigurer extends PropertyPlaceholderConfigurer {
private Logger log = Logger.getLogger(DecodePropertyConfigurer.class);
@Override
protected void processProperties(ConfigurableListableBeanFactory beanFactory, Properties props){
try{
String password = props.getProperty("jdbc.password");
//解密jdbc.password属性值,并重新设置
props.setProperty("jdbc.password", EncodeUtil.decode(password));
super.processProperties(beanFactory, props);
}catch (Exception e){
log.error(e+"\nDecrypt Error");
e.printStackTrace();
}
}
}

本文介绍了一种在Spring框架中对配置文件内的敏感信息,如数据库密码进行解密的方法。通过扩展PropertyPlaceholderConfigurer类创建DecodePropertyConfigurer,实现了对特定属性的解密,确保了应用的安全性。
867

被折叠的 条评论
为什么被折叠?



