Spring加载Properties配置文件的加密解密处理

版权声明:本文为博主原创文章,欢迎转载,转载请注明作者 https://blog.csdn.net/iamlake/article/details/51556915

需求场景:加密Properties配置文件中的数据库连接字串和用户名、密码

实现思路:重写PropertyPlaceholderConfigurer类中的processProperties方法,在读取配置信息之前实现解密


一、PropertyPlaceholderConfigurerExt.java

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 com.neusoft.icelake.common.constant.PropertyConstant;

/**
 * <br>Title:PropertyPlaceholderConfigurerExt
 * <br>Description:PropertyPlaceholderConfigurer扩展
 * <br>Date:2016-6-1
 */
public class PropertyPlaceholderConfigurerExt extends PropertyPlaceholderConfigurer {
    protected void processProperties(ConfigurableListableBeanFactory beanFactory, Properties props)
            throws BeansException {
        try {
            String jdbc_url = props.getProperty(PropertyConstant.JDBC_URL);
            if (jdbc_url != null)
                props.setProperty(PropertyConstant.JDBC_URL, CryptoUtil.decrypt(jdbc_url));

            String jdbc_username = props.getProperty(PropertyConstant.JDBC_USERNAME);
            if (jdbc_username != null)
                props.setProperty(PropertyConstant.JDBC_USERNAME, CryptoUtil.decrypt(jdbc_username));

            String jdbc_password = props.getProperty(PropertyConstant.JDBC_PASSWORD);
            if (jdbc_password != null)
                props.setProperty(PropertyConstant.JDBC_PASSWORD, CryptoUtil.decrypt(jdbc_password));
            super.processProperties(beanFactory, props);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BeanInitializationException(e.getMessage());
        }
    }
}


二、spring-context-mybatis.xml

	<!--  
	<context:property-placeholder ignore-unresolvable="true" location="classpath:config.properties" />
	-->
    <bean id="propertyConfigurer" class="com.neusoft.icelake.common.util.PropertyPlaceholderConfigurerExt">  
            <property name="locations">
                <list>
                    <value>classpath:config.properties</value>
                </list>
            </property>
    </bean>

三、config.properties

#===== Database sttings =====#
#sybase database setting
jdbc.type=sybase
jdbc.driver=com.sybase.jdbc2.jdbc.SybDriver
#jdbc.url=jdbc:sybase:Tds:erp08.minmetals.com.cn:5000/standard?charset=cp936
#jdbc.username=STT
#jdbc.password=ststst

jdbc.url=954215B307C15FB87A194CA77F27EC96C7AE6A3352F85136ECCC621EB64C2D76F6B82F91A554F6F4CF5AF36C4128E989D45B6CE0296D2946C776071E401A409A2CF96945BF9FC5FF
jdbc.username=F7B511DD811821ED
jdbc.password=428A99F379BE4883

#jdbc.testSql=SELECT 'x'
jdbc.testSql=select getdate()

四、PropertyConstant.java

public class PropertyConstant {
    /**
     * <br>Description:数据库连接字串
     * <br>Date:2016-6-1
     */
    public static final String JDBC_URL = "jdbc.url";

    /**
     * <br>Description:登录名
     * <br>Date:2016-6-1
     */
    public static final String JDBC_USERNAME = "jdbc.username";

    /**
     * <br>Description:密码
     * <br>Date:2016-6-1
     */
    public static final String JDBC_PASSWORD = "jdbc.password";

}

五、加密算法

就不公开了

猜你喜欢

转载自blog.csdn.net/iamlake/article/details/51556915
今日推荐