数据库用户名和密码加密
配置文件
去db-context.xml页面先配置上这个类EncryptPropertyPlaceholderConfigurer类是需要新建的
<bean class="test.startup.EncryptPropertyPlaceholderConfigurer" p:locations="classpath*:*.properties"></bean>
没有这个p标签的也需要添加上
xmlns:p="http://www.springframework.org/schema/p
EncryptPropertyPlaceholderConfigurer类的代码
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
*
* @Description 数据库加密
*/
public class EncryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer{
// 属性需与配置文件的KEY保持一致 加密名单
private String encryptPropName ="灵活配置需要加密的字段";
//加密状态是否已加密 0-否 1-是
private String isencoder = "1";
/**
* @param propertyName 属性名称
* @param propertyValue 属性值
*/
@Override
protected String convertProperty(String propertyName, String propertyValue) {
//加密状态是否已加密 0-否 1-是
if(("1").equals(isencoder)){
// 如果在加密属性名单中发现该属性
if (isEncryptProp(propertyName)) {
//解密或者其他方式
String decryptValue = DES.decoder(propertyValue);
return decryptValue;
} else {
return propertyValue;
}
}else{
return propertyValue;
}
}
/**
*
* @Description 判断是否在加密的名单里
* @param propertyName
*/
private boolean isEncryptProp(String propertyName) {
String[] encryptPropNames = encryptPropName.split(",");
for (String encryptName : encryptPropNames) {
if (encryptName.equals(propertyName)) {
return true;
}
}
return false;
}
}