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


    1.Jasypt是什么

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

    2.使用

    2.1 依赖

    
    <parent>
    	<groupId>org.springframework.bootgroupId>
    	<artifactId>spring-boot-starter-parentartifactId>
    	<version>2.5.3version>
    	<relativePath/> 
    parent>
    
    
    <dependency>
    	<groupId>com.github.ulisesbocchiogroupId>
    	<artifactId>jasypt-spring-boot-starterartifactId>
    	<version>3.0.3version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    2.2 实现类

    1. 自定义的属性探测器和密码解析器【作用是识别加密后的对象和解密】
    /**
     * 自定义的属性探测器和密码解析器
     */
    @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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    1. 自定义的属性探测器实现【可以设置被加密对象】
    /**
     * 自定义的属性探测器
     */
    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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    1. 自定义的密码解析器【解密自定义的加密对象】EncryptionUtil.toDecrypt()就是自定义的解密方法,与加密对象的加密方法相对应。
    /**
     * 自定义的密码解析器
     */
    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);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    2.3 加密配置

    spring:
      datasource:
        dynamic:
          datasource:
            # 主库数据源
            master:
              driver-class-name: org.postgresql.Driver
              url: ENC@URLENCStr
              username: ENC@UsernameENCStr
              password: ENC@PasswordENCStr
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    3.总结

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

  • 相关阅读:
    Spring源码分析-扩展点-配置文件自定义标签
    JAVA前端开发介绍
    KingbaseESV8R6 snapshot too old的配置和测试
    【智能家居-大模型】构建未来,聆思大模型智能家居交互解决方案正式发布
    JS中的栈和堆
    muduo库中实现Protbuf编码器与消息分发器
    性能测试怎么做?性能测试重点和各项性能测试流程(超级详细)
    基于匹配追踪和最大重叠离散小波变换的ECG心电信号R波检测(MATLAB 2018a)
    【云原生之kubernetes实战】在k8s环境下部署kanboard项目管理平台
    [手写spring](4)实现后置处理器
  • 原文地址:https://blog.csdn.net/weixin_39168541/article/details/134433893