发布于2026-07-06 阅读(0)
扫一扫,手机访问
在现代软件开发和部署中,应用程序的配置文件(如 application.properties 或 application.yml)常常包含敏感信息,例如:

这些敏感信息如果以明文形式存储在配置文件中,无论是在代码仓库、服务器文件系统还是容器镜像里,都存在极大的安全风险。一旦泄露,可能导致数据被窃取、服务被滥用或系统被攻击。因此,对配置文件中的敏感信息进行加密处理是保障应用安全的重要环节。
本文将详细介绍如何使用 jasypt-spring-boot 库,在 Spring Boot 和 Spring Cloud 项目中实现配置文件内容的加密与解密。
jasypt-spring-boot 是一个集成 Jasypt (Ja va Simplified Encryption) 的 Spring Boot Starter,它极大地简化了在 Spring Boot 应用中进行属性加密和解密的过程。
Environment 抽象层中,允许开发者使用特定的格式(如 ENC(encrypted_value))来标记加密后的属性值。Spring Boot 在启动时会自动识别并解密这些值,使应用代码无需关心解密过程。首先,需要在你的 Spring Boot 项目的 pom.xml(Ma ven)或 build.gradle(Gradle)文件中添加 jasypt-spring-boot-starter 依赖。
Ma ven (pom.xml)
com.github.ulisesbocchio jasypt-spring-boot-starter 3.0.5
Gradle (build.gradle)
dependencies {
implementation 'com.github.ulisesbocchio:jasypt-spring-boot-starter:3.0.5'
// ... 其他依赖
}
注意:jasypt-spring-boot 的版本通常需要与你的 Spring Boot 版本兼容。建议查阅 GitHub Releases 页面选择正确的版本。
Jasypt 加密和解密都需要一个共享的加密密钥(Password)。这个密钥必须被安全地传递给应用。常见的设置方式有:
ja va -jar myapp.jar --jasypt.encryptor.password=myStrongPasswordexport JASYPT_ENCRYPTOR_PASSWORD=myStrongPassword && ja va -jar myapp.jarapplication.properties 或 application.yml 中设置(强烈不推荐,因为这样失去了加密的意义,但可用于测试)。例如:# application.properties jasypt.encryptor.password=myStrongPassword
# application.yml
jasypt:
encryptor:
password: myStrongPassword
最佳实践:将 jasypt.encryptor.password 通过环境变量或命令行参数的方式传入,避免将其硬编码在配置文件中。
加密操作可以通过多种方式进行,最常用的是使用命令行工具或编写一个简单的 Ja va 工具类。
下载 Jasypt 的发行包,解压后在 bin 目录下有各种脚本(encrypt.bat, encrypt.sh 等)。
执行命令:
./encrypt.sh input="your-sensitive-value" password="myStrongPassword"
(Windows 用户使用 encrypt.bat)
输出示例:
----ENVIRONMENT----------------- Runtime: Oracle Corporation Ja va HotSpot(TM) 64-Bit Server VM 25.201-b09 ... (其他环境信息) ----ARGUMENTS------------------- input: your-sensitive-value password: myStrongPassword ... ----OUTPUT---------------------- UyBtaXNzaW5nIHlvdSB0aGVyZQ== # 这是加密后的值
创建一个简单的 Ja va 类来执行加密:
import org.jasypt.encryption.StringEncryptor;
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig;
public class JasyptEncryptionUtil {
public static void main(String[] args) {
if (args.length != 1) {
System.err.println("Usage: ja va JasyptEncryptionUtil ");
System.exit(1);
}
String valueToEncrypt = args[0];
String password = System.getenv("JASYPT_ENCRYPTOR_PASSWORD"); // 从环境变量读取密码
if (password == null || password.isEmpty()) {
System.err.println("Error: JASYPT_ENCRYPTOR_PASSWORD environment variable is not set.");
System.exit(2);
}
StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();
config.setPassword(password); // 设置加密密码
// 可选:设置算法、迭代次数等
// config.setAlgorithm("PBEWithMD5AndDES");
encryptor.setConfig(config);
String encryptedValue = encryptor.encrypt(valueToEncrypt);
System.out.println("Encrypted Value: " + encryptedValue);
}
}
编译并运行:
ja vac -cp "path/to/jasypt-*.jar" JasyptEncryptionUtil.ja va export JASYPT_ENCRYPTOR_PASSWORD=myStrongPassword ja va -cp ".:path/to/jasypt-*.jar" JasyptEncryptionUtil "your-sensitive-value"
如果你的应用已经包含了 jasypt-spring-boot-starter,你可以利用其内部的 StringEncryptor bean 来加密,但这通常不如前两种方法直接。
获得加密后的字符串后,你需要将其放入你的 Spring Boot 配置文件中。jasypt-spring-boot 会自动识别 ENC(encrypted_value) 格式的属性值并进行解密。
application.yml 示例:
spring:
datasource:
url: jdbc:mysql://localhost:3306/mydb
username: ENC(U2FsdGVkX1+...) # 加密后的用户名
password: ENC(U2FsdGVkX1+...) # 加密后的密码
driver-class-name: com.mysql.cj.jdbc.Driver
myapp:
api:
key: ENC(V2FsdGVkX1+...) # 加密后的API Key
application.properties 示例:
spring.datasource.url=jdbc:mysql://localhost:3306/mydb spring.datasource.username=ENC(U2FsdGVkX1+...) spring.datasource.password=ENC(U2FsdEVkX1+...) spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver myapp.api.key=ENC(V2FsdGVkX1+...)
当 Spring Boot 应用启动时,jasypt-spring-boot 会拦截这些属性的读取请求,检测到 ENC(...) 标记,然后使用配置的密码对其进行解密,并将解密后的原始值注入到相应的 Bean 属性或通过 @Value 注解注入到变量中。
EncryptablePropertyResolver 是 jasypt-spring-boot 内部使用的一个关键接口,用于解析包含 ENC() 标记的属性值。它扩展了 Spring 的 org.springframework.core.env.PropertyResolver 接口。
Environment 尝试解析一个属性时(例如 environment.getProperty("spring.datasource.password")),jasypt-spring-boot 会提供一个实现了 EncryptablePropertyResolver 的包装器。ENC( 开头且以 ) 结尾。StringEncryptor 解密,并返回解密后的结果。开发者通常不需要直接与 EncryptablePropertyResolver 交互,jasypt-spring-boot-starter 会自动完成这一过程。
jasypt.encryptor.* 属性来自定义加密算法、迭代次数、盐值生成器等。jasypt:
encryptor:
algorithm: PBEWITHHMACSHA512ANDAES_256 # 更强的算法
iv-generator-classname: org.jasypt.iv.RandomIvGenerator # 使用随机IV
# ... 其他配置
jasypt.encryptor.password 的安全性至关重要。不要将其硬编码在代码或配置文件中。使用环境变量、Kubernetes Secrets、HashiCorp Vault 等外部化、安全的密钥管理方案是更好的选择。jasypt-spring-boot 来加密和解密从 Config Server 获取的配置。通过 jasypt-spring-boot,可以非常方便地在 Spring Boot 和 Spring Cloud 项目中实现配置文件的加密。其核心在于使用 ENC() 标记加密后的值,并通过 jasypt.encryptor.password 来解密。EncryptablePropertyResolver 在后台自动完成了识别、解密和替换的过程。正确管理和保护加密密钥是确保此安全措施有效性的关键。
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
正版软件
正版软件
正版软件
正版软件
正版软件
1
2
3
7
8