当前位置:   article > 正文

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

jasypt-spring-boot

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代码

  1. // 默认加密/解密算法是 PBEWithMD5AndDES
  2. StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
  3. encryptor.setPassword(KEY);
  4. 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代码

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

jasypt 集成SpringBoot

第一种:jasypt-spring-boot-starter

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

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

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

  1. <dependency>
  2. <groupId>com.github.ulisesbocchio</groupId>
  3. <artifactId>jasypt-spring-boot-starter</artifactId>
  4. <version>2.1.1</version>
  5. </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添加到你的项目:

  1. <dependency>
  2. <groupId>com.github.ulisesbocchio</groupId>
  3. <artifactId>jasypt-spring-boot</artifactId>
  4. <version>2.1.1</version>
  5. </dependency>

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

  1. @Configuration
  2. @EnableEncryptableProperties
  3. public class MyApplication {
  4. ...
  5. }

 使用这种配置方式,在整个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环境中启用加密属性,那么还有第三种选择。

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

  1. <dependency>
  2. <groupId>com.github.ulisesbocchio</groupId>
  3. <artifactId>jasypt-spring-boot</artifactId>
  4. <version>2.0.0</version>
  5. </dependency>

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

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

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

  1. @Configuration
  2. @EncryptablePropertySources({@EncryptablePropertySource("classpath:encrypted.properties"),
  3. @EncryptablePropertySource("classpath:encrypted2.properties")})
  4. public class MyApplication {
  5. ...
  6. }

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

 

小提示

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

  1. @SpringBootConfiguration
  2. @EnableAutoConfiguration
  3. @ComponentScan

三个注解的组合

其中@SpringBootConfiguration注解,包含@Configuration

其中@EnableAutoConfiguration注解,包含@AutoConfigurationPackage

 

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/945652
推荐阅读
相关标签
  

闽ICP备14008679号