众所周知,在实际项目中都会加密数据源,防止密码泄露,网上有很多教程,现在将最常用的一种进行总结归纳。
Spring中加载properties文件使用下面的配置。
<context:property-placeholder location="classpath:jdbc.properties"/>
对加密的信息我们可以使用Spring中PropertyPlaceholderConfigurer 的 convertProperty方法来进行解密
public class EncryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
@Override
protected String convertProperty(String propertyName, String propertyValue) {
String decryptValue = DESUtil.getDecryptString(propertyValue);
return decryptValue;
}
}
从上面的方法可以看出,spring是通过该方法返回properties属性值的。
最后需要在配置文件中指定需要解密的properties文件
<bean class="com.qin.config.EncryptPropertyPlaceholderConfigurer " p:location="classpath:jdbc.properties"/>
或
<bean class="com.qin.config.EncryptPropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
这样就做完了。