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

This commit is contained in:
YunaiV 2022-05-19 23:01:35 +08:00
parent 0ae9af0492
commit e81ac9c1d0
3 changed files with 25 additions and 5 deletions

View File

@ -29,29 +29,36 @@ public class EncryptTypeHandler extends BaseTypeHandler<String> {
@Override @Override
public String getNullableResult(ResultSet rs, String columnName) throws SQLException { public String getNullableResult(ResultSet rs, String columnName) throws SQLException {
String value = rs.getString(columnName); String value = rs.getString(columnName);
return getResult(value); return decrypt(value);
} }
@Override @Override
public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException { public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
String value = rs.getString(columnIndex); String value = rs.getString(columnIndex);
return getResult(value); return decrypt(value);
} }
@Override @Override
public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
String value = cs.getString(columnIndex); 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) { if (value == null) {
return null; return null;
} }
return getEncryptor().decrypt(value); 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) { if (encryptor != null) {
return encryptor; return encryptor;
} }

View File

@ -132,4 +132,16 @@ public class DataSourceConfigServiceImplTest extends BaseDbUnitTest {
assertServiceException(() -> dataSourceConfigService.deleteDataSourceConfig(id), DATA_SOURCE_CONFIG_NOT_EXISTS); 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);
}
} }

View File

@ -61,6 +61,7 @@ mybatis-plus:
logic-delete-value: 1 # 逻辑已删除值(默认为 1) logic-delete-value: 1 # 逻辑已删除值(默认为 1)
logic-not-delete-value: 0 # 逻辑未删除值(默认为 0) logic-not-delete-value: 0 # 逻辑未删除值(默认为 0)
type-aliases-package: ${yudao.info.base-package}.module.*.dal.dataobject type-aliases-package: ${yudao.info.base-package}.module.*.dal.dataobject
type-handlers-package: ${yudao.info.base-package}.framework.mybatis.core.type
--- #################### 芋道相关配置 #################### --- #################### 芋道相关配置 ####################