1. 程式人生 > 實用技巧 >springboot專案配置檔案中的資料庫用密文展示如何做

springboot專案配置檔案中的資料庫用密文展示如何做

1.新增依賴:

<dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>1.16</version> </dependency> 2.在配置檔案中設定加密的鹽: jasypt.encryptor.password:test1234 3.使用工具類:
import org.jasypt.util.text.BasicTextEncryptor;

/** * 處理加密/解密資料的工具 * * @author zwq */ public class JasyptEncryptUtil { public static void main(String[] args){ BasicTextEncryptor textEncryptor = new BasicTextEncryptor(); //加密所需的salt(鹽),注意要與 配置檔案中設定jasypt.encryptor.password相同 String salt = "test1234"; //需要加密的資料 String data = "password"; textEncryptor.setPassword(salt);
//加密資料 String value = textEncryptor.encrypt(data); //加密的結果直接放入 application.yml中,注意加密後的資料要用按照 ENC(valaue) 的樣式 System.out.println("加密結果:"+value); //解密資料 //String value = textEncryptor.decrypt(""); } }

4.修改配置檔案中的資料:將第3步中列印的資料,寫在配置檔案中:

password:ENC(05RybgJrpb+uEZ0tWWIfgyiS9nOMcnJm)