blogspot.com-GA4

星期日, 7月 06, 2025

Springboot Jasypt 設定application yaml 無法解密問題處理

使用版本 Springboot3.5.0

引用版本
  <dependency>
    <groupid>com.github.ulisesbocchio</groupid>
    <artifactid>jasypt-spring-boot-starter</artifactid>
    <version>3.0.5</version>
  </dependency>


啟用加密功能application run

import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableEncryptableProperties // 啟用 Jasypt 加密功能
public class YourApplication {

  public static void main(String[] args) {
    SpringApplication.run(YourApplication.class, args);
  }
}


在application.properties 中設定
jasypt.encryptor.password=your_secret_password
spring.datasource.password=pwd

在YAML中設定
jasypt:
   encryptor:
     password: your_secret_password
spring:
  datasource:
    password: ENC(C/q1R8y***************aWFEnVZ0rg==)

注意在此如果用env 設定

name: spring.datasource.password
value: ENC(C/q1R8y***************aWFEnVZ0rg==)


用以上的方式設定時,在啟動時發現密碼並無法生效,可能是因替換參數時無法啟動解密,改用以下 YAML設定環境變數替代

application.properties值
jasypt.encryptor.password=${DB_PASS}

在YAML中設定
name: DB_PASS
value: ENC(C/q1R8y***************aWFEnVZ0rg==)