From b0d5cbdf6dba86ab37863499ebd8df6ad9253bba Mon Sep 17 00:00:00 2001 From: Gszekt Date: Sat, 13 Mar 2021 17:34:55 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93=E5=AF=86=E7=A0=81?= =?UTF-8?q?=E5=8A=A0=E5=AF=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/resources/application-druid.yml | 11 ++++++-- .../config/properties/DruidProperties.java | 25 +++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/ruoyi-admin/src/main/resources/application-druid.yml b/ruoyi-admin/src/main/resources/application-druid.yml index 5243abd67..451b1ad30 100644 --- a/ruoyi-admin/src/main/resources/application-druid.yml +++ b/ruoyi-admin/src/main/resources/application-druid.yml @@ -8,7 +8,7 @@ spring: master: url: jdbc:mysql://localhost:3306/ry?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 username: root - password: password + password: # 使用 com.alibaba.druid.filter.config.ConfigTools 加密后生成的 password # 从库数据源 slave: # 从数据源开关/默认关闭 @@ -54,4 +54,11 @@ spring: merge-sql: true wall: config: - multi-statement-allow: true \ No newline at end of file + multi-statement-allow: true + filters: + config: + # 数据库密码是否加密开关,false 表示密码使用明文 + enabled: true + connect-properties: + config.decrypt: true + config.decrypt.key: # 使用 com.alibaba.druid.filter.config.ConfigTools 加密后生成的 publicKey diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/properties/DruidProperties.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/properties/DruidProperties.java index 84f7e0090..ac0eeaecb 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/properties/DruidProperties.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/properties/DruidProperties.java @@ -4,6 +4,8 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; import com.alibaba.druid.pool.DruidDataSource; +import java.util.Properties; + /** * druid 配置属性 * @@ -45,6 +47,15 @@ public class DruidProperties @Value("${spring.datasource.druid.testOnReturn}") private boolean testOnReturn; + @Value("${spring.datasource.druid.filters.config.enabled}") + private boolean configFilterEnabled; + + @Value("${spring.datasource.druid.connect-properties.config.decrypt}") + private String decryptEnabled; + + @Value("${spring.datasource.druid.connect-properties.config.decrypt.key}") + private String decryptKey; + public DruidDataSource dataSource(DruidDataSource datasource) { /** 配置初始化大小、最小、最大 */ @@ -72,6 +83,20 @@ public class DruidProperties datasource.setTestOnBorrow(testOnBorrow); /** 归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。 */ datasource.setTestOnReturn(testOnReturn); + + if (configFilterEnabled) { + try { + /* 启用数据库密码解密 */ + datasource.setFilters("config"); + Properties properties = new Properties(); + properties.put("config.decrypt", decryptEnabled); + properties.put("config.decrypt.key", decryptKey); + datasource.setConnectProperties(properties); + } catch (Exception exception) { + exception.printStackTrace(); + } + } + return datasource; } }