springboot druid数据库密码加密
pom.xml引入jar
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.14</version> </dependency>
生成加密密码
输入命令
java -cp D:\Repositories\ricet\com\alibaba\druid\1.1.14\druid-1.1.14.jar com.alibaba.druid.filter.config.ConfigTools 123456
其中:publicKey 为公钥 , password为加密后密码,privateKey为私钥
配置数据库文件 application.yml
# 数据源配置 spring: datasource: type: com.alibaba.druid.pool.DruidDataSource driverClassName: com.mysql.cj.jdbc.Driver druid: # 主库数据源 master: url: jdbc:mysql://dbmast.confedu.com:3306/da_edu_springboss?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 username: root password: D/PR8jte+cXJVj74bavRpkqWbH/d1f/NlYliAyTafXDk5f8qKW7ITPdXskJ8Y3wz8zIM4on3zw+rCXD+ connect-properties: config.decrypt: true config.decrypt.key: MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAIeliL/0EAtp/R9ow+LXx59I4PpWjlYCG6jyf7SAoagdscRCBSi916x9Vg6zbOMKWH8ZtvdNE54Lj8 # 从库数据源 slave: # 从数据源开关/默认关闭 enabled: false url: username: password: # 初始连接数 initialSize: 5 # 最小连接池数量 minIdle: 10 # 最大连接池数量 maxActive: 20 # 配置获取连接等待超时的时间 maxWait: 60000 # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 timeBetweenEvictionRunsMillis: 60000 # 配置一个连接在池中最小生存的时间,单位是毫秒 minEvictableIdleTimeMillis: 300000 # 配置一个连接在池中最大生存的时间,单位是毫秒 maxEvictableIdleTimeMillis: 900000 # 配置检测连接是否有效 validationQuery: SELECT 1 FROM DUAL testWhileIdle: true testOnBorrow: false testOnReturn: false webStatFilter: enabled: true statViewServlet: enabled: true # 设置白名单,不填则允许所有访问 allow: url-pattern: /monitor/druid/* filter: stat: enabled: true # 慢SQL记录 log-slow-sql: true slow-sql-millis: 1000 merge-sql: true wall: config: multi-statement-allow: true #数据库过滤器 config: enabled: true
工具类
package com.ruoyi.common.utils; import com.alibaba.druid.filter.config.ConfigTools; /** * @author: dongao * @create: 2019/7/22 * 数据库密码加密工具类 */ public class DataSourcePasswordUtils { /** * 加密 * @param cipherText * @return * @throws Exception */ public static String encrypt(String cipherText) throws Exception { String encrypt = ConfigTools.encrypt(cipherText); return encrypt; } /** * 解密 * @param cipherText * @return * @throws Exception */ public static String decrypt(String cipherText) throws Exception { String decrypt = ConfigTools.decrypt(cipherText); return decrypt; } public static void main(String[] args) throws Exception { String password = "123456"; String encrypt = encrypt(password); System.out.println(encrypt); String decrypt = decrypt(encrypt); System.out.println(decrypt); } }
application.properties 数据库配置如下:
# 数据源配置 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver #spring.datasource.driverClassName=com.mysql.jdbc.Driver # 主库数据源 spring.datasource.druid.master.url=jdbc:mysql://ei.appcommunity.mysql1.com:3306/app_community?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 spring.datasource.druid.master.username=new_app spring.datasource.druid.master.password=Z/+M30mrTBcleSMHt3+Xq4z+DgImbaCV54+0//Uh80PUxFOXYS0JEd5GHwmC3wXKFPQfnzL6Ok1+3 spring.datasource.druid.master.connect-properties.config.decrypt=true spring.datasource.druid.master.connect-properties.config.decrypt.key=MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAK+6BeVD3laS7C1iHWwyA1IiT8JKh7LpCxhvvdrCPZhSYbID3NwLpGH4ECSM+WluOT7Bdq4CP3T7CUC # 从库数据源 spring.datasource.druid.slave.url=jdbc:mysql://ei.appcommunity.mysql1.com:3306/app_community?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 spring.datasource.druid.slave.username=new_app spring.datasource.druid.slave.password=Z/+M30mrTBcleSMHt3+Xq4z+DgImbaCV54+0//Uh80PUxFOXYS0JEd5GHwmC3wXKFPQfnzL6Ok1+3Fy spring.datasource.druid.slave.connect-properties.config.decrypt=true spring.datasource.druid.slave.connect-properties.config.decrypt.key=MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAK+6BeVD3laS7C1iHWwyA1IiT8JKh7LpCxhvvdrCPZhSYbID3NwLpGH4ECSM+WluOT7Bdq4CP3T7CUC # 初始连接数 spring.datasource.druid.initialSize=5 # 最小连接池数量 spring.datasource.druid.minIdle=10 # 最大连接池数量 spring.datasource.druid.maxActive=20 # 配置获取连接等待超时的时间 spring.datasource.druid.maxWait=60000 # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 spring.datasource.druid.timeBetweenEvictionRunsMillis=60000 # 配置一个连接在池中最小生存的时间,单位是毫秒 spring.datasource.druid.minEvictableIdleTimeMillis=300000 # 配置一个连接在池中最大生存的时间,单位是毫秒 spring.datasource.druid.maxEvictableIdleTimeMillis=900000 # 配置检测连接是否有效 spring.datasource.druid.validationQuery=SELECT 1 FROM DUAL spring.datasource.druid.testWhileIdle=true spring.datasource.druid.testOnBorrow=false spring.datasource.druid.testOnReturn=false spring.datasource.druid.web-stat-filter.enabled=true spring.datasource.druid.stat-view-servlet.enabled=true # 设置白名单,不填则允许所有访问 spring.datasource.druid.stat-view-servlet.allow= spring.datasource.druid.stat-view-servlet.url-pattern=/monitor/druid/* spring.datasource.druid.filter.stat.enabled=true # 慢SQL记录 spring.datasource.druid.filter.stat.log-slow-sql=true spring.datasource.druid.filter.stat.slow-sql-millis=1000 spring.datasource.druid.filter.stat.merge-sql=true spring.datasource.druid.filter.wall.config.multi-statement-allow=true #数据库过滤器 spring.datasource.druid.filter.config.enabled=true