From 9bc74d7e4e42cb5edabd1bab4a26a17a79f909a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Thu, 23 Nov 2023 10:40:54 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20@ColumnMask=EF=BC=8CtypeHandler=20?= =?UTF-8?q?=E4=B8=80=E8=B5=B7=E4=BD=BF=E7=94=A8=E6=97=B6=E5=86=B2=E7=AA=81?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98=20close=20#I8EXFW?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/mask/CompositeMaskTypeHandler.java | 55 +++++++++++++++++++ .../mybatisflex/core/table/ColumnInfo.java | 46 +++++++++++----- 2 files changed, 86 insertions(+), 15 deletions(-) create mode 100644 mybatis-flex-core/src/main/java/com/mybatisflex/core/mask/CompositeMaskTypeHandler.java diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/mask/CompositeMaskTypeHandler.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/mask/CompositeMaskTypeHandler.java new file mode 100644 index 00000000..0bb2c022 --- /dev/null +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/mask/CompositeMaskTypeHandler.java @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.mybatisflex.core.mask; + +import org.apache.ibatis.type.JdbcType; +import org.apache.ibatis.type.TypeHandler; + +import java.sql.CallableStatement; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +public class CompositeMaskTypeHandler implements TypeHandler { + + private final String maskType; + private final TypeHandler typeHandler; + + public CompositeMaskTypeHandler(String maskType, TypeHandler typeHandler) { + this.maskType = maskType; + this.typeHandler = typeHandler; + } + + @Override + public void setParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType) throws SQLException { + typeHandler.setParameter(ps, i, parameter, jdbcType); + } + + @Override + public Object getResult(ResultSet rs, String columnName) throws SQLException { + return MaskManager.mask(maskType, typeHandler.getResult(rs, columnName)); + } + + @Override + public Object getResult(ResultSet rs, int columnIndex) throws SQLException { + return MaskManager.mask(maskType, typeHandler.getResult(rs, columnIndex)); + } + + @Override + public Object getResult(CallableStatement cs, int columnIndex) throws SQLException { + return MaskManager.mask(maskType, typeHandler.getResult(cs, columnIndex)); + } +} diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/ColumnInfo.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/ColumnInfo.java index 7d8f021b..b8e70dbd 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/ColumnInfo.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/ColumnInfo.java @@ -16,6 +16,7 @@ package com.mybatisflex.core.table; import com.mybatisflex.core.FlexGlobalConfig; +import com.mybatisflex.core.mask.CompositeMaskTypeHandler; import com.mybatisflex.core.mask.MaskTypeHandler; import com.mybatisflex.core.util.StringUtil; import org.apache.ibatis.session.Configuration; @@ -54,6 +55,11 @@ public class ColumnInfo { */ protected TypeHandler typeHandler; + /** + * 最终使用和构建出来的 typeHandler + */ + protected TypeHandler buildTypeHandler; + /** * 数据脱敏类型。 */ @@ -107,23 +113,33 @@ public class ColumnInfo { public TypeHandler buildTypeHandler(Configuration configuration) { - //优先使用自定义的 typeHandler - if (typeHandler != null) { - return typeHandler; - } - //枚举 - else if (propertyType.isEnum()) { - if (configuration == null){ - configuration = FlexGlobalConfig.getDefaultConfig().getConfiguration(); - } - this.typeHandler = configuration.getTypeHandlerRegistry().getTypeHandler(propertyType); - } - //若用户未定义 typeHandler,而配置了数据脱敏,则使用脱敏的 handler 处理 - else if (StringUtil.isNotBlank(maskType)) { - typeHandler = new MaskTypeHandler(maskType); + if (buildTypeHandler != null) { + return buildTypeHandler; } - return typeHandler; + //脱敏规则配置 + else if (StringUtil.isNotBlank(maskType)) { + if (typeHandler != null) { + buildTypeHandler = new CompositeMaskTypeHandler(maskType, typeHandler); + } else { + buildTypeHandler = new MaskTypeHandler(maskType); + } + } + + //用户自定义的 typeHandler + else if (typeHandler != null) { + buildTypeHandler = typeHandler; + } + + //枚举 + else if (propertyType.isEnum()) { + if (configuration == null) { + configuration = FlexGlobalConfig.getDefaultConfig().getConfiguration(); + } + buildTypeHandler = configuration.getTypeHandlerRegistry().getTypeHandler(propertyType); + } + + return buildTypeHandler; } public void setTypeHandler(TypeHandler typeHandler) {