SpringBoot配置文件加密:使用Jasypt保护数据库、Redis敏感密码

admin 2026-09-04 04:33:06 网络安全文章 来源:ZONE.CI 全球网 0 阅读模式

文章总结: 本文介绍SpringBoot配置加密组件Jasypt-spring-boot-starter,通过ENC密文格式实现配置零侵入解密,支持多种密钥外部传入方式。实操演示SpringBoot3环境下的依赖引入、加密工具类编写、yml配置及验证流程,并总结生产环境密钥注入方案与常见踩坑。建议生产环境密钥通过K8sSecret或密钥管理服务注入,确保密钥安全。 综合评分: 88 文章分类: 应用安全,安全开发,解决方案


Spring Boot配置文件加密:使用Jasypt保护数据库、Redis敏感密码

原创

知码乎 知码乎

知码乎

2026年9月2日 18:30 山东

在小说阅读器读本章

去阅读

在公众号小说中沉浸阅读

在Spring Boot项目开发中,数据库、Redis、第三方密钥等敏感信息直接以明文写在application.yml/properties配置文件是非常普遍的现象。一旦Jar包发生泄露,所有账号密码直接暴露,会带来极高的安全风险。今天给大家介绍SpringBoot生态主流的配置加密组件 Jasypt‑spring‑boot‑starter,低成本解决配置明文泄露问题。

壹、Jasypt加密介绍

Jasypt‑spring‑boot‑starter是SpringBoot生态最常用配置加密组件,核心定位:对业务代码零侵入,只处理配置属性解密

在接入之前,先了解Jasypt的优缺点,评估是否适配你的项目。

1.1、 Jasypt优点

  • 业务代码零侵入:yml/properties 中使用固定包装格式 ENC(密文),框架自动识别并解密,业务逻辑完全不用改
  • 支持多种配置:支持 yml、properties、环境变量、命令行参数
  • 成本低,上手快:只引入 starter 依赖,少量配置即可工作
  • 支持多种密钥传入方式:密钥可以不写进配置文件,多种外部传入手段,包括JVM启动参数、环境变量、Kubernetes Secret 注入环境变量、自定义密钥读取逻辑(从远程密钥服务、数据库读取密钥)
  • 支持部分加密:只加密敏感字段(数据库密码、accessKey、token),普通配置保持明文

1.2、 Jasypt缺点

  • 默认是对称加密:加密解密同一个密钥,密钥泄露 = 所有密文全部泄露;不是非对称,不能当成安全银弹
  • 密钥依然需要通过外部渠道安全投递:本身不能保管密钥,只是解密工具
  • 不能阻止内存窥探:解密后的明文会存在 JVM 内存中,和普通配置一样

⚠️重要提示:Jasypt只是配置解密工具,不是万能安全方案。密钥的安全保管才是整个加密体系的核心

贰、Jasypt实操使用

需要注意,Jasypt在Spring Boot 2与Spring Boot 3中使用稍有不同,下面案例基于Spring Boot 3进行实际验证,并标注Spring Boot2差异点。

2.1、 项目环境

Spring Boot版本:3.5.16 JDK: 17

2.2、 添加Maven依赖

pom.xml中引入jasypt启动器,SpringBoot2与SpringBoot3版本号不一样,不要混用。

<!-- springboot3 -->
<dependency>
&nbsp; &nbsp; <groupId>com.github.ulisesbocchio</groupId>
&nbsp; &nbsp; <artifactId>jasypt-spring-boot-starter</artifactId>
&nbsp; &nbsp; <version>3.0.5</version>
</dependency>
<!-- springboot2
<dependency>
&nbsp; &nbsp; <groupId>com.github.ulisesbocchio</groupId>
&nbsp; &nbsp; <artifactId>jasypt-spring-boot-starter</artifactId>
&nbsp; &nbsp; <version>2.2.0</version>
</dependency>
-->

2.3、 编写密码加密工具类

我们需要先把明文密码加密为密文,再把密文写入配置文件。新建工具类执行main方法生成密文。

public class JasyptGen {
&nbsp; &nbsp; public static void main(String[] args) {
&nbsp; &nbsp; &nbsp; &nbsp; Set<String> pbeAlgorithms = new HashSet<>();
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;for&nbsp;(Provider provider : Security.getProviders()) {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; provider.getServices().stream()
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter(service ->&nbsp;"Cipher".equals(service.getType()))
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .map(Provider.Service::getAlgorithm)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter(algo -> algo.startsWith("PBE"))
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .forEach(pbeAlgorithms::add);
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("✅ 当前 JDK 支持的 PBE 算法:");
&nbsp; &nbsp; &nbsp; &nbsp; pbeAlgorithms.stream().sorted().forEach(System.out::println);
&nbsp; &nbsp; &nbsp; &nbsp; StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
&nbsp; &nbsp; &nbsp; &nbsp; // 加密密钥,生产环境不要写在代码/配置文件
&nbsp; &nbsp; &nbsp; &nbsp; encryptor.setPassword("MySecretKey");
&nbsp; &nbsp; &nbsp; &nbsp; // 算法,springboot3默认需要指定
&nbsp; &nbsp; &nbsp; &nbsp; encryptor.setAlgorithm("PBEWithHmacSHA512AndAES_256");
&nbsp; &nbsp; &nbsp; &nbsp; encryptor.setIvGenerator(new RandomIvGenerator());
&nbsp; &nbsp; &nbsp; &nbsp; String encrypt = encryptor.encrypt("我是明文");
&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("加密后密文:"&nbsp;+ encrypt);
&nbsp; &nbsp; }
}

关键点说明:

  1. 加密密钥MySecretKey生产环境禁止硬编码在代码、配置文件,后续通过启动参数/环境变量传入;
  2. Spring Boot3必须显式指定加密算法;
  3. 运行main方法,复制控制台输出的密文,后续粘贴到yml配置。

2.4、 项目配置与测试

2.4.1、 yml配置加密算法

application.yml中配置jasypt加密算法与iv生成器

jasypt.encryptor.algorithm=PBEWITHHMACSHA512ANDAES_256
jasypt.encryptor.iv-generator-classname=org.jasypt.iv.RandomIvGenerator

2.4.2、 将密文写入配置

使用固定格式 ENC(密文)包裹生成好的密文。

testJasypt : ENC(o3fk5AMqFDodOdZUpQMPHmIPuvmPHIp4R+wVDK3IE9d5twZp1Nz/3sBvAJb0yNFa)

2.4.3、 编写接口验证解密效果

写一个简单Rest接口读取加密配置,验证框架是否自动解密成功。

@RestController
public class IndexController {
&nbsp; &nbsp; @Value("${testJasypt}")
&nbsp; &nbsp; private String testJasypt;
&nbsp; &nbsp; @RequestMapping("index")
&nbsp; &nbsp; public String&nbsp;index(){
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;testJasypt;
&nbsp; &nbsp; }
}

打包项目,运行Jar包时通过JVM参数传入解密密钥:

java -jar jasypt-0.0.1-SNAPSHOT.jar --jasypt.encryptor.password=MySecretKey

jasypt.encryptor.password就是我们加密时使用的密钥。

浏览器访问地址:http://localhost:8080/index,接口返回原始明文,代表解密成功。

叁、生产环境密钥的几种传入方案

⚠️千万不要把密钥写进yml配置文件,否则加密就失去意义。这里列举生产常用的密钥注入方式:

  1. JVM启动参数(示例上文已经演示)
java -jar demo.jar --jasypt.encryptor.password=MySecretKey
  1. 操作系统环境变量设置环境变量 JASYPT_ENCRYPTOR_PASSWORD=MySecretKey,程序启动自动读取。
  2. K8s环境使用Secret注入环境变量将密钥存入Kubernetes Secret,容器启动注入环境变量,不写在镜像与配置文件。
  3. 自定义获取密钥逻辑对接密钥管理服务,程序启动时远程拉取密钥,安全性最高,适合大型企业项目。

肆、常见踩坑总结

  1. SpringBoot2和SpringBoot3的jasypt starter版本不能混用,否则会启动报错;
  2. SpringBoot3必须手动指定 PBEWithHmacSHA512AndAES_256算法与IvGenerator;
  3. 加密、解密必须使用同一套密钥+同一套算法,否则解密失败;
  4. Jasypt是对称加密,保护好密钥是安全的核心;
  5. 密文只保护配置文件,JVM内存中依然是明文,无法防止内存读取类攻击。

免责声明:

本文所载程序、技术方法仅面向合法合规的安全研究与教学场景,旨在提升网络安全防护能力,具有明确的技术研究属性。

任何单位或个人未经授权,将本文内容用于攻击、破坏等非法用途的,由此引发的全部法律责任、民事赔偿及连带责任,均由行为人独立承担,本站不承担任何连带责任。

本站内容均为技术交流与知识分享目的发布,若存在版权侵权或其他异议,请通过邮件联系处理,具体联系方式可点击页面上方的联系我

本文转载自:知码乎 知码乎 知码乎《Spring Boot配置文件加密:使用Jasypt保护数据库、Redis敏感密码》

评论:0   参与:  0