【Jasypt】Spring Boot 配置文件加解密

Jasypt是什么

首先,让我们解释一下什么是jasypt。jasypt是一款Java库,它提供了广泛使用的加密API,包括对称加密、非对称加密以及哈希算法。该库的主要用途是保护敏感数据的安全,例如数据库连接信息、用户名和密码等。

1、添加jasypt依赖

首先我们需要让我们的maven仓库有jasypt包,然后对他进行依赖,这样我们就可以使用他的功能了!

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

2、添加jasypt的配置

jasypt:
  encryptor:
    # 自定义加密盐值(密钥)
    password: jasypt
    # 加密算法设置
    algorithm: PBEWithMD5AndDES
    iv-generator-classname: org.jasypt.iv.RandomIvGenerator
    salt-generator-classname: org.jasypt.salt.RandomSaltGenerator

3、添加主要配置JasyptConfig

JasyptConfig用于初始化jasypt的配置

import org.jasypt.encryption.StringEncryptor;
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author **
 * @date 2023-09-19
 */
@Configuration(value = "jasyptConfig")
public class JasyptConfig {
    /**
     * 自定义 StringEncryptor,覆盖默认的 StringEncryptor
     */
    @Bean(value = "jasyptStringEncryptor")
    public StringEncryptor jasyptStringEncryptor() {
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        config.setPassword("jasypt");
        config.setAlgorithm("PBEWithMD5AndDES");
        config.setKeyObtentionIterations("1000");
        config.setPoolSize("1");
        config.setProviderName("SunJCE");
        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
        config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");
        config.setStringOutputType("base64");
        encryptor.setConfig(config);
        return encryptor;
    }
}

4、添加工具类JasyptUtils(也可以不添加,作用是加密你的密码)

注意:JasyptUtils的作用是把你的明文密码、用户名,进行算法处理得到加密后的密码。
当然也要注意你Config和yml里面的算法配置信息,以及盐值、IvGeneratorClassName、SaltGeneratorClassName,需要一致才能实现加密。(只实现功能直接copy,直接用就好了)


import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;

/**
 * @author **
 * @date 2023-09-19
 */
public class JasyptUtils {

    /**
     * 自己理解的加密盐值  也算是加密秘钥必须与配置文件中的一致
     */
    private static final String SALT_VALUE = "jasypt";

    /**
     * 加密
     * @param text    需要加密的字符串
     * @return 加密后的字符串
     */
    public static String encrypt(String text) {
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        encryptor.setConfig(cryptor());
        return encryptor.encrypt(text);
    }

    /**
     * 解密
     * @param text    需要解密的字符串
     * @return 解密后的字符串
     */
    public static String decrypt(String text) {
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        encryptor.setConfig(cryptor());
        return encryptor.decrypt(text);
    }

    /**
     * 配置(对应yml中的配置)
     * @return SimpleStringPBEConfig
     */
    public static SimpleStringPBEConfig cryptor() {
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        //设置盐值
        config.setPassword(SALT_VALUE);
        //设置算法配置信息
        config.setAlgorithm("PBEWithMD5AndDES");
        config.setKeyObtentionIterations("1000");
        config.setProviderName("SunJCE");
        config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");
        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
        config.setStringOutputType("base64");
        config.setPoolSize("1");
        return config;
    }


    public static void main(String[] args) {
        // 加密
        String encryptStr = encrypt("root123");
        // 解密
        String decryptStr = decrypt( encryptStr);
        System.out.println("加密后:" + encryptStr);
        System.out.println("解密后:" + decryptStr);
    }
}

5、使用

在你的配置文件中内容:

  datasource:
      # 主库数据源
      master:
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
        username: root
        password: root123

更换为:

  datasource:
      # 主库数据源
      master:
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
        username: ENC(arl9nMJgqPCllwlQ8KJ9XPwceuvX5KLY)
        password: ENC(ZtwpMLXax1gHufXalME2Pva2l5Oi+DCI)

arl9nMJgqPCllwlQ8KJ9XPwceuvX5KLY
这串加密内容是通过JasyptUtils 去执行出来的加密结果。

注意:必须添加       ENC ( 你加密后的内容 )

本文完结!

更多有趣内容可关注公众号
在这里插入图片描述

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring Boot中,可以使用加密算法来加密配置文件,以保护敏感信息的安全性。以下是一种常见的加密配置文件的方法: 1. 导入依赖:在`pom.xml`文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-crypto</artifactId> </dependency> ``` 2. 创建一个加密/解密工具类:可以使用`TextEncryptor`接口提供的实现类来进行加密和解密操作。例如,可以使用`Jasypt`库提供的实现。 3. 配置加密密钥:在`application.properties`或`application.yml`配置文件中,添加一个密钥属性。例如: ```properties jasypt.encryptor.password=mySecretKey ``` 4. 加密配置信息:可以使用上一步中配置的密钥来加密敏感信息。例如,可以在配置文件中使用`ENC(encryptedValue)`的形式来表示加密后的值。例如: ```properties my.property=ENC(encryptedValue) ``` 5. 解密配置信息:在需要访问敏感信息的地方,可以使用加密/解密工具类来解密配置值。例如,在Spring Boot应用程序中,可以使用`@Value`注解将解密后的值注入到属性中。 ```java @Value("${my.property}") private String myProperty; ``` 这样,敏感信息就会在配置文件中以加密形式存储,并且在应用程序中自动解密以供使用。 请注意,为了确保密钥的安全性,建议将密钥存储在安全的位置,并且不要将明文密钥直接硬编码在代码中。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值