引入jar包
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
yml添加配置
按照自己所需添加
jasypt:
encryptor:
password: shdchHJDK32FJ
测试加密
public static void main(String[] args) {
StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor();
EnvironmentPBEConfig config = new EnvironmentPBEConfig();
// 加密的算法,这个算法是默认的
config.setAlgorithm("PBEWithMD5AndDES");
// 加密的密钥,随便自己填写,很重要千万不要告诉别人
config.setPassword("Y6M9fAJQdU7jNp5MW");
standardPBEStringEncryptor.setConfig(config);
//要加密的密码
String plainText = "1234567";
String encryptedText = standardPBEStringEncryptor.encrypt(plainText);
System.out.println(encryptedText);
}
结果:ICEb0CnC+4aSBiouxJwP6Q==
测试解密
public static void main(String[] args) {
StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor();
EnvironmentPBEConfig config = new EnvironmentPBEConfig();
config.setAlgorithm("PBEWithMD5AndDES");
config.setPassword("Y6M9fAJQdU7jNp5MW");
standardPBEStringEncryptor.setConfig(config);
String encryptedText = "hc7DB7XRNcl6WbzUZX3c8w=="; //加密后的密码
String plainText = standardPBEStringEncryptor.decrypt(encryptedText);
System.out.println(plainText);
}
结果:1234567
注意⚠️
例子
username: ENC(CGitVEUe+fCzdSi4OMaQ683myHc+/qNn)
password: ENC(6KWGGYTm+bytaI5sGOALuBOQWlwiDum3)
后面一定要这样:ENC()
如下:
# Mysql数据库
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:21095/mh_prod?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=CTT
username: ENC(CGitVEUe+fCzdSi4OMaQ683myHc+/qNn)
password: ENC(6KWGGYTm+bytaI5sGOALuBOQWlwiDum3)
filters: wall,mergeStat
# 连接池大小根据实际情况调整
max-active: 20
max-pool-prepared-statement-per-connection-size: 20