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);
}
}
这个工具类是生成公钥和私钥,及其加密后的密码
配置文件的具体细节
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
{定义公钥}