Spring Boot + Mybatis-Plus + jasypt 数据库连接加密

2023年8月11日08:08:43

关于Spring Boot + Mybatis-Plus + jasypt 数据库连接加密

今天突然发现一个可以在连接数据库的时候将配置加密的东西,在此分享给广大朋友

  1. 首先我们导入相关的依赖
 <!--加密工具-->
 <dependency>
     <groupId>com.github.ulisesbocchio</groupId>
     <artifactId>jasypt-spring-boot-starter</artifactId>
     <version>2.1.0</version>
 </dependency>
  1. 之后我们在测试类中编写加密解密
    @Test
    void generateKey() {
        // 加密
        BasicTextEncryptor encryptor = new BasicTextEncryptor();
        encryptor.setPassword("abcd"); // 盐值
        String username = encryptor.encrypt("root");
        String password = encryptor.encrypt("1233");
        System.out.println("username: " + username);
        System.out.println("password: " + password);
        System.out.println(encryptor.decrypt(username));
        System.out.println(encryptor.decrypt(password));
    }
  1. 结果
    Spring Boot + Mybatis-Plus + jasypt 数据库连接加密
  2. 之后我们在yml中进行配置
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/jwt_security?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
    username: ENC(otk4Os0hjRq3ui44sSUgsA==)
    password: ENC(glqH/F/UyBJ4s/eMnaGDRQ==)
    
jasypt:
  encryptor:
    password: abcd #加密盐值
    algorithm: PBEWithMD5AndDES #加密算法设置
    iv-generator-classname: org.jasypt.iv.NoIvGenerator
  1. 最后我们进行测试
    Spring Boot + Mybatis-Plus + jasypt 数据库连接加密
  • 查询成功
  • 在此记录一次学习的一个过程
  • 作者:折木奉子
  • 原文链接:https://blog.csdn.net/qq_45590701/article/details/121891503
    更新时间:2023年8月11日08:08:43 ,共 1037 字。