由于项目的需要,要求对配置文件中的敏感信息进行加密处理,例如数据库用户名密码之类的
处理方式如下:
MyWebConstant
/**
* 对外接口,管理公共常量
* 对应properties中的key值
* @author Administrator
*
*/
public class MyWebConstant {
public static final String JDBC_DATASOURCE_DRIVERCLASSNAME_KEY = "jdbc.driverClassName";
public static final String JDBC_DATASOURCE_URL_KEY = "jdbc.url";
public static final String JDBC_DATASOURCE_USERNAME_KEY = "jdbc.username";
public static final String JDBC_DATASOURCE_PASSWORD_KEY = "jdbc.password";
}
EncryptablePropertyPlaceholderConfigurer.java
import java.util.Properties;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
/**
* 配置文件解密核心类
* 作为自定义类加到配置文件中,解密使用
* @author Administrator
*
*/
public class EncryptablePropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
protected void processProperties(ConfigurableListableBeanFactory beanFactory, Properties props)
throws BeansException {
try {
// DesEncrypt des = new DesEncrypt();
String username = props.getProperty(MyWebConstant.JDBC_DATASOURCE_USERNAME_KEY);
if (username != null) {
props.setProperty(MyWebConstant.JDBC_DATASOURCE_USERNAME_KEY,
DesEncrypt