• 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
  • 相关阅读:
    [办公自动化]Excel表格求和始终显示#VALUE!
    Excel中如何对IP地址排序(linux 中呢?用sort)
    勇士通关——记多年前的面试
    [听听音乐]路,李知蹊
    [办公自动化]PPT演示时加个动画倒计时
    [网络安全]burp 转换ascii编码
    [学习笔记]掌控安全的课程学习笔记:20191228
    [生活随笔]起死回生的九阳破壁机
    [IT新应用]如何拯救死机的苹果手机(iPhone X)
    八月份课堂--Python爬虫(Spider)基础
  • 原文地址:https://www.cnblogs.com/lizm166/p/16813511.html
Copyright © 2020-2023  润新知