spring加密properties中的密码

假设我的密码是db2,我先用密码类的encrypt()方法将这个密码加密

接下来我的做法是将这些密码加密后用{{}}包围起来, properties中这样写

jdbc.driverClassName=com.ibm.db2.jcc.DB2Driver
jdbc.url=jdbc:db2://192.168.1.101:50000/XXXX
jdbc.username=db2inst1
jdbc.password={{9432E7976A6BBF9E}}
然后使用自定义的PlaceholderConfigurer.,在其中将其解密以便application能正常使用, applicationContext.xml配置如下:
<bean id="propertyPlaceholderConfigurer"
      class="com.xxx.util.EncryptedPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:xxxx.properties</value>
        </list>
    </property>
</bean>

java代码如下:

/**
 * Created by cyper on 1/21/15.
 */
public class EncryptedPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
    public static final String PREFIX = "{{";

    @Override
    protected String convertPropertyValue(String originalValue) {
        if (originalValue.startsWith(PREFIX)) {
            logger.debug("Find encrypted value:" + originalValue);
            String newValue = originalValue.substring(2, originalValue.length() - 2);
            newValue = DES.decrypt(newValue);
            return newValue;
        }

        return originalValue;
    }
}
其中的DES类可以替换成任意的可逆加密算法类。

这样做能在一定程度上使你的应用程序更安全(简单的文件扫描或肉眼观察并不能直接得到密码,需要找到解密类进行解密)

参考:http://kayalvizhiameen.blogspot.in/2014/04/handling-obfuscated-property-values-in.html?view=classic, 比文中的简单.

写完后发现这里有一篇:http://my.oschina.net/noahxiao/blog/163719 不过感觉太长了,还是我的简单。 :D

转载于:https://my.oschina.net/uniquejava/blog/370360

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值