Spring Boot 使用 jasypt配置明文密码加密
-
引入依赖
<dependency><groupId>com.github.ulisesbocchio</groupId><artifactId>jasypt-spring-boot-starter</artifactId><version>3.0.4</version> </dependency>
-
添加配置
jasypt:encryptor:password: p@ssw0rd&Hubt2ec980e$ttsalgorithm: PBEWithMD5AndDES# 配置初始化向量生成器,解决Jasypt 3.0.3及以上版本启动报错的问题# NoIvGenerator表示不使用向量生成器(即无向量)iv-generator-classname: org.jasypt.iv.NoIvGenerator
-
生成密码的加密串
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;public class JasyptUtils {public static void main(String[] args) {//明文密码String password = "Mysql@123";//自定义密钥:与配置文件中的 jasypt.encryptor.password 保持一致String secretKey = "p@ssw0rd&Hubt2ec980e$tts";// 算法:与配置文件中的 jasypt.encryptor.algorithm 保持一致String algorithm = "PBEWithMD5AndDES";// 创建加密器对象StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();encryptor.setPassword(secretKey);encryptor.setAlgorithm(algorithm);// 执行加密操作System.out.println("加密前:" + password);String encryptedPassword = encryptor.encrypt(password);System.out.println("加密后:" + encryptedPassword); } }
-
替换明文密码
spring:#数据库datasource:driver-class-name:com.mysql.cj.jdbc.Driverur1: jdbc:mysql://127.0.0.1:3306/decoration_management_v1?useUnicode=true&characterEncoding=utf8username: root#password: Mysal@123password:ENC(nr029lISw82X2u3/eTHwUQ9W8C8gk04)#Redis configredis:host: 127.0.0.1#password: Rds&P@ssWOrd!password:ENC(Ew0ugG5tzly9x6Ihklp0flzqBjwGVpAb)uatapase.ssl:false
配置方式:ENC(加密串)