文档章节

springboot2.0.4 集成durid加密密码

素剑步青尘
 素剑步青尘
发布于 2018/08/01 13:46
字数 771
阅读 65
收藏 0

 

1.引入druid

		<!--druid -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.1.6</version>
		</dependency>

2.找到maven下载的jar包目录,运行如下命令

java -cp .\druid-1.1.6.jar com.alibaba.druid.filter.config.ConfigTools 123456
privateKey:MIIBUwIBADANBgkqhkiG9w0BAQEFAASCAT0wggE5AgEAAkEAneppviMY4dTHLKe0Dcgf1U8BwsLum4CZduMy14iixxzEtzfoukdfafkV0dmie
OMdJ9c8ngMYWpDlYz3JODha0wIDAQABAkAoiAMOB0oapJdekci4BNaO1FF6YLgHd5FsH1JsWaiV/BYFESCWNd9ac7+1YjX+8nqY2BcmtIlmO37LgP3JMCVZA
iEA9ZDEjE2U7FJBbI8Thz0nFp17MpqkTOdwIEkVl98NO9UCIQCkoDNyOneWvvk1D6tgO/Sd8zlDe8Xq5VVhwIj/MuDYBwIgR97n95xZQtVMOqahc4ptX0wWG
5hxD+UIxxo8zKdGq0UCIGfzvOipYLBwwN4vmdC3qzw9xHAHbS9pRuPl9tvmnbHrAiBtzcaUmEffS2mo4SDM2gaMJv/ah2fOkBcw62rWqRoB8w==
publicKey:MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJ3qab4jGOHUxyyntA3IH9VPAcLC7puAmXbjMteIosccxLc36LpHX2n5FdHZonjjHSfXPJ4DGFqQ5W
M9yTg4WtMCAwEAAQ==
password:P9mGVbi6dn+TF5YYxLe0O+Hp8VyAoTN+K6ZbBYjGhAslCaHZFG0cPJ8t7oDoWZBb7XSxald6be5VYvW2pXB9Sg==

3.application.properties

# 驱动配置信息  
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource  
spring.datasource.url = jdbc:mysql://127.0.0.1:3306/crm  
spring.datasource.username = root  
spring.datasource.password = 0O+Hp8VyAoTN+K6ZbBYjGhAslCaHZFG0cPJ8t7oDoWZBb7XSxald6be5VYvW2pXB9Sg==
spring.datasource.publicKey=JBAJ3qab4jGOHUxyyntA3IH9VPAcLC7puAmXbjMteIosccxLc36LpHX2n5FdHZonjjHSfXP
spring.datasource.driverClassName = com.mysql.jdbc.Driver  
  
# 连接池的配置信息  
# 初始化大小,最小,最大  
spring.datasource.initialSize=5  
spring.datasource.minIdle=5  
spring.datasource.maxActive=20  
# 配置获取连接等待超时的时间  
spring.datasource.maxWait=60000  
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒  
spring.datasource.timeBetweenEvictionRunsMillis=60000  
# 配置一个连接在池中最小生存的时间,单位是毫秒  
spring.datasource.minEvictableIdleTimeMillis=300000  
spring.datasource.validationQuery=SELECT 1 FROM DUAL  
spring.datasource.testWhileIdle=true  
spring.datasource.testOnBorrow=false  
spring.datasource.testOnReturn=false  
# 打开PSCache,并且指定每个连接上PSCache的大小  
spring.datasource.poolPreparedStatements=true  
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20  
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙  
spring.datasource.filters=config,stat,wall,slf4j
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录  
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000;config.decrypt=true;
#config.decrypt.key=your public key  

4.配置java类DataSourceConfig

package com.likaixuan.druid;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import com.alibaba.druid.filter.config.ConfigTools;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
 
 


@Configuration
@ConfigurationProperties(prefix="spring.datasource")
public class DataSourceConfig {
    private Logger logger = LoggerFactory.getLogger(DataSourceConfig.class);
    private String type;
    private String url;
    private String driverClassName;
    private String username;
    private String password;
    private Integer initialSize;
    private Integer minIdle;
    private Integer maxActive;
    private Integer maxWait;
    private Integer timeBetweenEvictionRunsMillis;
    private Integer minEvictableIdleTimeMillis;
    private String validationQuery;
    private Boolean testWhileIdle;
    private Boolean testOnBorrow;
    private Boolean testOnReturn;
    private Boolean poolPreparedStatements;
    private Integer maxPoolPreparedStatementPerConnectionSize;
    private String filters;
    private String connectionProperties;
    private String publicKey;
 
    public String getDriverClassName() {
        return driverClassName;
    }
 
    public void setDriverClassName(String driverClassName) {
        this.driverClassName = driverClassName;
    }
 
    public String getType() {
        return type;
    }
 
    public void setType(String type) {
        this.type = type;
    }
 
    public String getUrl() {
        return url;
    }
 
    public void setUrl(String url) {
        this.url = url;
    }
 
    public String getUsername() {
        return username;
    }
 
    public void setUsername(String username) {
        this.username = username;
    }
 
    public String getPassword() {
        return password;
    }
 
    public void setPassword(String password) {
        this.password = password;
    }
 
    public Integer getInitialSize() {
        return initialSize;
    }
 
    public void setInitialSize(Integer initialSize) {
        this.initialSize = initialSize;
    }
 
    public Integer getMinIdle() {
        return minIdle;
    }
 
    public void setMinIdle(Integer minIdle) {
        this.minIdle = minIdle;
    }
 
    public Integer getMaxActive() {
        return maxActive;
    }
 
    public void setMaxActive(Integer maxActive) {
        this.maxActive = maxActive;
    }
 
    public Integer getMaxWait() {
        return maxWait;
    }
 
    public void setMaxWait(Integer maxWait) {
        this.maxWait = maxWait;
    }
 
    public Integer getTimeBetweenEvictionRunsMillis() {
        return timeBetweenEvictionRunsMillis;
    }
 
    public void setTimeBetweenEvictionRunsMillis(Integer timeBetweenEvictionRunsMillis) {
        this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
    }
 
    public Integer getMinEvictableIdleTimeMillis() {
        return minEvictableIdleTimeMillis;
    }
 
    public void setMinEvictableIdleTimeMillis(Integer minEvictableIdleTimeMillis) {
        this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;
    }
 
    public String getValidationQuery() {
        return validationQuery;
    }
 
    public void setValidationQuery(String validationQuery) {
        this.validationQuery = validationQuery;
    }
 
    public Boolean getTestWhileIdle() {
        return testWhileIdle;
    }
 
    public void setTestWhileIdle(Boolean testWhileIdle) {
        this.testWhileIdle = testWhileIdle;
    }
 
    public Boolean getTestOnBorrow() {
        return testOnBorrow;
    }
 
    public void setTestOnBorrow(Boolean testOnBorrow) {
        this.testOnBorrow = testOnBorrow;
    }
 
    public Boolean getTestOnReturn() {
        return testOnReturn;
    }
 
    public void setTestOnReturn(Boolean testOnReturn) {
        this.testOnReturn = testOnReturn;
    }
 
    public Boolean getPoolPreparedStatements() {
        return poolPreparedStatements;
    }
 
    public void setPoolPreparedStatements(Boolean poolPreparedStatements) {
        this.poolPreparedStatements = poolPreparedStatements;
    }
 
    public Integer getMaxPoolPreparedStatementPerConnectionSize() {
        return maxPoolPreparedStatementPerConnectionSize;
    }
 
    public void setMaxPoolPreparedStatementPerConnectionSize(Integer maxPoolPreparedStatementPerConnectionSize) {
        this.maxPoolPreparedStatementPerConnectionSize = maxPoolPreparedStatementPerConnectionSize;
    }
 
    public String getFilters() {
        return filters;
    }
 
    public void setFilters(String filters) {
        this.filters = filters;
    }
 
    public String getConnectionProperties() {
        return connectionProperties;
    }
    public void setConnectionProperties(String connectionProperties) {
        this.connectionProperties = connectionProperties;
    }
 
    public String getPublicKey() {
        return publicKey;
    }
 
    public void setPublicKey(String publicKey) {
        this.publicKey = publicKey;
    }
 
    @Bean
    public ServletRegistrationBean druidServlet() {
        ServletRegistrationBean reg = new ServletRegistrationBean();
        reg.setServlet(new StatViewServlet());
        reg.addUrlMappings("/druid/*");
        reg.addInitParameter("loginUsername", username);
        reg.addInitParameter("loginPassword", password);
        return reg;
    }
    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new WebStatFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        filterRegistrationBean.addInitParameter("profileEnable", "true");
        return filterRegistrationBean;
    }
    @Bean
    @Primary
    public DataSource druidDataSource() throws Exception {
        DruidDataSource datasource = new DruidDataSource();
        datasource.setUrl(url);
        datasource.setUsername(username);
        datasource.setConnectionProperties(connectionProperties);
        datasource.setPassword(ConfigTools.decrypt(publicKey,password));
        datasource.setDriverClassName(driverClassName);
        datasource.setInitialSize(initialSize);
        datasource.setMinIdle(minIdle);
        datasource.setMaxActive(maxActive);
        datasource.setMaxWait(maxWait);
        datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
        datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
        datasource.setValidationQuery(validationQuery);
        datasource.setTestWhileIdle(testWhileIdle);
        datasource.setTestOnBorrow(testOnBorrow);
        datasource.setTestOnReturn(testOnReturn);
        String paramArray[] = connectionProperties.split(";");
        
        try {
            datasource.setFilters(filters);
        } catch (SQLException e) {
            logger.error("========druid configuration initialization filter========", e);
        }
        return datasource;
    }
	
    
}

源码地址:https://gitee.com/likaixuan0/druid.git

© 著作权归作者所有

共有 人打赏支持
素剑步青尘
粉丝 24
博文 62
码字总数 20476
作品 1
杭州
架构师
私信 提问
SpringBoot2.0 集成 mybatis、freemark、eureka、swagger

当我们从SpringBoot1.0过渡到 2.0的时候,可能会遇到各种各样的问题,比如我集成的时候遇到过版本冲突,打包错误,eureka注册不上去,以及无法启动等问题,因此我写了一片博客记录下来。我这...

江火似流萤
2018/08/30
0
0
durid防火墙参数支持动态设置么?

@wenshao 你好,想跟你请教个问题:,需求如下,启动durid,防火墙配置不允许执行删除语句,程序运行中在特定时间或者特定语句需要执行删除,这时想针对某条语句临时改变durid防火墙配置,因...

qiubao
2016/02/25
150
1
Java加密包--Jasypt

Jasypt这个Java类包为开发人员提供一种简单的方式来为项目增加加密功能,包括:密码Digest认证,文本和对象加密,集成hibernate,Spring Security(Acegi)来增强密码管理。Jasypt开发团队推出...

匿名
2009/06/17
9.6K
0
spring管理durid事务,如何配置?

有谁是用spring管理过durid的事务,初次接触durid数据库源,不知道怎么配置其事务管理,请高手帮忙看看,提点一下

丰行
2015/01/08
7.6K
7
关于Durid进行Sql替换的问题

@wenshao 你好,想跟你请教个问题: 最近我在用durid praser进行sql的分析和表名替换,分析部分我已经基本跑通,替换的话我是直接用string.replaceFirst进行的感觉好像有点不靠谱,不是知道d...

BOA007
2016/01/19
586
2

没有更多内容

加载失败,请刷新页面

加载更多

JDK 12又来了,我学不动了...

写在前面 看到 JDK 12又发布了,萌新不知不觉感觉瑟瑟发抖,从 Java 1.8的函数式编程思维和范式 到 Java 1.9的模块化特性的加持 以及还没来得及深切感受一下 Java 1.11 的 ZGC强大之后,这次...

CodeSheep
58分钟前
12
0
解决在gradle构建project时,发生peer not authenticated错误的方法

问题: FAILURE: Build failed with an exception.* What went wrong:Could not resolve all dependencies for configuration ':wl01-service:compile'.> Could not resolve com.ali......

Benz001
今天
2
0
Apache Ignite上的TensorFlow

任何深度学习都是从数据开始的,这是关键点。没有数据,就无法训练模型,也无法评估模型质量,更无法做出预测,因此,数据源非常重要。在做研究、构建新的神经网络架构、以及做实验时,会习惯...

李玉珏
今天
18
0
Java记录日志附带请求标识

起因 系统是web接口服务,排查故障的时候经常需要记录某次请求调用链路日志。这样我们拉日志的时候只要匹配这个traceid就行了 第一版解决方案 原来我们一直用了个很low的办法,在请求开始的时...

Tree
今天
4
0
使用split_size优化的ODPS SQL的场景

使用split_size优化的ODPS SQL的场景 首先有两个大背景需要说明如下: 说明1:split_size,设定一个map的最大数据输入量,单位M,默认256M。用户可以通过控制这个变量,从而达到对map端输入的...

阿里云官方博客
今天
5
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部