1、引入依赖
<!--springboot整合jasypt-->
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
2、编写加密工具类
package com.jwbasta.admin.util.jasypt;
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
/**
* jasypt加密工具类
* @author jiangwei
*
*/
public class JasyptUtil {
private static final String ALGORITHM_INFO = "PBEWithMD5AndDES";
private static final String PASSWORD_INFO = "qwert12345";
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor();
//配置文件中配置如下的算法
standardPBEStringEncryptor.setAlgorithm(ALGORITHM_INFO);
//配置文件中配置的password
standardPBEStringEncryptor.setPassword(PASSWORD_INFO);
String account = "aa";// 连接数据库用户名
String password = "*****";// 连接数据库密码
//BasicTextEncryptor encryptor = new BasicTextEncryptor();
//秘钥
// encryptor.setPassword("ENCKEY");
//密码进行加密
String newAccount = standardPBEStringEncryptor.encrypt(account);
String newPassword = standardPBEStringEncryptor.encrypt(password);
System.out.println("加密后账号:" + newAccount);
System.out.println("加密后密码:" + newPassword);
}
}
加密后密码截图:
3.配置文件修改,密码从工具类里面生成的
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
#使用ENC包住密文
username: ENC(gdVuzoI0etetyUesW0hddw==)
password: ENC(llDq/tbZ7t59TlvWxqQxQg==)
# jasypt加密的密匙(写到启动参数里面)
jasypt:
encryptor:
password: qwert12345
algorithm: PBEWithMD5AndDES