Spring Boot項目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({加密串})即可。

..........................................

啓動時需要配置 祕鑰

將祕鑰加入啓動參數

發佈了106 篇原創文章 · 獲贊 44 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章