SpringBoot整合Druid Mysql数据库密码加密

添加maven依赖

<!--数据库密码加密-->
       
         <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.21</version>
        </dependency>

密码加密工具类

public class DecryptDruid {
    public static void main(String[] args) throws Exception {
        testEncrypt("123456");
    }

    /**
     * 对指定的密码使用公钥进行解密
     *
     * @param passwd    加密后的密码
     * @param publicKey 公钥
     * @return 解密后的明文密码
     * @throws Exception 異常
     */
    public static String testDecrypt(String passwd, String publicKey) throws Exception {
        // 解密
        String decryptPassword = ConfigTools.decrypt(publicKey, passwd);
        System.out.println("decryptPassword:" + decryptPassword);
        return decryptPassword;
    }


    /**
     * 对指定的明文密码,生成公私钥进行加密
     *
     * @param password 明文密码
     * @return 密文密码
     * @throws Exception 異常
     */
    public static String testEncrypt(String password) throws Exception {
        String[] keyPair = ConfigTools.genKeyPair(512);
        //私钥
        String privateKey = keyPair[0];
        //公钥
        String publicKey = keyPair[1];
        //加密
        password = ConfigTools.encrypt(privateKey, password);

        System.out.println("privateKey:" + privateKey);
        System.out.println("publicKey:" + publicKey);
        System.out.println("password:" + password);
        // 解密操作
        testDecrypt(password, publicKey);
        return password;
    }

 配置yml配置文件

datasource:
    #    驱动
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://1.111.11.11:3306/abc?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useAffectedRows=true&useSSL=false&zeroDateTimeBehavior=convertToNull
    username: root
    password: jPvjuTx57IzAl16DTWsMlGGGcl49Qs8DOsvvnSuR1wmENxrvDeAOJIGBU4E0Ks5oNhE0ssA==
    publickey: MFwwDQYJKoZIhvcNAQEBBwzSl9QLoz5n7lpFQrk5CKCdqjZ1bBWgrCNiVFmxHJhpk1jhQtSsapebF501cCAwEAAQ==
    druid:
      connect-properties:
        config.decrypt: true
        config.decrypt.key: ${spring.datasource.publickey}
      filter:
        config:
          enabled: true # 启动ConfigFilter

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值