SpringBoot配置文件中密码属性加密的实现
作者:洒脱的智障 发布时间:2022-07-08 18:32:03
本文主要介绍了SpringBoot配置文件中的明文密码如何加密保存,读取以及对于自定义的加密算法加密的参数如何保存和读取。
背景
为了安全的需要,一些重要的信息比如数据库密码不能明文保存在配置文件中,需要进行加密之后再保存。SpringBoot可以使用jasypt-spring-boot这个组件来为配置属性提供加密的支持。
集成jasypt-spring-boot到项目中
根据官方README文档,可以有三种方式集成jasypt-spring-boot到项目中。
对于SpringBoot项目,直接通过引入jasypt-spring-boot-starter,然后所有的application.properties, application-*.properties, yaml的配置文件中就可以包含加密的属性。
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>3.0.4</version>
</dependency>
直接引入jasypt-spring-boot,这时候需要在启动类上添加一个@EnableEncryptableProperties,然后在配置文件中可以包含有加密的字段属性。
@SpringBootApplication
@EnableEncryptableProperties
public class Application {
...
}
如果不想要整个Spring的配置文件都启用加密的字段属性,还可以自己指定对应加密的配置文件路径。也需要引入jasypt-spring-boot,同时在启动类上添加@EncryptablePropertySource注解,设置注解的value属性为需要读取的加密配置文件路径。
@SpringBootApplication
@EncryptablePropertySource({"classpath:encrypted.properties"})
public class Application {
...
}
配置文件配置加密与读取
现在知道了如何集成jasypt-spring-boot到项目中,下面就介绍一下如何加密明文的密码,以及需要如何存储在配置文件中。
首先,需要添加一个maven的插件,这个插件可以帮助我们加密我们需要的明文信息。
<plugin>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-maven-plugin</artifactId>
<version>3.0.4</version>
</plugin>
之后,执行如下的mvn命令,可以在控制台得到对于的密文。然后将配置文件中明文替换为加密后的密文。
mvn jasypt:encrypt-value -Djasypt.encryptor.password="TKzhc3fz" -Djasypt.plugin.value="123456"
-Djasypt.encryptor.password参数指定用于加密密码,我理解这个应该和秘钥类似。
-Djasypt.plugin.value参数指定需要加密的明文参数。
执行命令后,可以在控制台看到加密后的密文,密文默认是用ENC()格式包围住的,当然这个格式也可以自定义。
也可以通过这个mvn插件解密,执行如下命令,可以在控制台看到解密之后的明文。
mvn jasypt:decrypt-value -Djasypt.encryptor.password="TKzhc3fz" -Djasypt.plugin.value="ENC(Muhq57xdiHA5jJX9pX7zmNz57w4emX2D/XYIXOMEx0LYcTL7RYyadWe2J7GCi9KJ)"
在SpringBoot配置文件中,添加jasypt.encryptor.password属性,这个值和第二步生成密文的值要一样,不然解密会失败。
jasypt:
encryptor:
password: TKzhc3fz # 设置加密的password信息 类似秘钥?
test:
password: ENC(Muhq57xdiHA5jJX9pX7zmNz57w4emX2D/XYIXOMEx0LYcTL7RYyadWe2J7GCi9KJ) # 测试数据
这样在主程序启动的时候,通过@Value注解就可以自动解密配置文件中的密文信息了,这样就完成了明文的加密以及后续的读取。
工作原理简析
参照官方文档,大概的工作原理如下:
首先,在Spring容器启动之后,会遍历配置文件中的所有的配置,然后发现所有按照jasypt约定规则加密的属性,此处就是使用ENC()包围的参数,这个ENC()前缀和后缀是可以自定义的,下面会讲到。
这边主要是有两个接口,分别是EncryptablePropertyDetector、EncryptablePropertyResolver,这两个接口根据名称可以看出来一个是发现器,一个是分解器。
先来看EncryptablePropertyDetector这个接口,这个接口提供了两个方法,isEncrypted和unwrapEncryptedValue,isEncrypted方法判断是否是jasypt约定规则加密的属性,unwrapEncryptedValue方法会返回去除掉前缀和后缀的真正加密的值,可以看下该接口默认的实现DefaultPropertyDetector:
/**
* Default property detector that detects encrypted property values with the format "$prefix$encrypted_value$suffix"
* Default values are "ENC(" and ")" respectively.
*
* @author Ulises Bocchio
*/
public class DefaultPropertyDetector implements EncryptablePropertyDetector {
// 默认的前缀和后缀
private String prefix = "ENC(";
private String suffix = ")";
public DefaultPropertyDetector() {
}
public DefaultPropertyDetector(String prefix, String suffix) {
Assert.notNull(prefix, "Prefix can't be null");
Assert.notNull(suffix, "Suffix can't be null");
this.prefix = prefix;
this.suffix = suffix;
}
// 判断配置属性是否是按照jasypt约定规则加密的属性
@Override
public boolean isEncrypted(String property) {
if (property == null) {
return false;
}
final String trimmedValue = property.trim();
return (trimmedValue.startsWith(prefix) &&
trimmedValue.endsWith(suffix));
}
// 去掉默认的前缀和后缀,返回加密的值
@Override
public String unwrapEncryptedValue(String property) {
return property.substring(
prefix.length(),
(property.length() - suffix.length()));
}
}
EncryptablePropertyResolver这个接口中只提供了一个方法resolvePropertyValue,这个方法会遍历配置文件属性,判断是否是加密属性,然后进行解密返回明文。在默认实现DefaultPropertyResolver中,依赖EncryptablePropertyDetector以及StringEncryptor,真正解密的方法是写在StringEncryptor,这边具体如何解密就不详细描述了,有兴趣可以自行看下。DefaultPropertyResolver类:
/**
* @author Ulises Bocchio
*/
public class DefaultPropertyResolver implements EncryptablePropertyResolver {
private final Environment environment;
// 加密和解密的实现
private StringEncryptor encryptor;
// jasypt默认发现器
private EncryptablePropertyDetector detector;
public DefaultPropertyResolver(StringEncryptor encryptor, Environment environment) {
this(encryptor, new DefaultPropertyDetector(), environment);
}
public DefaultPropertyResolver(StringEncryptor encryptor, EncryptablePropertyDetector detector, Environment environment) {
this.environment = environment;
Assert.notNull(encryptor, "String encryptor can't be null");
Assert.notNull(detector, "Encryptable Property detector can't be null");
this.encryptor = encryptor;
this.detector = detector;
}
@Override
public String resolvePropertyValue(String value) {
// 该方法获取加密的属性,然后使用StringEncryptor解密并返回
return Optional.ofNullable(value)
.map(environment::resolvePlaceholders)
.filter(detector::isEncrypted) // 过滤加密属性
.map(resolvedValue -> {
try {
// 去除前缀和后缀获取真正加密的值
String unwrappedProperty = detector.unwrapEncryptedValue(resolvedValue.trim());
String resolvedProperty = environment.resolvePlaceholders(unwrappedProperty);
// 解密获得明文
return encryptor.decrypt(resolvedProperty);
} catch (EncryptionOperationNotPossibleException e) {
throw new DecryptionException("Unable to decrypt property: " + value + " resolved to: " + resolvedValue + ". Decryption of Properties failed, make sure encryption/decryption " +
"passwords match", e);
}
})
.orElse(value);
}
}
使用自定义的加密算法
如果不想要使用jasypt工具中的加密算法,或者内部要求使用某种特定的加密算法,jasypt-spring-boot组件也提供了自定义加解密的实现方式。上面在工作原理简析中提到了两个接口EncryptablePropertyDetector、EncryptablePropertyResolver,我们可以通过自己实现这两个接口的方式,并且指定对应的bean名称为encryptablePropertyDetector和encryptablePropertyResolver来覆盖框架提供的默认实现,完成加密算法和前缀后缀的自定义。
这边我就用base64加密算法举例,实现自定义的加密算法:
自己实现EncryptablePropertyDetector、EncryptablePropertyResolver接口,并且交给Spring管理,设置bean名称为encryptablePropertyDetector和encryptablePropertyResolver。
重写接口对应的方法。
@Component("encryptablePropertyDetector")
public class Base64EncryptablePropertyDetector implements EncryptablePropertyDetector {
private static final String PREFIX = "password:";
@Override
public boolean isEncrypted(String property) {
if (property == null) {
return false;
}
return property.startsWith(PREFIX);
}
@Override
public String unwrapEncryptedValue(String property) {
return property.substring(PREFIX.length());
}
}
@Component("encryptablePropertyResolver")
public class Base64EncryptablePropertyResolver implements EncryptablePropertyResolver {
@Autowired
private Base64EncryptablePropertyDetector encryptablePropertyDetector;
@Override
public String resolvePropertyValue(String value) {
return Optional.ofNullable(value)
.filter(encryptablePropertyDetector::isEncrypted)
.map(resolveValue -> {
final String unwrapEncryptedValue = encryptablePropertyDetector.unwrapEncryptedValue(resolveValue);
return new String(Base64.getDecoder().decode(unwrapEncryptedValue),
StandardCharsets.UTF_8);
})
.orElse(value);
}
}
配置文件中的加密属性使用自定义的前缀和后缀。这边明文先使用base64加密,之后加上“password:”前缀:
jasypt:
encryptor:
password: TKzhc3fz # 设置加密的password信息 类似秘钥?
test:
password: password:MTIzNDU2 # 测试数据
启动和读取。
来源:https://blog.csdn.net/qq_32238611/article/details/122724605


猜你喜欢
- 废话不多说了,一切尽在代码中,具体代码如下所示:界面<?xml version="1.0" encoding=&q
- 公司的svn的地址改变了,怎么办呢。自己本地的正在修改的项目怎么办呢?修改一下svn的服务器地址咯。1.就是先关闭ide,重新打开,然后选择
- 一、@Configuration注解1、基本使用自定义配置类/** * 1、@Configuration 告诉SpringBoot这是一个配
- 前言RecyclerView几乎在每个app里面都有被使用,但凡使用了列表就会采用分页加载进行数据请求和加载。android 官方也推出了分
- 本文实例为大家分享了安卓Button按钮的四种点击事件,供大家参考,具体内容如下第一种:内部类实现 1.xml里面先设置Button属性&l
- 1.打开项目主界面,任意打开一个类文件,如MainActivity.java,不要打开布局文件的disign界面2.点击File-->
- 文件上传大小设置#文件大小 MB必须大写# maxFileSize 是单个文件大小# maxRequestSize是
- 这个问题属于非常初级的问题,但是对于初学不知道的人可能会比较头疼。C++ 中函数是不能直接返回一个数组的,但是数组其实就是指针,所以可以让函
- 对于大规模乱序的数组,插入排序很慢,因为它只会交换相邻的元素,因此元素只能一点一点地从数组地一段移动到另一端。希尔排序改进了插入排序,交换不
- 将10个整数按由小到大的顺序排列#include <iostream>using namespace std;int main(
- 前言该文章为对工作中部分业务实现的总结,阅读时间:20分钟,版本:Android 6.0 - 9.0 update time 2021年02
- 最近公司要求开发工具要用Idea,作为一个eclipse的老员工,记录一下Idea中遇到的坑刚开始用Idea从Git上导入一个项目时,遇到了
- 对Android的SD卡进行读取权限设置时: <uses-permission android:name="android.
- 一、前置说明本节大纲使用lombok插件的好处如何安装lombok插件使用lombok提高开发效率二、使用lombok插件的好处我们在jav
- 本文实例讲述了C#调用存储过程的方法。分享给大家供大家参考,具体如下:CREATE PROCEDURE [dbo].[GetNameById
- android通过google API获取天气信息public class WeatherActivity extends Activity
- ArrayList类List集合的实例化:List<String> l = new ArrayList<String>
- swing自带的窗体是不能够满足我们的应用需求的,所以需要制作任意图片和形状的JFrame框体,比如下图:并且可以设置窗体背景图片的透明度下
- 先来看一段魔法吧public class Test { private static void changeStr
- 注意:要先导入javamail的mail.jar包。以下三段代码是我的全部代码,朋友们如果想用,直接复制即可。第一个类:MailSender