SpringBoot【集成 jasypt】实现配置信息自定义加解密(自定义的属性探测和密码解析器)

1.Jasypt是什么

Jasypt是一个Java简易加密库,用于加密配置文件中的敏感信息,如数据库密码。它可以帮助开发人员在应用程序中加密密码、敏感信息和数据通信,还包括高安全性、基于标准的加密技术、可同时单向和双向加密的加密密码、文本、数字和二进制文件。如果您正在使用Spring Boot,Jasypt可以与Spring Boot集成,使加密和解密过程更加简单。

2.使用

2.1 依赖

xml 复制代码
<!-- SpringBoot 版本 -->
<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.5.3</version>
	<relativePath/> 
</parent>

<!-- jasypt 加密 -->
<dependency>
	<groupId>com.github.ulisesbocchio</groupId>
	<artifactId>jasypt-spring-boot-starter</artifactId>
	<version>3.0.3</version>
</dependency>

2.2 实现类

  1. 自定义的属性探测器和密码解析器【作用是识别加密后的对象和解密】
java 复制代码
/**
 * 自定义的属性探测器和密码解析器
 */
@Component
public class CustomEncryptableProperty {
    @Bean(name = "encryptablePropertyDetector")
    public EncryptablePropertyDetector encryptablePropertyDetector() {
        return new CustomEncryptablePropertyDetector();
    }
    @Bean("encryptablePropertyResolver")
    public EncryptablePropertyResolver encryptablePropertyResolver(EncryptablePropertyDetector encryptablePropertyDetector) {
        return new CustomEncryptablePropertyResolver(encryptablePropertyDetector);
    }
}
  1. 自定义的属性探测器实现【可以设置被加密对象】
java 复制代码
/**
 * 自定义的属性探测器
 */
public class CustomEncryptablePropertyDetector implements EncryptablePropertyDetector {

    /**
     * 探测字符串
     */
    private final String flagStr = "ENC@";

    /**
     * 是否为可以解密的字符串【自定义规则为 flagStr 开头】
     *
     * @param value 全部的字符串
     * @return 是否是解密的字符串,true,是,false,否
     */
    @Override
    public boolean isEncrypted(String value) {
        if (value != null) {
            return value.startsWith(flagStr);
        }
        return false;
    }

    /**
     * 截取到除了标识之后的值【截取 flagStr 之后的字符串】
     *
     * @param value 带前缀
     * @return string 去掉标识符的字符串
     */
    @Override
    public String unwrapEncryptedValue(String value) {
        return value.substring(flagStr.length());
    }
}
  1. 自定义的密码解析器【解密自定义的加密对象】EncryptionUtil.toDecrypt()就是自定义的解密方法,与加密对象的加密方法相对应。
java 复制代码
/**
 * 自定义的密码解析器
 */
public class CustomEncryptablePropertyResolver implements EncryptablePropertyResolver {

    /**
     * 属性探测器
     */
    private final EncryptablePropertyDetector detector;

    public CustomEncryptablePropertyResolver(EncryptablePropertyDetector detector) {
        this.detector = detector;
    }

    /**
     * 处理真正的解密逻辑
     *
     * @param value 原始值
     * @return 如果值未加密,返回原值,如果加密,返回解密之后的值
     */
    @Override
    public String resolvePropertyValue(String value) {
        return Optional.ofNullable(value)
                .filter(detector::isEncrypted)
                .map(resolvedValue -> {
                    try {
                        // 1.过滤加密规则后的字符串
                        String unwrappedProperty = detector.unwrapEncryptedValue(resolvedValue.trim());
                        // 2.解密
                        return EncryptionUtil.toDecrypt(unwrappedProperty);
                    } catch (EncryptionOperationNotPossibleException e) {
                        throw new DecryptionException("Unable to decrypt: " + value + ". Decryption of Properties failed,  make sure encryption/decryption " +
                                "passwords match", e);
                    }
                })
                .orElse(value);
    }
}

2.3 加密配置

yaml 复制代码
spring:
  datasource:
    dynamic:
      datasource:
        # 主库数据源
        master:
          driver-class-name: org.postgresql.Driver
          url: ENC@URLENCStr
          username: ENC@UsernameENCStr
          password: ENC@PasswordENCStr

3.说明

用户名及密码甚至是URL使用密文的安全性是很高的,本文参考知乎软件架构师:代码小咖SpringBoot 配置文件这样加密,才足够安全!,感谢大佬的分享,Jasypt 的使用还有很多自定义的方式,可查看原文学习。

4.小小的总结

本文介绍了如何使用Jasypt加密库实现Spring Boot配置信息的安全加密。Jasypt是一个Java简易加密库,可加密敏感信息如数据库密码。文章详细说明了集成步骤:首先引入Jasypt依赖,然后实现自定义加密处理类(包括属性探测器、密码解析器等),最后在配置文件中使用ENC@前缀标识加密内容。该方法通过自定义加解密逻辑,有效提升了配置文件安全性。参考了知乎相关技术文章,为Spring Boot应用提供了一种可靠的安全配置方案。

相关推荐
一 乐15 小时前
婚纱摄影网站|基于ssm + vue婚纱摄影网站系统(源码+数据库+文档)
前端·javascript·数据库·vue.js·spring boot·后端
码事漫谈16 小时前
Protocol Buffers 编码原理深度解析
后端
码事漫谈16 小时前
gRPC源码剖析:高性能RPC的实现原理与工程实践
后端
踏浪无痕17 小时前
AI 时代架构师如何有效成长?
人工智能·后端·架构
程序员小假17 小时前
我们来说一下无锁队列 Disruptor 的原理
java·后端
武子康19 小时前
大数据-209 深度理解逻辑回归(Logistic Regression)与梯度下降优化算法
大数据·后端·机器学习
maozexijr19 小时前
Rabbit MQ中@Exchange(durable = “true“) 和 @Queue(durable = “true“) 有什么区别
开发语言·后端·ruby
源码获取_wx:Fegn089519 小时前
基于 vue智慧养老院系统
开发语言·前端·javascript·vue.js·spring boot·后端·课程设计
独断万古他化19 小时前
【Spring 核心: IoC&DI】从原理到注解使用、注入方式全攻略
java·后端·spring·java-ee
毕设源码_郑学姐19 小时前
计算机毕业设计springboot基于HTML5的酒店预订管理系统 基于Spring Boot框架的HTML5酒店预订管理平台设计与实现 HTML5与Spring Boot技术驱动的酒店预订管理系统开
spring boot·后端·课程设计