From e81ac9c1d0a639a9e1e5a515369b6c84ea2e1b7b Mon Sep 17 00:00:00 2001 From: YunaiV Date: Thu, 19 May 2022 23:01:35 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20MyBatis=20Plus=20=E7=9A=84?= =?UTF-8?q?=E5=AD=97=E6=AE=B5=E5=8A=A0=E5=AF=86=E6=97=B6=EF=BC=8C=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E7=9A=84=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mybatis/core/type/EncryptTypeHandler.java | 17 ++++++++++++----- .../db/DataSourceConfigServiceImplTest.java | 12 ++++++++++++ .../src/main/resources/application.yaml | 1 + 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/type/EncryptTypeHandler.java b/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/type/EncryptTypeHandler.java index da55e057cf..f5d5a8fd1e 100644 --- a/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/type/EncryptTypeHandler.java +++ b/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/type/EncryptTypeHandler.java @@ -29,29 +29,36 @@ public class EncryptTypeHandler extends BaseTypeHandler { @Override public String getNullableResult(ResultSet rs, String columnName) throws SQLException { String value = rs.getString(columnName); - return getResult(value); + return decrypt(value); } @Override public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException { String value = rs.getString(columnIndex); - return getResult(value); + return decrypt(value); } @Override public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { String value = cs.getString(columnIndex); - return getResult(value); + return decrypt(value); } - private String getResult(String value) { + private static String decrypt(String value) { if (value == null) { return null; } return getEncryptor().decrypt(value); } - private StringEncryptor getEncryptor() { + public static String encrypt(String rawValue) { + if (rawValue == null) { + return null; + } + return getEncryptor().encrypt(rawValue); + } + + private static StringEncryptor getEncryptor() { if (encryptor != null) { return encryptor; } diff --git a/yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/db/DataSourceConfigServiceImplTest.java b/yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/db/DataSourceConfigServiceImplTest.java index 990067e3a9..c23edf537e 100755 --- a/yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/db/DataSourceConfigServiceImplTest.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/db/DataSourceConfigServiceImplTest.java @@ -132,4 +132,16 @@ public class DataSourceConfigServiceImplTest extends BaseDbUnitTest { assertServiceException(() -> dataSourceConfigService.deleteDataSourceConfig(id), DATA_SOURCE_CONFIG_NOT_EXISTS); } + @Test // 测试使用 password 查询,可以查询到数据 + public void testSelectPassword() { + // mock 数据 + DataSourceConfigDO dbDataSourceConfig = randomPojo(DataSourceConfigDO.class); + dataSourceConfigMapper.insert(dbDataSourceConfig);// @Sql: 先插入出一条存在的数据 + + // 调用 + DataSourceConfigDO result = dataSourceConfigMapper.selectOne(DataSourceConfigDO::getPassword, + EncryptTypeHandler.encrypt(dbDataSourceConfig.getPassword())); + System.out.println(result); + } + } diff --git a/yudao-server/src/main/resources/application.yaml b/yudao-server/src/main/resources/application.yaml index 179cad7617..0f477614f5 100644 --- a/yudao-server/src/main/resources/application.yaml +++ b/yudao-server/src/main/resources/application.yaml @@ -61,6 +61,7 @@ mybatis-plus: logic-delete-value: 1 # 逻辑已删除值(默认为 1) logic-not-delete-value: 0 # 逻辑未删除值(默认为 0) type-aliases-package: ${yudao.info.base-package}.module.*.dal.dataobject + type-handlers-package: ${yudao.info.base-package}.framework.mybatis.core.type --- #################### 芋道相关配置 ####################