Springcloud Nacos加密配置中心的配置数据库用户密码

Springcloud Nacos加密配置中心的配置数据库用户密码

1、引入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
</dependency>
 
<dependency>
    <groupId>com.github.ulisesbocchio</groupId>
    <artifactId>jasypt-spring-boot-starter</artifactId>
    <version>3.0.2</version>
</dependency>

2、启动配置注解

在Application主类中加入启动注解

@EnableEncryptableProperties

3、配置yml里面的加密算法

jasypt:
  encryptor:
    # 加密英子 自定义随机字符串
    password: 3b44347899385279a53a3abb1f29f05b
    # 加密算法 
    algorithm: PBEWithHmacSHA512AndAES_128

4.1、方式一使用工具类生成明文的加密配置

package com.rjh.designer.user.util;
 
import org.jasypt.encryption.StringEncryptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
 
/**
 * @author AL
 * @date 2022/6/28 16:18
 */
@SpringBootTest
public class EncryptorUtil {
 
    @Autowired
    private StringEncryptor stringEncryptor;
 
    @org.junit.jupiter.api.Test
    void encryptPwd() {
 
        // 给原账号生成加密后的用户名
        String username = stringEncryptor.encrypt("root");
 
        // 给原密码生成加密后的密码
        String pwd = stringEncryptor.encrypt("123456");
        System.err.println("------------username----------------");
        System.err.println(username);
        System.err.println("------------pwd----------------");
        System.err.println(pwd);
    }
}

4.2、方式二,用命令行生成明文的密文

找到jasypt.jar包,在此文件夹下执行

 
// input(必填):要加密的明文
// password(必填):随机加密因子
// algorithm:加密算法
// ivGeneratorClassName:org.jasypt.iv.RandomIvGenerator(algorithm=PBEWithHmacSHA512AndAES_128的时候需要得用这个)
 
java -cp jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input=123456 password=3b44347899385279a53a3abb1f29f05b algorithm=PBEWithHmacSHA512AndAES_128 ivGeneratorClassName=org.jasypt.iv.RandomIvGenerator
 

5、生成的加秘密文配置到数据库连接ym配置文件里。

格式为:ENC(加密后的密文)

  datasource:
#    username: root
#    password: 123456
    username: ENC(6mt1ykgDsQNN9kttrvihKzPgNF9d2hlkRBr6zM5eUq/Zzr2Z1H1aHwJD2vGF8WJ2)
    password: ENC(qTE6iEN1qFBmCWeOeCmxoJmlrWPhBZS9QZ3JlAIYRfM/Hh4BOS7h8ekmWSJbgcqf)
    url: jdbc:mysql://127.0.0.1:3306/user?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource

 

posted @ 2022-10-21 15:00  整合侠  阅读(3327)  评论(0编辑  收藏  举报