Skip to content

加密配置文件中的敏感信息

在Spring应用程序中,配置文件通常包含敏感信息,如数据库密码、API密钥等。为了保护这些信息的安全性,可以使用加密技术对配置文件中的敏感信息进行加密处理。本文将介绍如何在Spring应用程序中实现配置文件的加密和解密。

1. jasypt介绍

Jasypt (全称Java Simplified Encryption),是一个为开发者在项目中提供加/解密功能的依赖库。

但是,该库已不再维护。

jasypt-spring-boot在Spring项目中提供了Jasypt的集成,因此,本节内容以jasypt-spring-boot为例。

2. 流程介绍

2.1 项目介绍

演示环境基于JDK 17 + Spring Boot 3.5.8:

xml
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.5.8</version>
    <relativePath/>
</parent>

<properties>
    <java.version>17</java.version>
</properties>

2.2 引入依赖

参考资料[2] 选择合适的版本,引入依赖:

xml
<dependency>
    <groupId>com.github.ulisesbocchio</groupId>
    <artifactId>jasypt-spring-boot-starter</artifactId>
    <version>3.0.5</version>
</dependency>

2.3 明文加密

运行以下代码,进行明文加密:

java
// 简单的加密代码示例
@Test
public void test() {
    PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
    SimpleStringPBEConfig config = new SimpleStringPBEConfig();
    // 密钥,需要记住
    config.setPassword("test");
    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);

    // 要加密的明文
    String encrypted = encryptor.encrypt("root");
    System.out.println(encrypted);
}

输出内容如下:

txt
ghnNkohltK00tf0GbQPKETX0pGzP/nUfgD7JiEceb0z+qI9thFPoJBmR/G0GNeUn

2.4 使用密文

在配置文件application.yml中,我们就可以使用密文了,例如:

yaml
spring:
	datasource:
		password: ENC(ghnNkohltK00tf0GbQPKETX0pGzP/nUfgD7JiEceb0z+qI9thFPoJBmR/G0GNeUn)

2.5 提供密码

在虚拟机参数中提供解密的密钥(和之前加密提供的一致):

txt
-Djasypt.encryptor.password=test

当程序运行后,jasypt-spring-boot会使用提供的解密密钥,对配置文件中的密文进行解密,然后程序再使用解密后的敏感信息。

3. jasypt配置项

下表列出了jasypt-spring-boot的配置项,可以在配置文件中进行修改:

KeyRequiredDefault Value
jasypt.encryptor.passwordTrue-
jasypt.encryptor.algorithmFalsePBEWITHHMACSHA512ANDAES_256
jasypt.encryptor.key-obtention-iterationsFalse1000
jasypt.encryptor.pool-sizeFalse1
jasypt.encryptor.provider-nameFalseSunJCE
jasypt.encryptor.provider-class-nameFalsenull
jasypt.encryptor.salt-generator-classnameFalseorg.jasypt.salt.RandomSaltGenerator
jasypt.encryptor.iv-generator-classnameFalseorg.jasypt.iv.RandomIvGenerator
jasypt.encryptor.string-output-typeFalsebase64
jasypt.encryptor.proxy-property-sourcesFalsefalse
jasypt.encryptor.skip-property-sourcesFalseempty list

WARNING

注意,jasypt.encryptor.password不要出现在配置文件中,否则起不到保护作用

TIP

注意,这里的配置需要和加密时的配置相同,一般情况下不需要修改。

也可以使用以下配置,修改密文模式,默认模式为ENC(...)

yaml
jasypt:
  encryptor:
    property:
      prefix: "ENC@["
      suffix: "]"

参考资料

[1] jasypt-spring-boot:https://github.com/ulisesbocchio/jasypt-spring-boot

[2] jasypt-spring-boot-starter版本:https://mvnrepository.com/artifact/com.github.ulisesbocchio/jasypt-spring-boot-starter

[3] jasypt: https://github.com/jasypt/jasypt