jasypt-spring-boot:加密SpringBoot的敏感配置信息
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>
总之
- 增加配置属性jasypt.encryptor.password = XXX,这是加密的秘钥;
- 所有明文密码替换为ENC(加密字符串),例如ENC(XW2daxuaTftQ+F2iYPQu0g==);
- 引入上文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
相关文章
- Spring实战(第4版).pdf - 百度云资源
- SpringBoot:同一个类中调用另一个方法没有触发 Spring AOP
- springboot学习章节代码-spring高级话题
- springboot学习章节代码-spring基础
- Spring Boot 2.x :通过 spring-boot-starter-hbase 集成 HBase
- Spring异常解决 java.lang.NullPointerException,配置spring管理hibernate时出错
- springboot+vue实现前后端分离之前端vue部分(spring boot 2.5.4/vue.js 3.2.4)
- springboot+vue实现前后端分离之后端spring部分(spring boot 2.5.4/vue.js 3.2.4)
- Java SpringBoot 应用使用命令行 mvn spring-boot run 启动的原理
- 【springboot系列】Spring Boot 实现接口幂等性的 4 种方案
- 【SpringBoot系列】Spring Boot+Redis 分布式锁:模拟抢单
- SpringBoot 异常回滚 事务的使用___Springboot @Transactional 事务不回滚
- Spring Boot 之spring.factories
- 《Springboot极简教程》使用Spring Boot, JPA, Mysql, ThymeLeaf,gradle, Kotlin快速构建一个CRUD Web App
- 001-Spring Cloud Edgware.SR3 升级最新 Finchley.SR1,spring boot 1.5.9.RELEASE 升级2.0.4.RELEASE注意问题点
- 005-spring cache-原理、缓存AOP机制、Spring Cache抽象集成机制、springboot自动配置机制
- Spring面试题/SpringBoot面试题
- SpringBoot实例②springboot+jdbcTemplate小例子实现增删查改
- 【Spring Boot】SpringBoot项目jar、war包启动解析
- 【Spring Boot】SpringBoot 如何保证接口安全?老鸟们都是这么玩的!