
pom.xml 中引入依赖在springboot配置文件中配置加密解密用的密码在测试类中对原用户名、密码进行加密将加密后的账号密码替换到数据源配置中加密解密密码配置到JVM启动参数中
IDEA启动项目项目打成 jar 包运行
pom.xml 中引入依赖在springboot配置文件中配置加密解密用的密码com.github.ulisesbocchio jasypt-spring-boot-starter3.0.4
jasypt:
encryptor:
password: 123456
在测试类中对原用户名、密码进行加密
package com.mdc.system;
import org.jasypt.encryption.StringEncryptor;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class TestEncryptor {
@Autowired
private StringEncryptor stringEncryptor;
@Test
public void test(){
String username = stringEncryptor.encrypt("root");
System.out.println("账号加密: " + username);
String password = stringEncryptor.encrypt("123456");
System.out.println("密码加密: " + password);
// 解密
System.out.println(stringEncryptor.decrypt(username));
System.out.println(stringEncryptor.decrypt(password));
}
}
控制台输出:
账号加密: ZI3fUrA9vcz9FJ5v4uQQ6TQR++/ua4b9j1H+ETNFfOlnY30hlrDucFO4qys7WooM 密码加密: oSV3ixpATPnYDOIeox3FNLA2nqjnrdPeH+5aTlG1YFGmsDiL9sDJmYOB4vnk1Oxt 加密后的账号解密: root 加密后的密码解密: 123456将加密后的账号密码替换到数据源配置中
加密后的账号密码使用 ENC( )包裹,项目启动的时候,会根据配置好的解密密码进行解密
url: jdbc:mysql://42.193.149.155:3306/my_data_cloud?characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true username: ENC(ZI3fUrA9vcz9FJ5v4uQQ6TQR++/ua4b9j1H+ETNFfOlnY30hlrDucFO4qys7WooM) password: ENC(oSV3ixpATPnYDOIeox3FNLA2nqjnrdPeH+5aTlG1YFGmsDiL9sDJmYOB4vnk1Oxt) driver-class-name: com.mysql.cj.jdbc.Driver加密解密密码配置到JVM启动参数中
敏感信息已经进行加密,所以加密解密使用的密码不能直接暴露在配置文件中,可以将其配置到JVM启动参数:
IDEA启动项目IDEA启动项目时,如下配置
-Djasypt.encryptor.password=123456
将项目打包成 jar 包,通过 java -jar 命令启动时,使用如下命令配置
java -jar xxx.jar --jasypt.encryptor.password=123456
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)