mirror of
https://github.com/continew-org/continew-starter.git
synced 2025-09-09 04:59:21 +08:00
refactor(security/mask): 支持自定义脱敏策略
This commit is contained in:
@@ -21,6 +21,7 @@ import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
import top.charles7c.continew.starter.core.constant.StringConstants;
|
||||
import top.charles7c.continew.starter.security.mask.core.JsonMaskSerializer;
|
||||
import top.charles7c.continew.starter.security.mask.enums.MaskType;
|
||||
import top.charles7c.continew.starter.security.mask.strategy.IMaskStrategy;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
@@ -44,6 +45,14 @@ public @interface JsonMask {
|
||||
*/
|
||||
MaskType value() default MaskType.CUSTOM;
|
||||
|
||||
/**
|
||||
* 脱敏策略
|
||||
* <p>
|
||||
* 优先级高于脱敏类型
|
||||
* </p>
|
||||
*/
|
||||
Class<? extends IMaskStrategy> strategy() default IMaskStrategy.class;
|
||||
|
||||
/**
|
||||
* 左侧保留位数
|
||||
* <p>
|
||||
|
@@ -18,6 +18,7 @@ package top.charles7c.continew.starter.security.mask.core;
|
||||
|
||||
import cn.hutool.core.text.CharSequenceUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.databind.BeanProperty;
|
||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||
@@ -26,7 +27,7 @@ import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import com.fasterxml.jackson.databind.ser.ContextualSerializer;
|
||||
import top.charles7c.continew.starter.core.constant.StringConstants;
|
||||
import top.charles7c.continew.starter.security.mask.annotation.JsonMask;
|
||||
import top.charles7c.continew.starter.security.mask.enums.MaskType;
|
||||
import top.charles7c.continew.starter.security.mask.strategy.IMaskStrategy;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Objects;
|
||||
@@ -56,8 +57,12 @@ public class JsonMaskSerializer extends JsonSerializer<String> implements Contex
|
||||
jsonGenerator.writeString(StringConstants.EMPTY);
|
||||
return;
|
||||
}
|
||||
MaskType maskType = jsonMask.value();
|
||||
jsonGenerator.writeString(maskType.mask(str, jsonMask.character(), jsonMask.left(), jsonMask.right()));
|
||||
// 使用自定义脱敏策略
|
||||
Class<? extends IMaskStrategy> strategyClass = jsonMask.strategy();
|
||||
IMaskStrategy maskStrategy = strategyClass != IMaskStrategy.class
|
||||
? SpringUtil.getBean(strategyClass)
|
||||
: jsonMask.value();
|
||||
jsonGenerator.writeString(maskStrategy.mask(str, jsonMask.character(), jsonMask.left(), jsonMask.right()));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -17,8 +17,8 @@
|
||||
package top.charles7c.continew.starter.security.mask.enums;
|
||||
|
||||
import cn.hutool.core.text.CharSequenceUtil;
|
||||
import cn.hutool.core.util.CharUtil;
|
||||
import top.charles7c.continew.starter.core.constant.StringConstants;
|
||||
import top.charles7c.continew.starter.security.mask.strategy.IMaskStrategy;
|
||||
|
||||
/**
|
||||
* 脱敏类型
|
||||
@@ -26,7 +26,7 @@ import top.charles7c.continew.starter.core.constant.StringConstants;
|
||||
* @author Charles7c
|
||||
* @since 1.4.0
|
||||
*/
|
||||
public enum MaskType {
|
||||
public enum MaskType implements IMaskStrategy {
|
||||
|
||||
/**
|
||||
* 自定义脱敏
|
||||
@@ -119,11 +119,11 @@ public enum MaskType {
|
||||
buffer.append(cleanStr, 0, 4);
|
||||
for (int i = 0; i < midLength; ++i) {
|
||||
if (i % 4 == 0) {
|
||||
buffer.append(CharUtil.SPACE);
|
||||
buffer.append(StringConstants.SPACE);
|
||||
}
|
||||
buffer.append(character);
|
||||
}
|
||||
buffer.append(CharUtil.SPACE).append(cleanStr, length - endLength, length);
|
||||
buffer.append(StringConstants.SPACE).append(cleanStr, length - endLength, length);
|
||||
return buffer.toString();
|
||||
}
|
||||
},
|
||||
@@ -217,15 +217,4 @@ public enum MaskType {
|
||||
.format(":%s:%s:%s:%s:%s:%s:%s", character, character, character, character, character, character, character);
|
||||
}
|
||||
},;
|
||||
|
||||
/**
|
||||
* 数据脱敏
|
||||
*
|
||||
* @param str 原始字符串
|
||||
* @param character 脱敏符号
|
||||
* @param left 左侧保留位数
|
||||
* @param right 右侧保留位数
|
||||
* @return 脱敏后的数据
|
||||
*/
|
||||
public abstract String mask(String str, char character, int left, int right);
|
||||
}
|
||||
|
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||
* <p>
|
||||
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
* <p>
|
||||
* http://www.gnu.org/licenses/lgpl.html
|
||||
* <p>
|
||||
* 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 top.charles7c.continew.starter.security.mask.strategy;
|
||||
|
||||
/**
|
||||
* 脱敏策略
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 1.4.0
|
||||
*/
|
||||
public interface IMaskStrategy {
|
||||
|
||||
/**
|
||||
* 数据脱敏
|
||||
*
|
||||
* @param str 原始字符串
|
||||
* @param character 脱敏符号
|
||||
* @param left 左侧保留位数
|
||||
* @param right 右侧保留位数
|
||||
* @return 脱敏后的数据
|
||||
*/
|
||||
String mask(String str, char character, int left, int right);
|
||||
}
|
Reference in New Issue
Block a user