SpringBoot敏感配置加密:Jasypt

本文涉及的产品
云数据库 RDS MySQL,集群版 2核4GB 100GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,高可用版 2核4GB 50GB
简介: SpringBoot敏感配置加密:Jasypt

7DTXY0{15Z}AO%G7`ZIP`GW.png

背景


  • 对于配置中的密码(DB, MQ, Redis等),甚至账号,在生产环境下存明文,不安全,不专业,不合适。
  • 一把插着钥匙的锁,能说它是安全的吗?

操作流程


关于Jasypt实现对配置项的加密,网络上已经有很多这方面的资料,这里简要描述下步骤。

  1. 引入依赖
<dependency>
  <groupId>com.github.ulisesbocchio</groupId>
  <artifactId>jasypt-spring-boot-starter</artifactId>
  <version>1.18</version>
</dependency>
  1. 生成密文

  • 如果计算机上有项目用过Jasypt的,那么在maven的仓库目录下会有Jasypt的jar包。如果本地仓库没有,先下载jar包。在jar包所在目录下打开cmd命令行,键入java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="root" password=you-guess algorithm=PBEWithMD5AndDES

  • PC2Z$VB6WK4]H~BX8E~(V@B.png

  • 最下面输出的qN66aPx0SrcFulrPfmMXOw==是密文,在接下来要放入配置文件。
  1. 修改已有的配置

在已有的明文配置文件中,修改Jasypt密码相关配置。


spring:
  datasource:
    driverClassName: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mp?serverTimezone=Asia/Shanghai&characterEncoding=UTF-8&useSSL=false
    username: root
    password: ENC(qN66aPx0SrcFulrPfmMXOw==)
# 配置日志
logging:
  level:
    root: info
    com.heartsuit.dao: trace
  pattern:
    console: '%p%m%n'
# 加密密钥
jasypt:
  encryptor:
    password: you-guess

上面的修改主要有:

}80J`N3P~YB)]0A5RU05J6Q.png


Note: 生成的密文要用ENC()包起来,这是Jasypt的要求。

  1. 测试修改后的配置

略(按照上述配置,应一切正常~~)


Note: 需要注意的是,用于生成加密后的密码的密钥不可放在配置文件或者代码中,加密密钥jasypt.encryptor.password=you-guess可以对密文解密。因此,上述配置若在团队内可见,没什么影响,但是如果配置文件万一被放到了公网上,相当于把钥匙插在锁上,白加密了。。在生产环境下,建议去掉加密密钥配置:


spring:
  datasource:
    driverClassName: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mp?serverTimezone=Asia/Shanghai&characterEncoding=UTF-8&useSSL=false
    username: root
    password: ENC(qN66aPx0SrcFulrPfmMXOw==)
# 配置日志
logging:
  level:
    root: info
    com.heartsuit.dao: trace
  pattern:
    console: '%p%m%n'

仅去掉了:jasypt.encryptor.password=you-guess,这里jasypt.encryptor.password=you-guess可以有两种传入方式:

  • 通过服务启动参数


java -jar xxx.jar --jasypt.encryptor.password=you-guess
  • 通过系统环境变量

这个可以通过Idea IDE传入(开发环境),或者实际的系统环境变量传入(生产环境)。


Jasypt的加密与解密


通过Jasypt命令行的方式生产密文密码后,可以用Jasypt提供的API进行解密,当然,也可以用API的方式来完成加密。

  • 加密与解密

@Component
public class StringEncryptDecrypt {
    @Autowired
    StringEncryptor encryptor;
    public String encrypt(String plainText) {
        // Encrypt
        String encrypted = encryptor.encrypt(plainText);
        System.out.println("Encrypted: " + encrypted);
        return encrypted;
    }
    public String decrypt(String encrypted) {
        // Decrypt
        String decrypted = encryptor.decrypt(encrypted);
        System.out.println("Decrypted: " + decrypted);
        return decrypted;
    }
}

总结


实现对配置文件敏感数据的加密,网上资源很多,但一定要注意安全性,不可以把公钥公开配置在文件中;还是开头那句话:

一把插着钥匙的锁,能说它是安全的吗?


Source Code: Github

Reference: github.com/ulisesbocch…


If you have any questions or any bugs are found, please feel free to contact me.

Your comments and suggestions are welcome!

目录
相关文章
|
3天前
|
安全 Java 数据安全/隐私保护
Spring Boot中的数据加密与解密
Spring Boot中的数据加密与解密
|
2天前
|
Java
no main manifest attribute,软件开发部署SpringBoot要填配置,不填配置,报错哦@_@
no main manifest attribute,软件开发部署SpringBoot要填配置,不填配置,报错哦@_@
|
4天前
|
XML Java 关系型数据库
Action:Consider the following: If you want an embedde ,springBoot配置数据库,补全springBoot的xml和mysql配置信息就好了
Action:Consider the following: If you want an embedde ,springBoot配置数据库,补全springBoot的xml和mysql配置信息就好了
|
4天前
|
Java 测试技术 Spring
支付系统15-----支付宝支付,引入支付参数,如何使支付宝的配置信息变成SpringBoot相关的配置信息
支付系统15-----支付宝支付,引入支付参数,如何使支付宝的配置信息变成SpringBoot相关的配置信息
|
4天前
|
Java 数据库连接 mybatis
SpringBoot配置Mybatis注意事项,mappers层下的name命名空间,要落实到Dao的video类,resultType要落到bean,配置好mybatis的对应依赖。
SpringBoot配置Mybatis注意事项,mappers层下的name命名空间,要落实到Dao的video类,resultType要落到bean,配置好mybatis的对应依赖。
|
4天前
|
缓存 安全 Java
Spring Boot中的自动配置机制详解
Spring Boot中的自动配置机制详解
|
9天前
|
监控 IDE Java
探索 IntelliJ IDEA 中 Spring Boot 运行配置选项及其作用
探索 IntelliJ IDEA 中 Spring Boot 运行配置选项及其作用
13 0
|
9天前
|
Java 数据管理 关系型数据库
Spring Boot中实现多数据源配置
Spring Boot中实现多数据源配置
|
9天前
|
自然语言处理 Java UED
Spring Boot中的国际化配置
Spring Boot中的国际化配置
|
9天前
|
监控 Java Spring
Spring Boot中的热部署配置
Spring Boot中的热部署配置