配置文件中有关敏感数据的加密"/>
Jasypt整合springboot完成对配置文件中有关敏感数据的加密
场景
项目中有很多密码都是以明文的形式存储在配置文件中,这样很不安全。我们可以通过jasypt来完成敏感信息的加密。
步骤
1、pom文件引入依赖。
<dependency><groupId>com.github.ulisesbocchio</groupId><artifactId>jasypt-spring-boot-starter</artifactId><version>3.0.4</version>
</dependency>
2、写个测试类获取密文。
String secret = "haha";BasicTextEncryptor basicTextEncryptor = new BasicTextEncryptor();basicTextEncryptor.setPassword(secret); //设置密钥String s = basicTextEncryptor.encrypt("123456"); //加密System.out.println(s);
3、application.yml文件写死密文。
# Spring配置
spring:# 数据源配置datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/test?characterEncoding=utf-8&&serverTimezone=Asia/Shanghai&&useSSL=falseusername: root# 此处的密文要用ENC()进行包裹,不然是识别不了的,自然也就无法解密password: ENC(fUaD/g4L9TKx8AFhz8A5qQ==)# 加密配置
jasypt:encryptor:# 指定密钥password: haha# 下面这两个参数必须得加,不加的话会报参数绑定异常# 指定解密算法,需要和加密时使用的算法一致algorithm: PBEWithMD5AndDES# 指定initialization vector类型iv-generator-classname: org.jasypt.iv.NoIvGenerator
线上环境
如果是线上环境,密钥需作为启动参数加进去,这样更安全。
java -jar -Djasypt.encryptor.password=your-secret
更多推荐
Jasypt整合springboot完成对配置文件中有关敏感数据的加密
发布评论