改造背景:项目漏洞修复
改造目的:将jdbc.properties中的password加密展示,禁止明文展示
改造步骤:1、新建加密解密类。加密方式根据情况采用,3des或者MD5等
2、重写PropertyPlaceHolderConfigurer的convertProperties方法
public class PropertyPlaceHolder extends PropertyPlaceHolderConfigurer{
@Override
protected void convertProperties(Properties props){
super.convertProperties(props);
Enumeration<?> propertyNames = props.propertyNames();
while(propertyNames.hasMoreElements()){
String propertyName=propertyNames.nextElement();
String propertyValue=props.getProperty(propertyName);
String converedValue=convertPropertyValue(propertyValue);
//对名字中含有password字符串的密码进行解密,该处判断根据实际情况,变换字符串内容
if(propertyName.contains("password")){
//实例化加密类
DES des = new DES();
converedValue=des.strDec(converedValue,des .key1,des.key2,des.key3);
}
props.setProperty(propertyName,converedValue);
}
}
}
在appliocationcontext.xml中配置读取properties的配置
<bean id="placeholderConfigurer"
class="PropertyPlaceHolder 类路径">
<property name="locations"/>
<list>
<value>classpath*:jdbc.properties</value>
<value>classpath*:upload.properties</value>
</list>
</property>
<property name ="fileEncoding" value="UTF-8"/>
</bean>
在jdbc.prperties配置文件中将password的value值使用加密串即可