温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Spring加载加密的配置文件详解

发布时间:2020-10-05 03:22:04 来源:脚本之家 阅读:233 作者:二十六度半 栏目:编程语言

本文实例为大家分享了Spring加载加密的配置文件,供大家参考,具体内容如下

一、继承并实现自己的属性文件配置器类

 /** * 带加密的Spring属性配置文件扩展类 * 加密方式:AES * @author simon * */ public class EncryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer { //指定需要加密的属性 private String[] propertyNames = {"db.password"}; /** * 解密指定propertyName的属性值 * @param propertyName * @param propertyValue * @return */ @Override protected String convertProperty(String propertyName, String propertyValue) { //过滤出需要解密的属性 for (String p : propertyNames) { if (p.equalsIgnoreCase(propertyName)) { try { //返回AES解密后的字符串 return new String(SymmetricCryptoUtil.decryptAESWithDefaultKey(EncodeUtil.decodeBase64(propertyValue))); } catch (Exception e) { e.printStackTrace(); } } } return super.convertProperty(propertyName, propertyValue); } } 

二、Spring中配置以自定义的属性文件配置器类来加载加密后的配置文件

 <!-- 加载加密后的配置文件 --> <bean class="com.bounter.mybatis.extension.EncryptPropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:db.properties</value> </list> </property> </bean> 

三、将配置文件中的特殊属性用相同的算法和密钥加密

 db.driver= db.url= db.username=root #AES encrypt,Base64 encode db.password=jFYmt2f57RHhzItYDhWiSA==

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI