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

简介: 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>

总之

  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

 


目录
相关文章
|
13天前
|
SQL Java 数据库连接
(自用)Spring常用配置
(自用)Spring常用配置
16 0
|
1天前
|
Java Nacos 开发者
Java从入门到精通:4.2.1学习新技术与框架——以Spring Boot和Spring Cloud Alibaba为例
Java从入门到精通:4.2.1学习新技术与框架——以Spring Boot和Spring Cloud Alibaba为例
|
6天前
|
小程序 JavaScript Java
基于SpringBoot+Vue+uniapp微信小程序的学生公寓电费信息的详细设计和实现
基于SpringBoot+Vue+uniapp微信小程序的学生公寓电费信息的详细设计和实现
27 1
|
6天前
|
存储 安全 Java
第2章 Spring Security 的环境设置与基础配置(2024 最新版)(下)
第2章 Spring Security 的环境设置与基础配置(2024 最新版)(下)
16 0
|
6天前
|
安全 Java 数据库
第2章 Spring Security 的环境设置与基础配置(2024 最新版)(上)
第2章 Spring Security 的环境设置与基础配置(2024 最新版)
28 0
|
7天前
|
安全 Java Spring
Spring Security 5.7 最新配置细节(直接就能用),WebSecurityConfigurerAdapter 已废弃
Spring Security 5.7 最新配置细节(直接就能用),WebSecurityConfigurerAdapter 已废弃
19 0
|
7天前
|
安全 Java 应用服务中间件
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
24 0
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
|
10天前
|
消息中间件 运维 供应链
springboot区域云HIS医院信息综合管理平台源码
云HIS系统分为两个大的系统,一个是基层卫生健康云综合管理系统,另一个是基层卫生健康云业务系统。基层卫生健康云综合管理系统由运营商、开发商和监管机构使用,用来进行运营管理、运维管理和综合监管。基层卫生健康云业务系统由基层医院使用,用来支撑医院各类业务运转。
20 2
|
13天前
|
JSON Java 数据库连接
属性注入掌握:Spring Boot配置属性的高级技巧与最佳实践
属性注入掌握:Spring Boot配置属性的高级技巧与最佳实践
22 1
|
13天前
|
Java 数据库连接 Spring
简化配置,提高灵活性:Spring中的参数化配置技巧
简化配置,提高灵活性:Spring中的参数化配置技巧
19 0