Druid加密数据库密码

1.借助Druid中的ConfigTools工具类来加密数据库对应的密码:

public class DruidEncryptUtil {

    public static String publicKey;
    public static String privateKey;

    static {
        try {
            String[] keyPair = ConfigTools.genKeyPair(512);
            privateKey=keyPair[0];
            System.out.println("privateKey:"+privateKey);
            publicKey=keyPair[1];
            System.out.println("publicKey:"+publicKey);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (NoSuchProviderException e) {
            throw new RuntimeException(e);
        }
    }
//    加密的方法
    public static String encrypt(String plainText) throws  Exception{
        String encrypt = ConfigTools.encrypt(privateKey, plainText);
        System.out.println("encrypt:"+encrypt);

        return encrypt;
    }

//    解密的方法
    public static String decrypt(String encryptText) throws Exception{
        String decrypt = ConfigTools.decrypt(publicKey, encryptText);
        System.out.println("decrypt:"+decrypt);

        return decrypt;
    }

    /**
     *  加密数据库的密码
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        // 加密数据库的密码
        String mysqlPS = encrypt("1234");

        System.out.println("加密数据库的密码:"+mysqlPS);
   
       
    }

}

执行后即可得到如下结果:

privateKey:MIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEAtYS4iXEgE4ZT6lbcpugw5rG89qcPb9my+oaltHxkJ4MaNXKsgkVT1c1ZHAk7vAlUER5mHQ3e5Yw5Jb5PbV9YZwIDAQABAkBot5h+MfT/To86scG5yFntvLv2z4noTP8j6GnQJbLh15YmW2S3VnbL4R/3/htfDhAm2g+W6iCIvjlDv4uvJYOBAiEA8ExkDoSJjXpgDpmBuMO6rmEm1IeBBO0lYg9MxgrvhGUCIQDBYRV5ycOyMMwBbdmbsKH2sKgBfVNGLe20u0XFvKxe2wIhAMf0aRYDh4pomGHNrroWdNMCGJOlJeD5jK/qV1I8a05ZAiAjkIxXkrDoE6thU+eih5CyAvlTFM586LN/+rUxUarN4wIhAMTUxLq7zCMtfdvyRaLQRWO/N+D5OgtElOGW+9uGzKjp
publicKey:MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALWEuIlxIBOGU+pW3KboMOaxvPanD2/ZsvqGpbR8ZCeDGjVyrIJFU9XNWRwJO7wJVBEeZh0N3uWMOSW+T21fWGcCAwEAAQ==
encrypt:ft/01MRK3eZT5thl9kCyCZfZpKuV3iDJRGSFu4Pm6G4NneI+lE6UQPdNtNlIPTukuWF4MuO5DfAZ22HcHu1+qA==
加密数据库的密码:ft/01MRK3eZT5thl9kCyCZfZpKuV3iDJRGSFu4Pm6G4NneI+lE6UQPdNtNlIPTukuWF4MuO5DfAZ22HcHu1+qA==

可以看到执行结果包含3个部分的内容:

  1. privateKey:私钥,暂时不会用到,用于密码的加密;

  1. publicKey:公钥,用于密码的解密;

3,添加到yml配置文件

server:
  port: 3000

spring:
  datasource:
    username: root
    password: tFxuEgMmb51mgsukZ+pO8/WIpQJZ6ihwml38NaY6dt/vM+X56C7LO2A339+W6duSi5qb7eUBtFnPHX2VQbu+iw==
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/jc-club?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true&useSSL=false
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      initial-size: 20
      min-idle: 20
      connectionProperties: config.decrypt=true;config.decrypt.key=${publicKey};
      max-active: 100
      max-wait: 60000
      stat-view-servlet:
        enabled: true
        url-pattern: /druid/*
        login-username: admin
        login-password: 123456
      filter:
        stat:
          enabled: true
          slow-sql-millis: 2000
          log-slow-sql: true
        wall:
          enabled: true
        config:
          enabled: true

publicKey: MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANB2RtL7mi5oVDagr5dtO59br5h31+anRY1ApnJmxEuanRUKeZqLoT7W7REV7vf/bMuZ6RKI+tqz6yC81AlqGVcCAwEAAQ==

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值