今天看<<Spring3.x企业应用开发实战>>,发现spring配置中的配置属性可以加密解密。特备份如下:
1:自定义配置加载属性的java类
package com.zealot.test.placeholder;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import com.zealot.util.DES;
public class MyPropertyPlaceholderConfigurer extends
PropertyPlaceholderConfigurer {
private String [] encryptPropNames = {"username","password"};
@Override
protected String convertProperty(String propertyName, String propertyValue) {
if(isEncryptProp(propertyName))
{
String decryptValue = DES.decrypt(propertyValue);
System.out.println(propertyName+"="+decryptValue);
return decryptValue;
}
else
{
return propertyValue;
}
}
private boolean isEncryptProp(String propName)
{
for(String encryptPropName : encryptPropNames)
{
if(propName.equals(encryptPropName))
{
return true;
}
}
return false;
}
}
2:配置applicationContext.xml
<bean id="propertyConfigurer"
class="com.zealot.test.placeholder.MyPropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:system.properties</value>
</list>
</property>
</bean>