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";
     
    }


五、加密算法

可以用自己想用的加密算法!
 

发布了47 篇原创文章 · 获赞 38 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/Summer_And_Opencv/article/details/103072164
今日推荐