网站首页 全球最实用的IT互联网站!

人工智能P2P分享Wind搜索发布信息网站地图标签大全

当前位置:诺佳网 > 软件工程 > 后端开发 > Java >

dynamic-datasource detect druid publicKey,It is highly recom

时间:2025-06-24 16:09

人气:

作者:admin

标签:

导读:使用druid-spring-boot-starter 1.2.11作为数据库连接池 dynamic-datasource-spring-boot-starter 3.4.1作为多数据源支持,并且使用了druid的数据库密钥加密功能,启动项目发现日志中有如下日志: [2024-10...

使用druid-spring-boot-starter 1.2.11作为数据库连接池 + dynamic-datasource-spring-boot-starter 3.4.1作为多数据源支持,并且使用了druid的数据库密钥加密功能,启动项目发现日志中有如下日志:

[2024-10-31 15:42:55.343] - [INFO ] - [15336] - [240E04791E60243BB7BE00FEE00CC8F33BE822D8CFE09DDE00D10000] - [main] - [c.b.d.d.s.b.a.d.DruidConfig-255] - dynamic-datasource detect druid publicKey,It is highly recommended that you use the built-in encryption method
 https://dynamic-datasource.com/guide/advance/Encode.html

yml中数据源的配置信息为:

spring:
  datasource:
    # 多数据源配置
    dynamic:
      primary: db1
      strict: true
      datasource:
        # 第一个数据源
        db1:
          url: jdbc:mysql://localhost:3306/db1?...
          username: root
          password: xxx
          druid:
            ...
            min-evictable-idle-time-millis: 300000
            max-evictable-idle-time-millis: 300000
            # 公钥
            public-key: xxx
        # 第二个数据源
        db2:
          url: jdbc:mysql://localhost:3306/db2?...
          username: root
          password: xxx
          druid:
            ...
            min-evictable-idle-time-millis: 300000
            max-evictable-idle-time-millis: 300000
            # 公钥
            public-key: xxx

根据日志在com.baomidou.dynamic.datasource.spring.boot.autoconfigure.druid.DruidConfig类中定位到了日志输出位置,这个类是druid数据库连接池的配置类,

Properties connectProperties = connectionProperties == null ? g.getConnectionProperties() : connectionProperties;
if (publicKey != null && publicKey.length() > 0) {
    if (connectProperties == null) {
        connectProperties = new Properties();
    }
    log.info("dynamic-datasource detect druid publicKey,It is highly recommended that you use the built-in encryption method \n " +
            "https://dynamic-datasource.com/guide/advance/Encode.html");
    connectProperties.setProperty("config.decrypt", "true");
    connectProperties.setProperty("config.decrypt.key", publicKey);
}
this.connectionProperties = connectProperties;

发现如果druid的公钥配置在publicKey下就会触发日志输出,并且会设置两个配置属性到connectProperties中,一个是config.decrypt,一个是config.decrypt.key。

修改yml中的配置,不在publicKey下配置公钥,而是配置到connectionProperties下:

spring:
  datasource:
    # 多数据源配置
    dynamic:
      primary: db1
      strict: true
      datasource:
        # 第一个数据源
        db1:
          url: jdbc:mysql://localhost:3306/db1?...
          username: root
          password: xxx
          druid:
            ...
            min-evictable-idle-time-millis: 300000
            max-evictable-idle-time-millis: 300000
            # 公钥
            connection-properties:
               "config.decrypt": "true"
               "config.decrypt.key": xxx
        # 第二个数据源
        db2:
          url: jdbc:mysql://localhost:3306/db2?...
          username: root
          password: xxx
          druid:
            ...
            min-evictable-idle-time-millis: 300000
            max-evictable-idle-time-millis: 300000
            # 公钥
            connection-properties:
               "config.decrypt": "true"
               "config.decrypt.key": xxx

启动项目发现数据库连接失败:

Caused by: java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)

再次在DruidConfig类中查看publicKey使用到的位置,发现:

//filters单独处理,默认了stat,wall
String filters = this.filters == null ? g.getFilters() : this.filters;
if (filters == null) {
    filters = "stat";
}
if (publicKey != null && publicKey.length() > 0 && !filters.contains("config")) {
    filters += ",config";
}
properties.setProperty(FILTERS, filters);

原来还需要设置druid的filters属性,修改yml中的配置为:

spring:
  datasource:
    # 多数据源配置
    dynamic:
      primary: db1
      strict: true
      datasource:
        # 第一个数据源
        db1:
          url: jdbc:mysql://localhost:3306/db1?...
          username: root
          password: xxx
          druid:
            ...
            min-evictable-idle-time-millis: 300000
            max-evictable-idle-time-millis: 300000
            filters: "stat,config"
            # 公钥
            connection-properties:
               "config.decrypt": "true"
               "config.decrypt.key": xxx
        # 第二个数据源
        db2:
          url: jdbc:mysql://localhost:3306/db2?...
          username: root
          password: xxx
          druid:
            ...
            min-evictable-idle-time-millis: 300000
            max-evictable-idle-time-millis: 300000
            filters: "stat,config"
            # 公钥
            connection-properties:
               "config.decrypt": "true"
               "config.decrypt.key": xxx

再次启动项目,成功启动且没有再出现dynamic-datasource detect druid publicKey,It is highly recommended that you use the built-in encryption method日志。

本文来自博客园,作者:杜劲松,转载请注明原文链接:https://www.cnblogs.com/imadc/p/18517970

温馨提示:以上内容整理于网络,仅供参考,如果对您有帮助,留下您的阅读感言吧!
相关阅读
本类排行
相关标签
本类推荐

CPU | 内存 | 硬盘 | 显卡 | 显示器 | 主板 | 电源 | 键鼠 | 网站地图

Copyright © 2025-2035 诺佳网 版权所有 备案号:赣ICP备2025066733号
本站资料均来源互联网收集整理,作品版权归作者所有,如果侵犯了您的版权,请跟我们联系。

关注微信