左右明 發表於 2025-6-24 16:09:00

dynamic-datasource detect druid publicKey,It is highly recommended that you use the built-in encryption method

<p>使用druid-spring-boot-starter 1.2.11作为数据库连接池 + dynamic-datasource-spring-boot-starter 3.4.1作为多数据源支持,并且使用了druid的数据库密钥加密功能,启动项目发现日志中有如下日志:</p>
<pre><code> - - - - - - 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
</code></pre>
<p>yml中数据源的配置信息为:</p>
<pre><code>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
</code></pre>
<p>根据日志在com.baomidou.dynamic.datasource.spring.boot.autoconfigure.druid.DruidConfig类中定位到了日志输出位置,这个类是druid数据库连接池的配置类,</p>
<pre><code>Properties connectProperties = connectionProperties == null ? g.getConnectionProperties() : connectionProperties;
if (publicKey != null &amp;&amp; publicKey.length() &gt; 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;
</code></pre>
<p>发现如果druid的公钥配置在publicKey下就会触发日志输出,并且会设置两个配置属性到connectProperties中,一个是config.decrypt,一个是config.decrypt.key。</p>
<p>修改yml中的配置,不在publicKey下配置公钥,而是配置到connectionProperties下:</p>
<pre><code>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
</code></pre>
<p>启动项目发现数据库连接失败:</p>
<pre><code>Caused by: java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
</code></pre>
<p>再次在DruidConfig类中查看publicKey使用到的位置,发现:</p>
<pre><code>//filters单独处理,默认了stat,wall
String filters = this.filters == null ? g.getFilters() : this.filters;
if (filters == null) {
    filters = "stat";
}
if (publicKey != null &amp;&amp; publicKey.length() &gt; 0 &amp;&amp; !filters.contains("config")) {
    filters += ",config";
}
properties.setProperty(FILTERS, filters);
</code></pre>
<p>原来还需要设置druid的filters属性,修改yml中的配置为:</p>
<pre><code>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
</code></pre>
<p>再次启动项目,成功启动且没有再出现dynamic-datasource detect druid publicKey,It is highly recommended that you use the built-in encryption method日志。</p>


</div>
<div id="MySignature" role="contentinfo">
    <p>本文来自博客园,作者:杜劲松,转载请注明原文链接:https://www.cnblogs.com/imadc/p/18517970</p><br><br>
来源:https://www.cnblogs.com/imadc/p/18517970
頁: [1]
查看完整版本: dynamic-datasource detect druid publicKey,It is highly recommended that you use the built-in encryption method