code review 脱敏组件的代码

This commit is contained in:
YunaiV 2023-01-17 08:11:57 +08:00
parent e5ab9b71b6
commit 32d7d6ab29
7 changed files with 26 additions and 13 deletions

View File

@ -9,8 +9,11 @@
<version>${revision}</version>
</parent>
<!-- TODO 城yudao-spring-boot-starter-biz-desensitize =》yudao-spring-boot-starter-desensitize不属于业务哈 -->
<artifactId>yudao-spring-boot-starter-biz-desensitize</artifactId>
<!-- TODO 城:这个组件的说明; -->
<!-- TODO 城:这里去掉哈 -->
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
@ -40,4 +43,4 @@
<scope>test</scope>
</dependency>
</dependencies>
</project>
</project>

View File

@ -8,14 +8,17 @@ import cn.iocoder.yudao.framework.desensitize.core.slider.handler.DefaultDesensi
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
// TODO @城DesensitizationHandlerHolder = DesensitizationHandlerFactory Holder 没有太多工厂的味道哈
/**
* 脱敏处理器 Holder
*/
public class DesensitizationHandlerHolder {
/**
* handler 缓存默认初始化内置的处理器
*/
private static final Map<Class<? extends DesensitizationHandler>, DesensitizationHandler> HANDLER_MAP = new ConcurrentHashMap<>() {{
// TODO @城 2 是不是不用呀需要的时候初始化
put(DefaultRegexDesensitizationHandler.class, new DefaultRegexDesensitizationHandler());
put(DefaultDesensitizationHandler.class, new DefaultDesensitizationHandler());
}};

View File

@ -11,19 +11,21 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
// TODO @城每个接口上author 写下哈
// TODO @城Desensitize = DesensitizeBy
// TODO @城Desensitize 类注释方便读者阅读理解
/**
* Desensitize 顶级脱敏注解
*/
@Documented
@Target({ ElementType.ANNOTATION_TYPE})
@Target(ElementType.ANNOTATION_TYPE)
@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotationsInside
@JsonSerialize(using = StringDesensitizeSerializer.class)
@JacksonAnnotationsInside // TODO @城这个的作用也可以写下
@JsonSerialize(using = StringDesensitizeSerializer.class) // TODO @城这个的作用也可以写下
public @interface Desensitize {
/**
* 脱敏处理器
*/
Class<? extends DesensitizationHandler> desensitizationBy();
Class<? extends DesensitizationHandler> desensitizationBy(); // TODO @城desensitizationBy -> handler
}

View File

@ -11,7 +11,7 @@ public interface DesensitizationHandler<T extends Annotation> {
* 脱敏
*
* @param origin 原始字符串
* @param anno 注解信息
* @param anno 注解信息 // TODO 不要这样的缩写哈anno -> annotation
* @return 脱敏后的字符串
*/
String desensitize(String origin, T anno);

View File

@ -15,6 +15,8 @@ public abstract class AbstractRegexDesensitizationHandler<T extends Annotation>
return origin.replaceAll(regex, replacer);
}
// TODO @城是不是抽象两个方法一个拿 regex一个拿 replacer
/**
* 获取注解的参数
*

View File

@ -12,25 +12,26 @@ public abstract class AbstractDesensitizationHandler<T extends Annotation> imple
int prefixKeep = (Integer) args[0];
int suffixKeep = (Integer) args[1];
String replacer = (String) args[2];
int length = origin.length();
// 原始字符串长度小于等于保留长度则原始字符串全部替换
// 情况一原始字符串长度小于等于保留长度则原始字符串全部替换
if (prefixKeep >= length || suffixKeep >= length) {
return buildReplacerByLength(replacer, length);
}
// 如果原始字符串小于等于前后缀保留字符串长度则原始字符串全部替换
// 情况二如果原始字符串小于等于前后缀保留字符串长度则原始字符串全部替换
if ((prefixKeep + suffixKeep) >= length) {
return buildReplacerByLength(replacer, length);
}
// 情况三TODO
int interval = length - prefixKeep - suffixKeep;
return origin.substring(0, prefixKeep) +
buildReplacerByLength(replacer, interval) +
origin.substring(prefixKeep + interval);
}
// TODO @城类似子类直接获取到参数哈
/**
* 获取注解的参数
*
@ -49,4 +50,5 @@ public abstract class AbstractDesensitizationHandler<T extends Annotation> imple
private String buildReplacerByLength(String replacer, int length) {
return String.valueOf(replacer).repeat(Math.max(0, length));
}
}

View File

@ -19,16 +19,19 @@ import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
// TODO @城挪到 base/serializer 包下
/**
* 脱敏序列化器
*/
public class StringDesensitizeSerializer extends StdSerializer<String> implements ContextualSerializer {
private DesensitizationHandler desensitizationHandler;
protected StringDesensitizeSerializer() {
super(String.class);
}
// TODO @城get set 方法是必须的么如果是的话可以换成 lombok 注解哈简洁一点~
public DesensitizationHandler getDesensitizationHandler() {
return desensitizationHandler;
}
@ -55,6 +58,7 @@ public class StringDesensitizeSerializer extends StdSerializer<String> implement
return;
}
// TODO @城抽个 private getField 方法让这个方法的逻辑主干更清晰
String currentName = gen.getOutputContext().getCurrentName();
Object currentValue = gen.getCurrentValue();
Class<?> currentValueClass = currentValue.getClass();
@ -66,16 +70,13 @@ public class StringDesensitizeSerializer extends StdSerializer<String> implement
gen.writeString(value);
return;
}
for (Annotation annotation : field.getAnnotations()) {
if (AnnotationUtil.hasAnnotation(annotation.annotationType(), Desensitize.class)) {
value = this.desensitizationHandler.desensitize(value, annotation);
gen.writeString(value);
return;
}
}
gen.writeString(value);
}