Commit e81ac9c1d0a639a9e1e5a515369b6c84ea2e1b7b

Authored by YunaiV
1 parent 0ae9af04

增加 MyBatis Plus 的字段加密时,查询的示例

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<String> {
29 29 @Override
30 30 public String getNullableResult(ResultSet rs, String columnName) throws SQLException {
31 31 String value = rs.getString(columnName);
32   - return getResult(value);
  32 + return decrypt(value);
33 33 }
34 34  
35 35 @Override
36 36 public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
37 37 String value = rs.getString(columnIndex);
38   - return getResult(value);
  38 + return decrypt(value);
39 39 }
40 40  
41 41 @Override
42 42 public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
43 43 String value = cs.getString(columnIndex);
44   - return getResult(value);
  44 + return decrypt(value);
45 45 }
46 46  
47   - private String getResult(String value) {
  47 + private static String decrypt(String value) {
48 48 if (value == null) {
49 49 return null;
50 50 }
51 51 return getEncryptor().decrypt(value);
52 52 }
53 53  
54   - private StringEncryptor getEncryptor() {
  54 + public static String encrypt(String rawValue) {
  55 + if (rawValue == null) {
  56 + return null;
  57 + }
  58 + return getEncryptor().encrypt(rawValue);
  59 + }
  60 +
  61 + private static StringEncryptor getEncryptor() {
55 62 if (encryptor != null) {
56 63 return encryptor;
57 64 }
... ...
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 {
132 132 assertServiceException(() -> dataSourceConfigService.deleteDataSourceConfig(id), DATA_SOURCE_CONFIG_NOT_EXISTS);
133 133 }
134 134  
  135 + @Test // 测试使用 password 查询,可以查询到数据
  136 + public void testSelectPassword() {
  137 + // mock 数据
  138 + DataSourceConfigDO dbDataSourceConfig = randomPojo(DataSourceConfigDO.class);
  139 + dataSourceConfigMapper.insert(dbDataSourceConfig);// @Sql: 先插入出一条存在的数据
  140 +
  141 + // 调用
  142 + DataSourceConfigDO result = dataSourceConfigMapper.selectOne(DataSourceConfigDO::getPassword,
  143 + EncryptTypeHandler.encrypt(dbDataSourceConfig.getPassword()));
  144 + System.out.println(result);
  145 + }
  146 +
135 147 }
... ...
yudao-server/src/main/resources/application.yaml
... ... @@ -61,6 +61,7 @@ mybatis-plus:
61 61 logic-delete-value: 1 # 逻辑已删除值(默认为 1)
62 62 logic-not-delete-value: 0 # 逻辑未删除值(默认为 0)
63 63 type-aliases-package: ${yudao.info.base-package}.module.*.dal.dataobject
  64 + type-handlers-package: ${yudao.info.base-package}.framework.mybatis.core.type
64 65  
65 66 --- #################### 芋道相关配置 ####################
66 67  
... ...