1. 程式人生 > 實用技巧 >jasypt 對 配置檔案密碼進行加密處理

jasypt 對 配置檔案密碼進行加密處理

內容來源:https://blog.csdn.net/She_lock/article/details/84371215

jasypt由於其使用的是PBEWithMD5AndDES加密方式,所以每次加密出來的結果都不一樣,所以很適合對資料進行加密

1、新增依賴

	<!-- jasypt 密碼加密處理 -->
	<dependency>
	    <groupId>com.github.ulisesbocchio</groupId>
	    <artifactId>jasypt-spring-boot-starter</artifactId>
	    <version>2.1.1</version>
	</dependency>

  

2、編寫加密方法

   public static void encrytion() {
             BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
             //加密所需的salt(鹽)
             textEncryptor.setPassword("acbd123!");
             //要加密的資料(資料庫的使用者名稱或密碼)
             String password = textEncryptor.encrypt("123456");
             
             
//解密: //textEncryptor.decrypt(""); System.out.println("password:"+password); }

3、在配置檔案中寫上jasypt salt (鹽)內容

jasypt:
  encryptor:
    password: acbd123!

  

4、將第二步得到的加密結果,寫在資料庫配置資訊的password中,寫法如下,(前面增加ENC,用括號把內容括起來)

password: ENC(DnHgQZbrx2+/S7xE11MXrw==)