文章目录
前言
数据库账号密码加密
提示:以下是本篇文章正文内容
一、引入依赖
1.引入库
代码如下(示例):
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>3.0.2</version>
</dependency>
2.读入数据
代码如下(示例):
提示:encryptor.setPassword()自己设置 password:盐值
public static void main(String[] args) {
String account = "root";
String password = "123";
BasicTextEncryptor encryptor = new BasicTextEncryptor();
//秘钥
//encryptor.setPassword(System.getProperty("jasypt.encryptor.password"));
encryptor.setPassword("eug83f3gG");
//密码进行加密
String newAccount = encryptor.encrypt(account);
String newPassword = encryptor.encrypt(password);
System.out.println("加密后账号:" + newAccount);
System.out.println("加密后密码:" + newPassword);
AnsiOutput.setEnabled(Enabled.ALWAYS);
SpringApplication.run(PmsDcApplication.class, args);
}
3.配置文件
3.1 数据库配置
提示:配置文件中数据库加密后的账号密码使用ENC()包裹
datasource:
MyDb1:
driver-class-name: org.postgresql.Driver
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:postgresql://127.0.0.1:5432/test?reWriteBatchedInserts=true¤tSchema=test
username: ENC(rtEkW3k0lptWxIbri8BvNg==)
password: ENC(cSsZgL6Tx1egac46esgzGg==)
4 启动
4.1 (方式一)配置文件中配置盐值等(不建议,会暴漏)
提示:password使用自己设置的 algorithm:加密方式
jasypt:
encryptor:
password: eug83f3gG
algorithm: PBEWithMD5AndDES
iv-generator-classname: org.jasypt.iv.NoIvGenerator
4.2 (方式二)启动参数启动(建议使用)
4.2.1 jar包启动
提示:password使用自己设置的
jara -Djasypt.encryptor.password=eug83f3gG -Djasypt.encryptor.algorithm=PBEWithMD5AndDES -Djasypt.encryptor.iv-generator-classname=org.jasypt.iv.NoIvGenerator -jar wstest.1.1.1.jar
4.2.2 idea启动
启动参数:-Djasypt.encryptor.password=eug83f3gG -Djasypt.encryptor.algorithm=PBEWithMD5AndDES -Djasypt.encryptor.iv-generator-classname=org.jasypt.iv.NoIvGenerator