概要
王道は jasypt を使う方法なのでまずはこれを試します
環境
- macOS 26.4.1
- openjdk 26.0.1
- SpringBoot 4.0.6
- jasypt-spring-boot 4.0.4
- gradle 9.5.1
- VSCode 1.121.0
- MySQL 9.6.0
パスワードなどの暗号化
password の部分は復号化に必要な秘密鍵を入力します
あとで jasypt.encryptor.password などアプリケーション内でパスワードを復号化する場合に使用するので忘れないようにしましょう
-
java -cp jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="root" password="xxx" algorithm=PBEWithMD5AndDES
-
java -cp jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input='""' password="xxx" algorithm=PBEWithMD5AndDES
OUTPUT で表示された値を application.properties に記載します
パスワードの暗号化方法は mvn やシェルでも提供されているのでお好みに応じて変更してください
application.properties
暗号化した情報を記載します
かならず ENC で囲って定義します
- vim src/main/resources/application.properties
spring.application.name=demo
# update はアプリケーション起動時に、Entityに対応するテーブルがなければ作成します
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/mydatabase
spring.datasource.username=ENC(bFMupckSRHJ/9QmXd1EVTw==)
spring.datasource.password=ENC(j64PBY1HLuuLo6Qoxo2xUg==)
# Jasypt 暗号化キーの設定(環境変数から取得)
jasypt.encryptor.algorithm=PBEWithMD5AndDES
jasypt.encryptor.iv-generator-classname=org.jasypt.iv.NoIvGenerator
jasypt.encryptor.password=${JASYPT_PASSWORD:default_password}
build.gradle
jasypt を spring-boot で使うための設定を追加します
implementation 'com.github.ulisesbocchio:jasypt-spring-boot-starter:4.0.4' を追記します
- vim build.gradle
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-webmvc'
implementation 'com.github.ulisesbocchio:jasypt-spring-boot-starter:4.0.4'
developmentOnly 'org.springframework.boot:spring-boot-docker-compose'
runtimeOnly 'com.mysql:mysql-connector-j'
testImplementation 'org.springframework.boot:spring-boot-starter-data-jpa-test'
testImplementation 'org.springframework.boot:spring-boot-starter-webmvc-test'
testImplementation 'org.springframework.boot:spring-boot-testcontainers'
testImplementation 'org.testcontainers:testcontainers-junit-jupiter'
testImplementation 'org.testcontainers:testcontainers-mysql'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}
アプリケーション
@EnableEncryptableProperties を追記します
これだけでアプリで自動的に使ってくれます
- vim src/main/java/com/example/demo/AccessingDataMysqlApplication.java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties;
@EnableEncryptableProperties
@SpringBootApplication
public class AccessingDataMysqlApplication {
public static void main(String[] args) {
SpringApplication.run(AccessingDataMysqlApplication.class, args);
}
}
起動
-
JASYPT_PASSWORD="xxx" ./gradlew bootRun
もしくは
-
./gradlew bootRun --args='--jasypt.encryptor.password=xxx'
もし jasypt.encryptor.password の復号化パスワードが違う場合はアプリが起動できません
最後に
SpringBoot で jasypt を使って秘密情報を暗号化する方法を紹介しました
かなり簡単にできるので必須の設定かなと思います
暗号化方式もたくさんあるので好きなものを使うといいかなと思います