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

 

原文地址:http://www.cnblogs.com/lizm166/p/16813511.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性