Home  >  Article  >  Java  >  How to configure Springboot2 integrated druid encrypted database password

How to configure Springboot2 integrated druid encrypted database password

WBOY
WBOYforward
2023-05-11 11:31:051031browse

One: Environment

springboot 2.x
druid 1.1.21

Two: druid encrypted database password

Download the druid-1.1.21.jar package locally , run cmd, enter the command

java -cp jar包路径 com.alibaba.druid.filter.config.ConfigTools 数据库密码
java -cp druid-1.1.21.jar com.alibaba.druid.filter.config.ConfigTools 数据库密码

Successful operation output

privateKey:MIIBVAIBAD...
publicKey:MFwwDQYJKo...
password:PNd/zcG JEn. ..

Fill the obtained publicKey and password into the yml configuration file respectively

3: Single data source

Add dependencies


    com.alibaba
    druid-spring-boot-starter
    1.1.21

yml configuration

spring:
  datasource:
    name: 名称
    url: 地址
    username: 用户名
    password: 加密后的密码
    driver-class-name: com.mysql.cj.jdbc.Driver
    # druid
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      #特别提示:配置数据库加密 config这个不能忘掉
      filters: stat,wall,config
      use-global-data-source-stat: true
      # 开启解密config.decrypt=true; 公钥:config.decrypt.key
      connect-properties:
        druid.stat.mergeSql: true
        druid.stat.slowSqlMillis: 5000
        druid.stat.logSlowSql: true
        config.decrypt: true
        config.decrypt.key: 公钥
      # 连接池的配置信息
      # 初始化大小,最小空闲连接数,最大活跃数
      initial-size: 5
      min-idle: 5
      maxActive: 20
      # 配置获取连接等待超时的时间
      maxWait: 60000
      # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
      timeBetweenEvictionRunsMillis: 60000
      # 配置一个连接在池中最小生存的时间,单位是毫秒
      minEvictableIdleTimeMillis: 300000
      validationQuery: SELECT 1 FROM DUAL
      testWhileIdle: true
      testOnBorrow: false
      testOnReturn: false
      # 打开PSCache,并且指定每个连接上PSCache的大小
      poolPreparedStatements: true
      maxPoolPreparedStatementPerConnectionSize: 20

Four: Multiple data sources

Add dependencies


    com.alibaba
    druid-spring-boot-starter
    1.1.21


   com.baomidou
   dynamic-datasource-spring-boot-starter
   2.5.3

Startup class configuration

@SpringBootApplication(exclude = DruidDataSourceAutoConfigure.class)

eg:

How to configure Springboot2 integrated druid encrypted database password

ymlconfig

spring:
  datasource:
    dynamic:
      # 默认数据源
      primary: CLOUD
      datasource:
        CLOUD:
          url: 数据库地址
          username: 用户名
          password: 加密后的密码
          driver-class-name: com.mysql.cj.jdbc.Driver
          druid:
            public-key: 加密后的公钥
        WAREHOUSE:
          driver-class-name: com.mysql.cj.jdbc.Driver
          url: 数据库地址
          username: 用户名
          password: 加密后的密码
          druid:
            public-key: 加密后的公钥

The above is the detailed content of How to configure Springboot2 integrated druid encrypted database password. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete