需要的jar包
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
##使用方法生成加密后的用户名和密码
public class CreateUsernaemAndPassword {
public static void main(String[] args) {
BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
//加密所需要的salt(盐值)
textEncryptor.setPassword("innodata");
textEncryptor.setPassword("innodata");
//加密
String username = textEncryptor.encrypt("root");
String password = textEncryptor.encrypt("root");
//解密
String u= textEncryptor.decrypt("KGivkxkRwbRoauGUMHXb1lVzWhbfUf9o");
String p= textEncryptor.decrypt("ZaJx75ElCLuY9Qyj0+4wxUqRvoizypOP");
System.out.println(u); System.out.println(p);
System.out.println("username:"+username+"password:"+password);
}
}
加密时需要的盐值需要写在配置文件中,把获取到的加密后的用户名和密码用ENC函数的形式写入
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost/quartz?autoReconnect=true&characterEncoding=utf-8&allowMultiQueries=true #allowMultiQueries=true 允许批量更新
password: ENC(M6C9sRLfVBoSPyG7dFJbFw==)
username: ENC(t0TtxTjcm4Ni71gJEU+HJg==)
jasypt:
encryptor:
password: quartz #数据库用户名密码加密盐值
注意:spring boot启动后自动解密加密后的用户名和密码,若无法解密,请看如下连接:
https://blog.csdn.net/weixin_45568648/article/details/105753089