介绍
在生产环境,数据库采用明文配置会有安全问题,jasypt是一个通用的加解密库
pom依赖
<!-- 数据库密码加密 -->
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
密文获取,对数据进行加密解密
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
public class EncryptUtils {
public static void main(String[] arg){
StandardPBEStringEncryptor standardPBEStringEncryptor =new StandardPBEStringEncryptor();
/*配置文件中配置如下的算法*/
standardPBEStringEncryptor.setAlgorithm("PBEWithMD5AndDES");
/*配置文件中配置的password*/
standardPBEStringEncryptor.setPassword("SMRsmr2022202220222022");
/*解密:加密的文本*/
System.out.println( standardPBEStringEncryptor.decrypt("PBu1ro5ySdVbYivoer5uPw=="));
// 加密账号
String name = standardPBEStringEncryptor.encrypt("root");
// 加密密码
String password =standardPBEStringEncryptor.encrypt("root");
/*将加密的文本写到配置文件中*/
System.out.println("name="+name);
System.out.println("password="+password);
}
}
SpringBoot-yml配置文件中的配置
jasypt:
encryptor:
password: SMRsmr2022202220222022
algorithm: PBEWithMD5AndDES
spring:
datasource:
url: jdbc:mysql://localhost:3306/data?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
username: ENC(wFn1/z2FEjXVQfynQphO7A==)
password: ENC(PBu1ro5ySdVbYivoer5uPw==)