以下是未经过加密的数据库配置,密码均是采用明文密码,很容易导致数据库泄露。
spring:datasource:dynamic:postgresql:url: jdbc:postgresql://127.0.0.1:5589/mypg?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=Asia/Shanghaiusername: rootpassword: admin driver-class-name: org.postgresql.Driver ...redis:ip: www.xxxx.topport: 6379# 密码pass: admin # 最大实例max-total: 1024# 最大空闲实例max-idle: 100# 等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。max-wait: 10000# 超时时间,单位毫秒。timeout: 10000
以下是经过ENC加密之后的配置,这样之后,数据库密码安全级别就高了。
spring:datasource:dynamic:postgresql:url: jdbc:postgresql://127.0.0.1:5589/mypg?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=Asia/Shanghaiusername: rootpassword: ENC(2qWZr4WLw8H0nPBzMXBV9WimRfKC3pv5UVGB5mMApbINg2s83VfsYvL7XTWaUR8q)driver-class-name: org.postgresql.Driver ...redis:ip: www.xxxx.topport: 6379# 密码pass: ENC(2qWZr4WLw8H0nPBzMXBV9WimRfKC3pv5UVGB5mMApbINg2s83VfsYvL7XTWaUR8q) # 最大实例max-total: 1024# 最大空闲实例max-idle: 100# 等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。max-wait: 10000# 超时时间,单位毫秒。timeout: 10000
导入依赖:
com.github.ulisesbocchio jasypt-spring-boot-starter 3.0.2
测试类:
import org.jasypt.encryption.StringEncryptor;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;import javax.annotation.Resource;/*** @Author: chenJY* @Description:* @Date: 2022-11-18 9:19*/
@SpringBootTest
@RunWith(SpringRunner.class)
public class EncryptorTest {@Resourceprivate StringEncryptor jasyptStringEncryptor;@Testpublic void encode() {System.out.println( "加密密文:" + jasyptStringEncryptor.encrypt("admin") );System.out.println("解密密文:" + jasyptStringEncryptor.decrypt(jasyptStringEncryptor.encrypt("admin")));}
}
运行测试类:
将加密密文加入到配置文件中:
spring:datasource:dynamic:postgresql:url: jdbc:postgresql://127.0.0.1:5589/mypg?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=Asia/Shanghaiusername: rootpassword: Chen(2qWZr4WLw8H0nPBzMXBV9WimRfKC3pv5UVGB5mMApbINg2s83VfsYvL7XTWaUR8q)chendriver-class-name: org.postgresql.Driver ...redis:ip: www.xxxx.topport: 6379# 密码pass: Chen(2qWZr4WLw8H0nPBzMXBV9WimRfKC3pv5UVGB5mMApbINg2s83VfsYvL7XTWaUR8q)chen # 最大实例max-total: 1024# 最大空闲实例max-idle: 100# 等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。max-wait: 10000# 超时时间,单位毫秒。timeout: 10000
重启springboot项目,能正常启动
由于springboot自动配置的特性,导入 jasypt-spring-boot 依赖包之后,不用进行过多配置,就能实现配置文件加密字段自动解密,所以特别方便。
可以在配置文件中自定义一个加密秘钥(盐), 来获取明文密码。
# 加密秘钥
jasypt:encryptor:password: Chen # 加密时的salt值
自定义加密前缀、后缀: 如果不想使用 ENC来作为加密前缀,那么可以通过配置文件修改:
# 加密秘钥
jasypt:encryptor:password: Chenproperty:prefix: Chen( # 前缀suffix: )chen # 后缀
配置类
@Configuration
public class MyEncryptorCfg {/*** @Description 自定义的加密器配置* @author chenJY* @date 2022/11/18 9:52* @return StringEncryptor*/@Bean(name = "myStringEncryptor")public StringEncryptor myStringEncryptor() {PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();SimpleStringPBEConfig config = new SimpleStringPBEConfig();config.setPassword("Chen");config.setAlgorithm("PBEWITHHMACSHA512ANDAES_256");config.setKeyObtentionIterations("1000");config.setPoolSize("1");config.setProviderName("SunJCE");config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");config.setStringOutputType("base64");encryptor.setConfig(config);return encryptor;}
}
注意1: bean必须重命名,bean默认名是 jasyptStringEncryptor,当我们要自定义加密方案的时候,就必须重命名。
注意2: 需要在配置文件中加入如下配置:
jasypt:encryptor:bean: myStringEncryptor
并修改测试类:
@Autowiredprivate StringEncryptor myStringEncryptor;
密钥(盐值)存储说明: 本身加解密过程都是通过盐值进行处理的,所以正常情况下盐值和加密串是分开存储的。盐值应该放在系统属性、命令行或是环境变量来使用,而不是放在配置文件。
程序启动 命令行参数:
java -jar xxx.jar --jasypt.encryptor.password=Chen &
程序启动 环境变量:
java -jar -Djasypt.encryptor.password=Chen xxx.jar
下一篇:准备pmp考试第13天