Druid使用及开启数据库密码加密

Posted mcorleon

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Druid使用及开启数据库密码加密相关的知识,希望对你有一定的参考价值。

Springboot环境下的配置

1.maven引包

1         <dependency>
2             <groupId>com.alibaba</groupId>
3             <artifactId>druid</artifactId>
4             <version>1.0.26</version>
5         </dependency>    

 

2.创建配置类

  1 package com.tqh.demo.config;
  2 
  3 import com.alibaba.druid.pool.DruidDataSource;
  4 import com.alibaba.druid.support.http.StatViewServlet;
  5 import com.alibaba.druid.support.http.WebStatFilter;
  6 import org.slf4j.Logger;
  7 import org.slf4j.LoggerFactory;
  8 import org.springframework.beans.factory.annotation.Value;
  9 import org.springframework.boot.web.servlet.FilterRegistrationBean;
 10 import org.springframework.boot.web.servlet.ServletRegistrationBean;
 11 import org.springframework.context.annotation.Bean;
 12 import org.springframework.context.annotation.Configuration;
 13 
 14 import javax.sql.DataSource;
 15 import java.sql.SQLException;
 16 import java.util.Properties;
 17 
 18 
 19 /**
 20  * @Author: Mcorleon
 21  * @Date: 18-7-27 14:37
 22  */
 23 @Configuration
 24 public class DruidConfig {
 25 
 26         private Logger logger = LoggerFactory.getLogger(DruidConfig.class);
 27 
 28         @Value("${spring.datasource.url}")
 29         private String dbUrl;
 30 
 31         @Value("${spring.datasource.username}")
 32         private String username;
 33 
 34         @Value("${spring.datasource.password}")
 35         private String password;
 36 
 37         @Value("${spring.datasource.driver-class-name}")
 38         private String driverClassName;
 39 
 40         @Value("${spring.datasource.initialSize}")
 41         private int initialSize;
 42 
 43         @Value("${spring.datasource.minIdle}")
 44         private int minIdle;
 45 
 46         @Value("${spring.datasource.maxActive}")
 47         private int maxActive;
 48 
 49         @Value("${spring.datasource.maxWait}")
 50         private int maxWait;
 51 
 52         @Value("${spring.datasource.timeBetweenEvictionRunsMillis}")
 53         private int timeBetweenEvictionRunsMillis;
 54 
 55         @Value("${spring.datasource.minEvictableIdleTimeMillis}")
 56         private int minEvictableIdleTimeMillis;
 57 
 58         @Value("${spring.datasource.validationQuery}")
 59         private String validationQuery;
 60 
 61         @Value("${spring.datasource.testWhileIdle}")
 62         private boolean testWhileIdle;
 63 
 64         @Value("${spring.datasource.testOnBorrow}")
 65         private boolean testOnBorrow;
 66 
 67         @Value("${spring.datasource.testOnReturn}")
 68         private boolean testOnReturn;
 69 
 70         @Value("${spring.datasource.filters}")
 71         private String filters;
 72 
 73         @Value("${spring.datasource.logSlowSql}")
 74         private String logSlowSql;
 75 
 76         @Value("${spring.datasource.publickey}")
 77         private String publickey;
 78 
 79         @Bean
 80         public ServletRegistrationBean druidServlet() {
 81             ServletRegistrationBean reg = new ServletRegistrationBean();
 82             reg.setServlet(new StatViewServlet());
 83             reg.addUrlMappings("/druid/*");
 84             //登录druid是否需要密码
 85 //            reg.addInitParameter("loginUsername", username);
 86 //            reg.addInitParameter("loginPassword", password);
 87             reg.addInitParameter("logSlowSql", logSlowSql);
 88             return reg;
 89         }
 90 
 91         @Bean
 92         public FilterRegistrationBean filterRegistrationBean() {
 93             FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
 94             filterRegistrationBean.setFilter(new WebStatFilter());
 95             filterRegistrationBean.addUrlPatterns("/*");
 96             filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
 97             filterRegistrationBean.addInitParameter("profileEnable", "true");
 98             return filterRegistrationBean;
 99         }
100 
101         @Bean
102         public DataSource druidDataSource() {
103             DruidDataSource datasource = new DruidDataSource();
104             //数据库密码加密配置
105             Properties connectionProperties=new Properties();
106             connectionProperties.setProperty("config.decrypt","true");
107             connectionProperties.setProperty("config.decrypt.key",publickey);
108 
109             datasource.setUrl(dbUrl);
110             datasource.setUsername(username);
111             datasource.setPassword(password);
112             datasource.setConnectProperties(connectionProperties);
113             datasource.setDriverClassName(driverClassName);
114             datasource.setInitialSize(initialSize);
115             datasource.setMinIdle(minIdle);
116             datasource.setMaxActive(maxActive);
117             datasource.setMaxWait(maxWait);
118             datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
119             datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
120             datasource.setValidationQuery(validationQuery);
121             datasource.setTestWhileIdle(testWhileIdle);
122             datasource.setTestOnBorrow(testOnBorrow);
123             datasource.setTestOnReturn(testOnReturn);
124             try {
125                 datasource.setFilters(filters);
126             } catch (SQLException e) {
127                 logger.error("druid configuration initialization filter", e);
128             }
129             return datasource;
130         }
131 
132     }

 

3.application.yml配置

 1 server:
 2   port: 9090
 3 spring:
 4   datasource:
 5     type: com.alibaba.druid.pool.DruidDataSource
 6     url: jdbc:mysql://localhost:3306/shiro_test?useUnicode=true&characterEncoding=utf8&useSSL=false
 7     username: root
 8     password: HKMN8fFgGT4s23cKLt5WD6/PN0kSWzgF1o5FcCZnjh1fD5+vw4FXDiWWttFQtiq4NJK64DgVXCYechjfsUyJzA==
 9     publickey: MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJ7GBjiihzT3GYDn2JfpqjhPJ8Opo/Nn4n9GVHCv0d2PM8DPrwAtjQ9RmpwpWFWK5ErmMDKF32Hv7JFMEaCzH9MCAwEAAQ==
10     driver-class-name: com.mysql.jdbc.Driver
11     # 下面为连接池的补充设置,应用到上面所有数据源中
12     initialSize: 5
13     minIdle: 5
14     maxActive: 20
15     # 配置获取连接等待超时的时间
16     maxWait: 60000
17     # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
18     timeBetweenEvictionRunsMillis: 60000
19     # 配置一个连接在池中最小生存的时间,单位是毫秒
20     minEvictableIdleTimeMillis: 300000
21     validationQuery: SELECT 1 FROM DUAL
22     testWhileIdle: true
23     testOnBorrow: false
24     testOnReturn: false
25     # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,‘wall‘用于防火墙 ‘config‘开启密码加密
26     filters: stat,wall,log4j,config
27     logSlowSql: true
28   mvc:
29     view:
30       prefix: classpath:/templates/
31       suffix: .html
32     static-path-pattern: /static/**
33 mybatis:
34   type-aliases-package: com.tqh.demo.model

其中,publickey用于解密出密码,私钥,公钥,暗文密码都是用 ConfigTools类生成的,生成方法:找到durid jar包的路径并运行:

java -cp   路径/druid.jar   com.alibaba.druid.filter.config.ConfigTools 你的明文密码

 

 

最后在浏览器输入http://localhost:9090/druid即可开启监控页面

技术分享图片

 

如果使用了shiro记得在shiro配置类里的过滤链设置不拦截druid :   

 filterChainDefinitionMap.put("/druid/**", "anon");

以上是关于Druid使用及开启数据库密码加密的主要内容,如果未能解决你的问题,请参考以下文章

配置 Druid 数据源及密码加密-SpringBoot 2.7

spring boot基于DRUID数据源密码加密及数据源监控实现

如何使用druid加密数据库密码

使用druid 数据池对连接密码加密

Druid-Druid数据源加密数据库密码配置

SpringBoot使用Druid数据库加密链接完整方案