SpringBoot項目application.yml文件數據庫配置密碼加密的方法
在Spring boot開發中,需要在application.yml文件里配置數據庫的連接信息,或者在啟動時傳入數據庫密碼,如果不加密,傳明文,數據庫就直接暴露了,相當于'裸奔'了,因此需要進行加密處理才行。
使用@SpringBootApplication注解啟動的項目,只需增加maven依賴
我們對信息加解密是使用這個jar包的:
編寫加解密測試類:
package cn.linjk.ehome; import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;import org.jasypt.encryption.pbe.config.EnvironmentPBEConfig;import org.junit.Test; public class JasyptTest { @Test public void testEncrypt() throws Exception { StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor(); EnvironmentPBEConfig config = new EnvironmentPBEConfig(); config.setAlgorithm('PBEWithMD5AndDES'); // 加密的算法,這個算法是默認的 config.setPassword('test'); // 加密的密鑰 standardPBEStringEncryptor.setConfig(config); String plainText = '88888888'; String encryptedText = standardPBEStringEncryptor.encrypt(plainText); System.out.println(encryptedText); } @Test public void testDe() throws Exception { StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor(); EnvironmentPBEConfig config = new EnvironmentPBEConfig(); config.setAlgorithm('PBEWithMD5AndDES'); config.setPassword('test'); standardPBEStringEncryptor.setConfig(config); String encryptedText = 'ip10XNIEfAMTGQLdqt87XnLRsshu0rf0'; String plainText = standardPBEStringEncryptor.decrypt(encryptedText); System.out.println(plainText); }}
加密串拿到了,現在來修改application.yml的配置:
我們把加密串放在ENC({加密串})即可。
啟動時需要配置 秘鑰
將秘鑰加入啟動參數
到此這篇關于SpringBoot項目application.yml文件數據庫配置密碼加密的方法的文章就介紹到這了,更多相關SpringBoot application.yml數據庫加密內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章:
