数据库密码加密

package com.cqie.datacommon.utils;

import com.alibaba.druid.filter.config.ConfigTools;

import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

/**
 * druid数据源password加密
 *
 * @author: QingYuQiao
 */
public class DruidEncryptUtil {

    private static String publicKey;

    private 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) {
            e.printStackTrace();
        } catch (NoSuchProviderException e) {
            e.printStackTrace();
        }
    }

    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;
    }


    //生成password
    public static void main(String[] args) throws Exception {
        String encrypt = encrypt("qyq200112");
        System.out.println("encrypt:" + encrypt);
    }

}

这个工具类是生成公钥和私钥,及其加密后的密码
image.png

配置文件的具体细节

spring:
  banner:
    location: classpath:banner.txt
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
     driver-class-name: com.mysql.cj.jdbc.Driver
     url: jdbc:mysql://localhost:3306/category_info?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&useSSL=false
     password: 私钥
     username: root
     connection-properties: config.decrypt=true;config.decrypt.key=${publicKey}
     connectTimeout: 60000  #1.2.12版本需要单独配置才生效
     socketTimeout: 60000   #1.2.12版本需要单独配置才生效
     initialSize: 5 #初始化时建立物理连接的个数
     minIdle: 5 #最小连接池数量
     maxActive: 20 #最大连接池数量
     maxWait: 60000 #获取连接时最大等待时间,单位毫秒。
     timeBetweenEvictionRunsMillis: 60000 #配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
     min-evictableIdleTimeMillis: 300000 #配置一个连接在池中最小生存的时间,单位是毫秒
     validationQuery: SELECT 1 FROM DUAL #用来检测连接是否有效的sql,要求是一个查询语句,常用select 1。
     testWhileIdle: true #建议配置为true,不影响性能,并且保证安全性。
     testOnBorrow: false #申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。
     testOnReturn: false #归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。
     poolPreparedStatements: true #是否缓存preparedStatement,也就是PSCache。PSCache对支持游标的数据库性能提升巨大,比如MySQL、SQLServer等。
     maxPoolPreparedStatementPerConnectionSize: 20 #每个连接上PSCache的大小,默认是256。
     filters: stat,wall #配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
     filter:
       config:
         enabled: true
         
   {定义公钥}

image.png
image.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Object-v

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值