spring配置文件加密
- 2015-10-29 10:59:00
- admin 原创
- 2294
<!--配置文件-->
<!-- 对JDBC配置进行解密--><beanid="propertyConfigurer"class="xxx.security.EncryptablePropertyPlaceholderConfigurer"><propertyname="locations"><list><value>classpath:conf/jdbc.properties</value><value>classpath:conf/memcache.properties</value></list></property></bean>
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;
import cn.com.dbappsecurity.common.utils.DesEncrypt;
import cn.com.dbappsecurity.common.utils.MyWebConstant;
public class EncryptablePropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
private static final String key = MyWebConstant.JDBC_DESC_KEY;
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.Decrypt(username, DesEncrypt.hex2byte(key)));
}
String password = props.getProperty(MyWebConstant.JDBC_DATASOURCE_PASSWORD_KEY);
if (password != null) {
props.setProperty(MyWebConstant.JDBC_DATASOURCE_PASSWORD_KEY, DesEncrypt.Decrypt(password, DesEncrypt.hex2byte(key)));
}
String url = props.getProperty(MyWebConstant.JDBC_DATASOURCE_URL_KEY);
if (url != null) {
props.setProperty(MyWebConstant.JDBC_DATASOURCE_URL_KEY, DesEncrypt.Decrypt(url, DesEncrypt.hex2byte(key)));
}
String driverClassName = props.getProperty(MyWebConstant.JDBC_DATASOURCE_DRIVERCLASSNAME_KEY);
if(driverClassName != null){
props.setProperty(MyWebConstant.JDBC_DATASOURCE_DRIVERCLASSNAME_KEY, DesEncrypt.Decrypt(driverClassName, DesEncrypt.hex2byte(key)));
}
String dbtype = props.getProperty(MyWebConstant.JDBC_DATASOURCE_TYPE_KEY);
if(dbtype != null){
props.setProperty(MyWebConstant.JDBC_DATASOURCE_TYPE_KEY, DesEncrypt.Decrypt(dbtype, DesEncrypt.hex2byte(key)));
}
super.processProperties(beanFactory, props);
} catch (Exception e) {
e.printStackTrace();
throw new BeanInitializationException(e.getMessage());
}
}
}
联系人: | 郑州-小万 |
---|---|
电话: | 13803993919 |
Email: | 1027060531@qq.com |
QQ: | 1027060531 |
网址: | www.wanhejia.com |