在 Spring Boot 应用里,配置文件(如 application.propertiesapplication.yml)常常包含敏感信息,像数据库密码、API 密钥等。为了保障这些敏感信息的安全,可对配置文件进行加密。下面为你介绍几种常见的配置文件加密方法。

1. 使用 Jasypt 进行加密

Jasypt 是一个 Java 库,可简化加密操作,Spring Boot 集成 Jasypt 后能方便地对配置文件进行加密。

步骤

  • 添加依赖:在 pom.xml 中添加 Jasypt 的依赖。
<dependency>
    <groupId>com.github.ulisesbocchio</groupId>
    <artifactId>jasypt-spring-boot-starter</artifactId>
    <version>3.0.5</version>
</dependency>
  • 配置加密密钥:在运行应用时,通过系统属性或者环境变量设置加密密钥。
java -Djasypt.encryptor.password=yourSecretKey -jar yourApp.jar
  • 加密配置值:使用 Jasypt 提供的工具类加密敏感信息。
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;

public class EncryptorExample {
    public static void main(String[] args) {
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        encryptor.setPassword("yourSecretKey");
        String encryptedValue = encryptor.encrypt("yourSensitiveValue");
        System.out.println(encryptedValue);
    }
}
  • 在配置文件中使用加密值:将加密后的字符串放入配置文件,并用 ENC() 包裹。
spring.datasource.password=ENC(encryptedValue)

2. 使用 Spring Cloud Config Server 进行加密

Spring Cloud Config Server 提供了加密和解密功能,可对配置文件中的敏感信息进行加密。

步骤

  • 添加依赖:在 pom.xml 中添加 Spring Cloud Config Server 的依赖。
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>
  • 配置加密密钥:在 application.properties 中配置加密密钥。
encrypt.key=yourSecretKey
  • 启动 Config Server:在主类上添加 @EnableConfigServer 注解。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}
  • 加密配置值:使用 Config Server 提供的加密端点进行加密。
curl http://localhost:8888/encrypt -d yourSensitiveValue
  • 在配置文件中使用加密值:将加密后的字符串放入配置文件,不用额外包裹。
spring.datasource.password=encryptedValue

3. 使用 Vault 进行加密

HashiCorp Vault 是一个用于安全存储和访问敏感信息的工具,Spring Boot 可集成 Vault 来管理配置文件中的敏感信息。

步骤

  • 安装和启动 Vault:从 Vault 官网下载并安装 Vault,然后启动 Vault 服务。
  • 配置 Vault:初始化 Vault 并获取根令牌,配置 Vault 的存储后端和访问策略。
  • 添加依赖:在 pom.xml 中添加 Spring Cloud Vault 的依赖。
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-vault-config</artifactId>
</dependency>
  • 配置 Spring Boot 应用:在 application.properties 中配置 Vault 的连接信息和访问令牌。
spring.cloud.vault.uri=http://localhost:8200
spring.cloud.vault.token=yourRootToken
  • 存储敏感信息:使用 Vault 的命令行工具或 API 将敏感信息存储到 Vault 中。
vault kv put secret/myapp spring.datasource.password=yourSensitiveValue
  • 在应用中使用敏感信息:Spring Boot 应用会自动从 Vault 中获取配置信息。
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {
    @Value("${spring.datasource.password}")
    private String password;

    @GetMapping("/password")
    public String getPassword() {
        return password;
    }
}

以上这些方法都能有效保护 Spring Boot 配置文件中的敏感信息,你可以根据具体需求和场景选择合适的加密方式。