基于PropertyPlaceholderConfigurer对properties文件加密解密

出于对安全的考虑,需要对项目里面的.properties文件中的数据进行加密,确保项目被黑客攻击后任然无法得到敏感数据。Spring3框架提供了统一加载属性文件的方法PropertyPlaceholderConfigurer,所以最好可以在这个加载之前实现对.properties文件中的属性进行加密/解密处理。

配置文件中的属性最初始时敏感属性值可以在上线之前加密明文得到密文,程序加载后读取到的是密文,需要在PropertyPlaceholderConfigurer读取之前解密。

编写一个继承类,继承PropertyPlaceholderConfigurer,对密文的属性进行解密。DESUtil.java工具类可以参考https://mp.csdn.net/mdeditor/79815134

转载请注明:https://blog.csdn.net/qfashly/article/details/79815473

public class EncryptProperties extends PropertyPlaceholderConfigurer {
    @Override
    protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException {
        DESUtil des = new DESUtil(DESUtil.KEY_ARR[0]);
        Enumeration<Object> eKeys = props.keys();
        Object ooKey = null;
        String pKey = null;
        String pValue = null;
        while (eKeys.hasMoreElements()) {
            ooKey = eKeys.nextElement();
            if (ooKey == null) {
                continue;
            }
            pKey = String.valueOf(ooKey);
            pValue = props.getProperty(pKey);
            if (pValue == null) {
                continue;
            }
            pValue = pValue.trim();
            //对明文加密
            if (pValue.startsWith("{DES}")) { //如果是以 {DES} 开头的, 则认为是加密数据
                try {
                    props.setProperty(pKey, des.decryptStr(pValue.substring(5, pValue.length())));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            //对密文解密
            /*if(!pValue.startsWith("{DES}")){ //如果是以 {DES} 开头的, 则认为是加密数据
                try {
                    props.setProperty(pKey, des.encryptStr(pValue));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }*/
        }
        super.processProperties(beanFactoryToProcess, props);
    }
}

在applicationContext.xml加上

<bean id="propertyConfig" class="com.fw.core.server.ds.config.EncryptProperties">
    <property name="locations">
        <list>
            <value>classpath:jdbc.properties</value>
            <value>classpath:system.properties</value>
        </list>
    </property>
</bean>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值