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

来源:互联网 发布:淘宝怎么调价格 编辑:程序博客网 时间:2024/06/17 00:48

需求场景:加密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 settingjdbc.type=sybasejdbc.driver=com.sybase.jdbc2.jdbc.SybDriver#jdbc.url=jdbc:sybase:Tds:erp08.minmetals.com.cn:5000/standard?charset=cp936#jdbc.username=STT#jdbc.password=stststjdbc.url=954215B307C15FB87A194CA77F27EC96C7AE6A3352F85136ECCC621EB64C2D76F6B82F91A554F6F4CF5AF36C4128E989D45B6CE0296D2946C776071E401A409A2CF96945BF9FC5FFjdbc.username=F7B511DD811821EDjdbc.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";}

五、加密算法

就不公开了

0 0
原创粉丝点击