構成ファイルにはデータベースのアカウント番号やパスワードなどの機密情報が含まれますが、プレーン テキストを使用することが安全でない場合は、jasypt を使用してこの情報を暗号化できます。
1. 依存関係を導入します。
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
バージョンによっては復号化できない場合があるので、バージョンに注意してください。
2. 暗号化
public class EncryptConfigUtil {
public static void main(String[] args) {
BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
//加密所需的salt
textEncryptor.setPassword("12321");
//要加密的数据(数据库的用户名或密码)
String username = textEncryptor.encrypt("root");
String password = textEncryptor.encrypt("123");
System.out.println("username:"+username);
System.out.println("password:"+password);
}
}
3. 平文を暗号文に置き換えます
4.jaspytの設定
暗号化アルゴリズムなども設定できるのでここでは詳しく説明しません
5. JVM 起動パラメータにキーを設定します。