当前位置:   article > 正文

Spring Boot整合Druid并使用Druid加密数据库链接_怎么连上使用druid加密的数据库

怎么连上使用druid加密的数据库

1. Spring Boot引入Druid依赖

  <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.18</version>
  </dependency>
  <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.2.8</version>
  </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

2. application.yml 配置

server:
  port: 8001
spring:
    application:
        name: druid-dome
    datasource:
        url: jdbc:mysql://127.0.0.1:3306/dome?characterEncoding=utf-8&useUnicode=true&useSSL=false
        driver-class-name: com.mysql.cj.jdbc.Driver  # mysql8.0以前使用com.mysql.jdbc.Driver
        username: root
        password: 123456
        platform: mysql
        #通过这句配置将druid连接池引入到我们的配置中,spring会尽可能判断类型是什么,然后根据情况去匹配驱动类。
        type: com.alibaba.druid.pool.DruidDataSource
        druid:
            initial-size: 5  # 初始化大小
            min-idle: 5  # 最小
            max-active: 100  # 最大
            max-wait: 60000  # 配置获取连接等待超时的时间
            time-between-eviction-runs-millis: 60000  # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
            min-evictable-idle-time-millis: 300000  # 指定一个空闲连接最少空闲多久后可被清除,单位是毫秒
            validationQuery: select 'x'
            test-while-idle: true  # 当连接空闲时,是否执行连接测试
            test-on-borrow: false  # 当从连接池借用连接时,是否测试该连接
            test-on-return: false  # 在连接归还到连接池时是否测试该连接
            filters: config,wall,stat  # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
            poolPreparedStatements: true # 打开PSCache,并且指定每个连接上PSCache的大小
            maxPoolPreparedStatementPerConnectionSize: 20
            maxOpenPreparedStatements: 20
            # 通过connectProperties属性来打开mergeSql功能;慢SQL记录
            connectionProperties: druid.stat.slowSqlMillis=200;druid.stat.logSlowSql=true;config.decrypt=false
             # 合并多个DruidDataSource的监控数据
            #use-global-data-source-stat: true
            #WebStatFilter配置,说明请参考Druid Wiki,配置_配置WebStatFilter
            web-stat-filter:
                enabled: true #是否启用StatFilter默认值true
                url-pattern: /*
                exclusions: /druid/*,*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico
                session-stat-enable: true
                session-stat-max-count: 10
            #StatViewServlet配置,说明请参考Druid Wiki,配置_StatViewServlet配置
            stat-view-servlet:
                enabled: true #是否启用StatViewServlet默认值true
                url-pattern: /druid/*
                reset-enable: true
                login-username: admin
                login-password: admin
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46

3. 使用druid-x.x.x.jar Encode密码

# > druid_encode.txt 输出到指定文件
java -cp druid-1.2.8.jar com.alibaba.druid.filter.config.ConfigTools you_password > druid_encode.txt
  • 1
  • 2

使用druid-x.x.x.jar Encode密码 并输出到指定文件

4. 使用加密字符换替换Password 并添加publicKey

......

spring:
    datasource:
        url: jdbc:mysql://127.0.0.1:3306/dome?characterEncoding=utf-8&useUnicode=true&useSSL=false
        driver-class-name: com.mysql.cj.jdbc.Driver  # mysql8.0以前使用com.mysql.jdbc.Driver
        username: root
        password: <<加密字符串>>
        publicKey: <<encode_publicKey>>
......
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

5. 配置字符串解析

  1. 创建Druid配置类来解析加密字符串
import com.alibaba.druid.pool.DruidDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.sql.SQLException;

@Configuration
public class DruidConfig {
    private static final Logger logger = LoggerFactory.getLogger(DruidConfig.class);

    @Value("spring.datasource.publicKey")
    private String publicKey;


    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DruidDataSource dataSource() throws SQLException {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setFilters("config");
        druidDataSource.setConnectionProperties("config.decrypt=true;config.decrypt.key=" + publicKey);
        logger.error("Decode successful");
        return druidDataSource;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  1. 启动工程查看解析:
    在这里插入图片描述
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/天景科技苑/article/detail/794650
推荐阅读
相关标签
  

闽ICP备14008679号