mirror of
https://gitee.com/huangge1199_admin/vue-pro.git
synced 2024-11-22 23:31:52 +08:00
code review 脱敏组件的代码
This commit is contained in:
parent
e5ab9b71b6
commit
32d7d6ab29
@ -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>
|
||||
|
@ -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());
|
||||
}};
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -15,6 +15,8 @@ public abstract class AbstractRegexDesensitizationHandler<T extends Annotation>
|
||||
return origin.replaceAll(regex, replacer);
|
||||
}
|
||||
|
||||
// TODO @城:是不是抽象两个方法,一个拿 regex;一个拿 replacer
|
||||
|
||||
/**
|
||||
* 获取注解的参数
|
||||
*
|
||||
|
@ -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));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user