自学内容网 自学内容网

SpringBoot配置文件使用jasypt加密

SpringBoot配置文件使用jasypt加密

在项目中配置文件明文往往不安全,我们可以使用jasypt加密

1、引入依赖jasypt

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

2、在application.yml中新增jasypt配置,此处的password为密钥,加密时使用该密钥加密,此处采用DES对称加密算法。这里的password的值此处是16进制随机值

spring:
  application:
   name: orderService
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/tea_data?severTimezone=UTC
    username: root
    password: xu123
jasypt:
  encryptor:
    password: f0d15ddc

3、生成密文

package com.example.order;

import org.jasypt.encryption.StringEncryptor;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Random;

@SpringBootTest
class OrderServiceApplicationTests {
    @Autowired
    private StringEncryptor stringEncryptor;

    @Test
    void contextLoads() {
//        Random random=new Random();
//        System.out.println(Integer.toHexString(random.nextInt()));
        String url=stringEncryptor.encrypt("jdbc:mysql://localhost:3306/tea_data?serverTimezone=UTC");
        String userName=stringEncryptor.encrypt("root");
        String password=stringEncryptor.encrypt("xu123");
        System.out.println("url:"+url);
        System.out.println("userName:"+userName);
        System.out.println("password:"+password);

    }

}
 

4、复制控制台的密文,修改application.yml对应值,在前面加ENC,初始化时后台会自动解密
application.yml


spring:
  application:
   name: orderService
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: ENC(w1Teu1o/5A3viCdkQ3VkC6++hMwBrsGip37LnjTi0h0Xqa3MoQ3ilOxSbg9otQTuoTm/Z3bTPPFqsBlYFo+6aiNdyOe25oON5AqF0mycFxMreo3VVrkn8ZkpXETkUBYt)
    username: ENC(jSszHsa26QY5M/aI7aM6pNtQGcyEnPU0NJIHG5C4oN5uu00PGgkyh+Up/gHIIlah)
    password: ENC(VhlpQvPt9FUqy4sbpweQ5Q5PQ/mspFmOUV+RdjI4VGfekp/gK/2swL6b5cgnPWDa)
jasypt:
  encryptor:
    password: f0d15ddc

在这里插入图片描述

5、重启OrderServiceApplication,启动成功。

在这里插入图片描述


原文地址:https://blog.csdn.net/qq_38527427/article/details/142929413

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!