• 基于 jasypt 实现spring boot 配置文件脱敏


    前言

    在项目构建过程中,保护敏感信息的安全性至关重要,为了提高系统的安全性能,我们采用了Jasypt来对配置文件中的敏感信息进行加密处理,以确保系统的机密信息不被轻易泄露。

    步骤

    1. 添加Maven依赖

    首先,我们需要添加Maven依赖,以引入Jasypt库:

           <!-- jasypt 配置文件脱敏-->
            <dependency>
                <groupId>com.github.ulisesbocchio</groupId>
                <artifactId>jasypt-spring-boot-starter</artifactId>
                <version>3.0.3</version>
            </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    1. 创建加密解密工具类

    编写解密工具类:

    package com.aq.common.utils;
    
    import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
    import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
    
    /**
     * @Author ShipleyZeng
     * @Date 2023/10/1 16:39
     * @ClassName: JasyptUtil
     */
    public class JasyptUtils {
        /**
         * Jasypt生成加密结果
         * @param password 配置文件中设定的加密盐值
         * @param value 加密值
         * @return
         */
        public static String encryptPwd(String password, String value){
            PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
            encryptor.setConfig(cryptor(password));
            String result = encryptor.encrypt(value);
            return result;
        }
    
        /**
         * 解密
         * @param password 配置文件中设定的加密盐值
         * @param value 解密密文
         * @return
         */
        public static String decryptPwd(String password, String value){
            PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
            encryptor.setConfig(cryptor(password));
            String result = encryptor.decrypt(value);
            return result;
        }
    
        public static SimpleStringPBEConfig cryptor(String password){
            SimpleStringPBEConfig config = new SimpleStringPBEConfig();
            config.setPassword(password);
            // 指定加密算法,这里使用了PBE(Password-Based Encryption)算法,采用MD5和DES算法。
            config.setAlgorithm("PBEWithMD5AndDES");
            // 设置密钥获取迭代次数
            config.setKeyObtentionIterations("1000");
            // 密钥池大小 1代表使用密钥池
            config.setPoolSize("1");
            // 加密提供程序的名称
            config.setProviderName("SunJCE");
            // 盐
            config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
            // 加密后输出的数据类型
            config.setStringOutputType("base64");
            return config;
    
        }
    
        public static void main(String[] args) {
        	// 对密码123456加密
            String jasypt = encryptPwd("jasypt", "123456");
    
            System.out.println(jasypt);
    
    		// 对得到的密文解密 运行成功
            String jasypt1 = decryptPwd("jasypt", "1DFRBJI+4S+SiCtcqK0IrA==");
    
            System.out.println(jasypt1);
    
    
        }
    }
    
    
    • 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
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    1. 将得到的密文替换配置文件进行运行 我这边使用的是mysql数据库的密码加密
      在这里插入图片描述
    2. 启动项目 结果发现报错了
      在这里插入图片描述
    3. 原因是官方在 3.0.0 以后更改了加密算法,所以你不设置的话,使用网上的方法加密出来的密码启动就会报错(官方 issue:Failed to bind properties under ‘spring.datasource.password’ to java.lang.String` #154)
      在这里插入图片描述
    4. 在配置文件当中加入下配置可以解决启动项目报错的问题
    # jasypt 密码加密配置
    jasypt:
      encryptor:
        # 加密盐值
        password: jasypt
        # 加密算法设置 3.0.0 以后
        algorithm: PBEWithMD5AndDES
        iv-generator-classname: org.jasypt.iv.NoIvGenerator
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    1. 调用本地的查询接口、访问数据库查询到了用户信息
      在这里插入图片描述
    2. 在项目中配置Jasypt的参数(如加密盐值和加密算法),系统知道如何进行解密操作.Spring Boot会在启动过程中自动检测配置文件中使用ENC()标识的属性值,然后调用Jasypt进行解密。这是因为Spring Boot具有属性注入和解析的能力,可以在运行时检测并处理这些加密的属性。
  • 相关阅读:
    力扣第 302 场周赛复盘
    5. 线性回归的从零开始实现
    Springboot学生作业管理系统毕业设计-附源码251208
    千呼万唤openGauss资源池化系列培训来了
    学术分享 | 清华大学 康重庆:电力系统碳计量技术与应用(Matlab代码实现)
    TMS320F28374S之CMPSS
    2022最新Java后端面试题(带答案),重点都给画出来了!你不看?
    时间序列预测:用电量预测 02 KNN(K邻近算法)
    Unity-Resources资源同步加载
    Redis的8种内存淘汰机制
  • 原文地址:https://blog.csdn.net/aq_money/article/details/133467000