springboot項目通過jasypt-spring-boot-starter加密配置


1、pom.xml文件中加入

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

2、密文的生成

package com.xc.luckysheet.utils;


import org.jasypt.util.text.BasicTextEncryptor;

/**
 * jasypt-spring-boot-starter 生成密文的工具代碼
 * @author cr
 */
public class EncryptConfigUtil {
    public static void main(String[] args) {

        BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
        //加密所需的salt
        textEncryptor.setPassword("123456");
        //要加密的數據(數據庫的用戶名或密碼)
        String username = textEncryptor.encrypt("root");
        String password = textEncryptor.encrypt("123456");
        System.out.println("username:"+username);
        System.out.println("password:"+password);
    }
}

3、修改配置文件

spring:
  redis:
    host: 192.168.0.1
    port: 15556 password: ENC(r6YJrfKMAAzzltzmEpEYv7lzYINULq6g)
    timeout: 10000ms
    lettuce:
      pool:
        max-active: 8
        max-wait: -1ms
        max-idle: 8
        min-idle: 0
    database: 0

db:
  mysql:
    druid:
      url: jdbc:mysql://192.168.0.1:3307/luckysheetdb?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
      driverClassName: com.mysql.cj.jdbc.Driver
      username: ENC(opGC8sQt5JVH3j4VD5i2Ug) password: ENC(r6YUrfKMAAzzltzmApEYv7lZyILULq==)

4、使用

可配置在application.yml文件中(這種方式不安全)

jasypt.encryptor.password=123456

推薦在JVM啟動參數中設置

在idea中配置如下:

-Djasypt.encryptor.password=123456

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM