zl程序教程

您现在的位置是:首页 >  后端

当前栏目

jasypt-spring-boot:加密SpringBoot的敏感配置信息

2023-09-11 14:15:39 时间

jasypt 简介

jasypt 全称  Java Simplified Encryption

Jasypt为Spring Boot应用提供property sources的加密支持,可以加密的数据有:

  • system property
  • environment property
  • command line argument
  • application.properties
  • yaml properties
  • other custom property sources

哪些是敏感信息?

由于很多应用使用 配置文件 (eg:properties、yml) 来存储配置信息,配置中经常会涉及到许多敏感信息。

举几个小例子:

  • 普通应用密码信息,如:DB、Rabbit、Redis等
  • 特殊密码信息,如:Spring Cloud Config需要配置Git等VCS密码信息
  • 第三方通讯凭证信息,如:调用第三方接口发送短信的通讯凭证信息

由于各业务场景不同,因此敏感信息的定义也不同。

jasypt加解密 使用

前提:jasypt-1.9.0.jar

加密:

命令行

java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="123abc" password=FLY algorithm=PBEWithMD5AndDES

其中:

  • input的值就是原密码。
  • password的值就是参数jasypt.encryptor.password指定的值,即秘钥。

 Java代码

// 默认加密/解密算法是 PBEWithMD5AndDES
StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
encryptor.setPassword(KEY);
return encryptor.encrypt(text);

解密:

命令行

java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringDecryptionCLI input="+oE67TSfU/j7+Mr4oKPLYg==" password=FLY algorithm=PBEWithMD5AndDES

Java代码

StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
encryptor.setPassword(KEY);
return encryptor.decrypt(ciphertext);

jasypt 集成SpringBoot

第一种:jasypt-spring-boot-starter

如果你的Spring Boot应用程序使用了@SpringBootApplication或者@EnableAutoConfiguration注解

想要在整个Spring环境中启用加密属性,那么只需将jasypt-spring-boot-starter依赖项添加到项目中

这意味着任何系统属性,环境属性,命令行参数,application.properties,yaml属性和任何属性)其他自定义属性源可以包含加密属性:

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

总之

  1. 增加配置属性jasypt.encryptor.password = XXX,这是加密的秘钥;
  2. 所有明文密码替换为ENC(加密字符串),例如ENC(XW2daxuaTftQ+F2iYPQu0g==);
  3. 引入上文MAVEN依赖:jasypt-spring-boot-starter

注意:这种方式,不需要显示增加注解@EnableEncryptableProperties;

 

第二种:jasypt-spring-boot

如果您没有使用@SpringBootApplication@EnableAutoConfiguration注解,可以添加jasypt-spring-boo添加到你的项目:

<dependency>
        <groupId>com.github.ulisesbocchio</groupId>
        <artifactId>jasypt-spring-boot</artifactId>
        <version>2.1.1</version>
</dependency>

然后增加注解@EnableEncryptableProperties到你的启动类(Application.java)或者配置类上;

@Configuration
@EnableEncryptableProperties
public class MyApplication {
    ...
}

 使用这种配置方式,在整个Spring环境中,任何加密属性将是可用的。

这意味着任何系统属性,环境属性,命令行参数,application.properties,yaml属性和任何属性)其他自定义属性源可以包含加密属性。

总之:

  • Application.java上增加注解@EnableEncryptableProperties;
  • 增加配置文件jasypt.encryptor.password = XXX,这是加密的秘钥;
  • 所有明文密码替换为ENC(加密字符串),例如ENC(XW2daxuaTftQ+F2iYPQu0g==);
  • 引入上文MAVEN依赖:jasypt-spring-boot;

第三种:jasypt-spring-boot

如果您没有使用@SpringBootApplication@EnableAutoConfiguration注解,并且您不希望在整个Spring环境中启用加密属性,那么还有第三种选择。

首先将以下依赖项添加到项目中:

<dependency>
        <groupId>com.github.ulisesbocchio</groupId>
        <artifactId>jasypt-spring-boot</artifactId>
        <version>2.0.0</version>
</dependency>

 然后在配置文件中添加@EncryptablePropertySource。就像使用Spring的@PropertySource注释一样。例如:

@Configuration
@EncryptablePropertySource(name = "EncryptedProperties", value = "classpath:encrypted.properties")
public class MyApplication {
	...
}

 方便的是,@EncryptablePropertySources可以用来对类型的注释进行分组,如下所示:

	@Configuration
	@EncryptablePropertySources({@EncryptablePropertySource("classpath:encrypted.properties"),
	                             @EncryptablePropertySource("classpath:encrypted2.properties")})
	public class MyApplication {
		...
	}

另请注意,从1.8版开始,@EncryptablePropertySource支持YAML文件

 

小提示

启动类(Application.java)中@SpringBootApplication注解,是

@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan

三个注解的组合

其中@SpringBootConfiguration注解,包含@Configuration

其中@EnableAutoConfiguration注解,包含@AutoConfigurationPackage

 

参考链接:https://github.com/ulisesbocchio/jasypt-spring-boot