spring 数据库用户名密码加密解密

来源:互联网 发布:微信美工工资一般多少 编辑:程序博客网 时间:2024/04/30 16:15

参考http://blog.csdn.net/zhangt85/article/details/42122311



<bean id="propertyConfigurer"
class="com.pingan.ecological.util.EncryptPropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:*.properties</value>
</list>
</property>
<property name="fileEncoding" value="GBK" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>



EncryptPropertyPlaceholderConfigurer.java

import java.util.Properties;


import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
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;


public class EncryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {


    private static Logger logger = LogManager.getLogger(EncryptPropertyPlaceholderConfigurer.class);


    protected void processProperties(ConfigurableListableBeanFactory beanFactory, Properties props)
            throws BeansException {
        try {


            String passwordKey = "jdbc.password";
            String password = props.getProperty(passwordKey);
            if (password != null) {
                props.setProperty(passwordKey, DESUtils.getDecryptString(password));
            }


            super.processProperties(beanFactory, props);
        } catch (Exception e) {


            logger.error("processProperties", e);


            throw new BeanInitializationException(e.getMessage());
        }
    }
}


0 0