Spring加載加密的配置文件詳解
本文實(shí)例為大家分享了Spring加載加密的配置文件,供大家參考,具體內(nèi)容如下
一、繼承并實(shí)現(xiàn)自己的屬性文件配置器類
/** * 帶加密的Spring屬性配置文件擴(kuò)展類 * 加密方式: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==
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaEE簡介_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了JavaEE簡介,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07SpringCloud zookeeper作為注冊中心使用介紹
ZooKeeper由雅虎研究院開發(fā),是Google Chubby的開源實(shí)現(xiàn),后來托管到Apache,于2010年11月正式成為Apache的頂級(jí)項(xiàng)目。ZooKeeper是一個(gè)經(jīng)典的分布式數(shù)據(jù)一致性解決方案,致力于為分布式應(yīng)用提供一個(gè)高性能、高可用,且具有嚴(yán)格順序訪問控制能力的分布式協(xié)調(diào)服務(wù)2022-11-11詳解springboot如何更新json串里面的內(nèi)容
這篇文章主要為大家介紹了springboot 如何更新json串里面的內(nèi)容,文中有詳細(xì)的解決方案供大家參考,對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-10-10SpringBoot 如何自定義請(qǐng)求參數(shù)校驗(yàn)
這篇文章主要介紹了SpringBoot 如何自定義請(qǐng)求參數(shù)校驗(yàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10關(guān)于FastJson?long?溢出問題的小結(jié)
這篇文章主要介紹了關(guān)于FastJson?long?溢出問題的小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。2022-01-01