SpringProperties文件加密

众所周知,在实际项目中都会加密数据源,防止密码泄露,网上有很多教程,现在将最常用的一种进行总结归纳。
Spring中加载properties文件使用下面的配置。

<context:property-placeholder location="classpath:jdbc.properties"/>  

对加密的信息我们可以使用Spring中PropertyPlaceholderConfigurer 的 convertProperty方法来进行解密

  public class EncryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
	@Override
    protected String convertProperty(String propertyName, String propertyValue) {
            String decryptValue = DESUtil.getDecryptString(propertyValue);
            return decryptValue;
    }
  }
  从上面的方法可以看出,spring是通过该方法返回properties属性值的。

最后需要在配置文件中指定需要解密的properties文件

<bean class="com.qin.config.EncryptPropertyPlaceholderConfigurer "   p:location="classpath:jdbc.properties"/>  
或
<bean class="com.qin.config.EncryptPropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:jdbc.properties</value>
            </list>
        </property>
    </bean>

这样就做完了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值