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

 

 

 

 

 

 

 

posted @ 2021-02-04 22:12  桥头堡洗脚城  阅读(8801)  评论(0编辑  收藏  举报