您的位置:首页 > 编程语言 > Java开发

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

2016-11-08 11:17 183 查看
参考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());

        }

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐