feat: 脱敏组件代码规范更新

This commit is contained in:
gaibu 2023-01-17 10:52:37 +08:00
parent 32d7d6ab29
commit 7b3fca2844
38 changed files with 390 additions and 209 deletions

View File

@ -143,7 +143,7 @@
</dependency> </dependency>
<dependency> <dependency>
<groupId>cn.iocoder.boot</groupId> <groupId>cn.iocoder.boot</groupId>
<artifactId>yudao-spring-boot-starter-biz-desensitize</artifactId> <artifactId>yudao-spring-boot-starter-desensitize</artifactId>
<version>${revision}</version> <version>${revision}</version>
</dependency> </dependency>

View File

@ -41,7 +41,7 @@
<module>yudao-spring-boot-starter-flowable</module> <module>yudao-spring-boot-starter-flowable</module>
<module>yudao-spring-boot-starter-captcha</module> <module>yudao-spring-boot-starter-captcha</module>
<module>yudao-spring-boot-starter-websocket</module> <module>yudao-spring-boot-starter-websocket</module>
<module>yudao-spring-boot-starter-biz-desensitize</module> <module>yudao-spring-boot-starter-desensitize</module>
</modules> </modules>
<artifactId>yudao-framework</artifactId> <artifactId>yudao-framework</artifactId>

View File

@ -1,27 +0,0 @@
package cn.iocoder.yudao.framework.desensitize.core.regex.handler;
import cn.iocoder.yudao.framework.desensitize.core.base.handler.DesensitizationHandler;
import java.lang.annotation.Annotation;
public abstract class AbstractRegexDesensitizationHandler<T extends Annotation> implements DesensitizationHandler<T> {
@Override
public String desensitize(String origin, T anno) {
Object[] args = getArgs(anno);
String regex = (String) args[0];
String replacer = (String) args[1];
return origin.replaceAll(regex, replacer);
}
// TODO @城是不是抽象两个方法一个拿 regex一个拿 replacer
/**
* 获取注解的参数
*
* @param anno 注解信息
* @return 注解的参数
*/
abstract Object[] getArgs(T anno);
}

View File

@ -1,11 +0,0 @@
package cn.iocoder.yudao.framework.desensitize.core.slider.handler;
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.BankCard;
public class BankCardDesensitization extends AbstractDesensitizationHandler<BankCard> {
@Override
Object[] getArgs(BankCard anno) {
return new Object[]{anno.prefixKeep(), anno.suffixKeep(), anno.replacer()};
}
}

View File

@ -1,10 +0,0 @@
package cn.iocoder.yudao.framework.desensitize.core.slider.handler;
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.CarLicense;
public class CarLicenseDesensitization extends AbstractDesensitizationHandler<CarLicense> {
@Override
Object[] getArgs(CarLicense anno) {
return new Object[]{anno.prefixKeep(), anno.suffixKeep(), anno.replacer()};
}
}

View File

@ -1,10 +0,0 @@
package cn.iocoder.yudao.framework.desensitize.core.slider.handler;
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.ChineseName;
public class ChineseNameDesensitization extends AbstractDesensitizationHandler<ChineseName> {
@Override
Object[] getArgs(ChineseName anno) {
return new Object[]{anno.prefixKeep(), anno.suffixKeep(), anno.replacer()};
}
}

View File

@ -1,14 +0,0 @@
package cn.iocoder.yudao.framework.desensitize.core.slider.handler;
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.Slider;
/**
* 滑动脱敏处理器
*/
public class DefaultDesensitizationHandler extends AbstractDesensitizationHandler<Slider> {
@Override
Object[] getArgs(Slider anno) {
return new Object[]{anno.prefixKeep(), anno.suffixKeep(), anno.replacer()};
}
}

View File

@ -1,10 +0,0 @@
package cn.iocoder.yudao.framework.desensitize.core.slider.handler;
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.FixedPhone;
public class FixedPhoneDesensitization extends AbstractDesensitizationHandler<FixedPhone> {
@Override
Object[] getArgs(FixedPhone anno) {
return new Object[]{anno.prefixKeep(), anno.suffixKeep(), anno.replacer()};
}
}

View File

@ -1,10 +0,0 @@
package cn.iocoder.yudao.framework.desensitize.core.slider.handler;
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.IdCard;
public class IdCardDesensitization extends AbstractDesensitizationHandler<IdCard> {
@Override
Object[] getArgs(IdCard anno) {
return new Object[]{anno.prefixKeep(), anno.suffixKeep(), anno.replacer()};
}
}

View File

@ -1,10 +0,0 @@
package cn.iocoder.yudao.framework.desensitize.core.slider.handler;
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.Password;
public class PasswordDesensitization extends AbstractDesensitizationHandler<Password> {
@Override
Object[] getArgs(Password anno) {
return new Object[]{anno.prefixKeep(), anno.suffixKeep(), anno.replacer()};
}
}

View File

@ -1,11 +0,0 @@
package cn.iocoder.yudao.framework.desensitize.core.slider.handler;
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.PhoneNumber;
public class PhoneNumberDesensitization extends AbstractDesensitizationHandler<PhoneNumber> {
@Override
Object[] getArgs(PhoneNumber anno) {
return new Object[]{anno.prefixKeep(), anno.suffixKeep(), anno.replacer()};
}
}

View File

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

View File

@ -2,33 +2,28 @@ package cn.iocoder.yudao.framework.desensitize.core.base;
import cn.hutool.core.util.ReflectUtil; import cn.hutool.core.util.ReflectUtil;
import cn.iocoder.yudao.framework.desensitize.core.base.handler.DesensitizationHandler; import cn.iocoder.yudao.framework.desensitize.core.base.handler.DesensitizationHandler;
import cn.iocoder.yudao.framework.desensitize.core.regex.handler.DefaultRegexDesensitizationHandler;
import cn.iocoder.yudao.framework.desensitize.core.slider.handler.DefaultDesensitizationHandler;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
// TODO @城DesensitizationHandlerHolder = DesensitizationHandlerFactory Holder 没有太多工厂的味道哈
/** /**
* 脱敏处理器 Holder * 脱敏处理器 Holder
*
* @author gaibu
*/ */
public class DesensitizationHandlerHolder { public class DesensitizationHandlerFactory {
/** /**
* handler 缓存默认初始化内置的处理器 * handler 缓存默认初始化内置的处理器
*/ */
private static final Map<Class<? extends DesensitizationHandler>, DesensitizationHandler> HANDLER_MAP = new ConcurrentHashMap<>() {{ private static final Map<Class<? extends DesensitizationHandler>, DesensitizationHandler> HANDLER_MAP = new ConcurrentHashMap<Class<? extends DesensitizationHandler>, DesensitizationHandler>();
// TODO @城 2 是不是不用呀需要的时候初始化
put(DefaultRegexDesensitizationHandler.class, new DefaultRegexDesensitizationHandler());
put(DefaultDesensitizationHandler.class, new DefaultDesensitizationHandler());
}};
public static DesensitizationHandler getDesensitizationHandler(Class<? extends DesensitizationHandler> clazz) { public static DesensitizationHandler getDesensitizationHandler(Class<? extends DesensitizationHandler> clazz) {
DesensitizationHandler handler = HANDLER_MAP.get(clazz); DesensitizationHandler handler = HANDLER_MAP.get(clazz);
if (handler != null) { if (handler != null) {
return handler; return handler;
} }
synchronized (DesensitizationHandlerHolder.class) { synchronized (DesensitizationHandlerFactory.class) {
handler = HANDLER_MAP.get(clazz); handler = HANDLER_MAP.get(clazz);
// 双重校验锁 // 双重校验锁
if (handler != null) { if (handler != null) {

View File

@ -1,7 +1,7 @@
package cn.iocoder.yudao.framework.desensitize.core.base.annotation; package cn.iocoder.yudao.framework.desensitize.core.base.annotation;
import cn.iocoder.yudao.framework.desensitize.core.base.handler.DesensitizationHandler; import cn.iocoder.yudao.framework.desensitize.core.base.handler.DesensitizationHandler;
import cn.iocoder.yudao.framework.desensitize.serializer.StringDesensitizeSerializer; import cn.iocoder.yudao.framework.desensitize.core.base.serializer.StringDesensitizeSerializer;
import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
import com.fasterxml.jackson.databind.annotation.JsonSerialize; import com.fasterxml.jackson.databind.annotation.JsonSerialize;
@ -11,21 +11,20 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
// TODO @城每个接口上author 写下哈
// TODO @城Desensitize = DesensitizeBy
// TODO @城Desensitize 类注释方便读者阅读理解
/** /**
* Desensitize 顶级脱敏注解 * 顶级脱敏注解自定义注解需要使用此注解
*
* @author gaibu
*/ */
@Documented @Documented
@Target(ElementType.ANNOTATION_TYPE) @Target(ElementType.ANNOTATION_TYPE)
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotationsInside // TODO @城这个的作用也可以写下 @JacksonAnnotationsInside // 此注解是其他所有 jackson 注解的元注解打上了此注解的注解表明是 jackson 注解的一部分
@JsonSerialize(using = StringDesensitizeSerializer.class) // TODO @城这个的作用也可以写下 @JsonSerialize(using = StringDesensitizeSerializer.class) // 指定序列化器
public @interface Desensitize { public @interface DesensitizeBy {
/** /**
* 脱敏处理器 * 脱敏处理器
*/ */
Class<? extends DesensitizationHandler> desensitizationBy(); // TODO @城desensitizationBy -> handler Class<? extends DesensitizationHandler> handler();
} }

View File

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

View File

@ -1,12 +1,12 @@
package cn.iocoder.yudao.framework.desensitize.serializer; package cn.iocoder.yudao.framework.desensitize.core.base.serializer;
import cn.hutool.core.annotation.AnnotationUtil; import cn.hutool.core.annotation.AnnotationUtil;
import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ReflectUtil; import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.desensitize.core.base.annotation.Desensitize; import cn.iocoder.yudao.framework.desensitize.core.base.annotation.DesensitizeBy;
import cn.iocoder.yudao.framework.desensitize.core.base.handler.DesensitizationHandler; import cn.iocoder.yudao.framework.desensitize.core.base.handler.DesensitizationHandler;
import cn.iocoder.yudao.framework.desensitize.core.base.DesensitizationHandlerHolder; import cn.iocoder.yudao.framework.desensitize.core.base.DesensitizationHandlerFactory;
import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.BeanProperty; import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.JsonMappingException;
@ -14,40 +14,36 @@ import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider; import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.ContextualSerializer; import com.fasterxml.jackson.databind.ser.ContextualSerializer;
import com.fasterxml.jackson.databind.ser.std.StdSerializer; import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import lombok.Getter;
import lombok.Setter;
import java.io.IOException; import java.io.IOException;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
import java.lang.reflect.Field; import java.lang.reflect.Field;
// TODO @城挪到 base/serializer 包下
/** /**
* 脱敏序列化器 * 脱敏序列化器
*
* @author gaibu
*/ */
public class StringDesensitizeSerializer extends StdSerializer<String> implements ContextualSerializer { public class StringDesensitizeSerializer extends StdSerializer<String> implements ContextualSerializer {
@Getter
@Setter
private DesensitizationHandler desensitizationHandler; private DesensitizationHandler desensitizationHandler;
protected StringDesensitizeSerializer() { protected StringDesensitizeSerializer() {
super(String.class); super(String.class);
} }
// TODO @城get set 方法是必须的么如果是的话可以换成 lombok 注解哈简洁一点~
public DesensitizationHandler getDesensitizationHandler() {
return desensitizationHandler;
}
public void setDesensitizationHandler(DesensitizationHandler desensitizationHandler) {
this.desensitizationHandler = desensitizationHandler;
}
@Override @Override
public JsonSerializer<?> createContextual(SerializerProvider serializerProvider, BeanProperty beanProperty) throws JsonMappingException { public JsonSerializer<?> createContextual(SerializerProvider serializerProvider, BeanProperty beanProperty) throws JsonMappingException {
Desensitize annotation = beanProperty.getAnnotation(Desensitize.class); DesensitizeBy annotation = beanProperty.getAnnotation(DesensitizeBy.class);
if (annotation == null) { if (annotation == null) {
return this; return this;
} }
StringDesensitizeSerializer serializer = new StringDesensitizeSerializer(); StringDesensitizeSerializer serializer = new StringDesensitizeSerializer();
serializer.setDesensitizationHandler(DesensitizationHandlerHolder.getDesensitizationHandler(annotation.desensitizationBy())); serializer.setDesensitizationHandler(DesensitizationHandlerFactory.getDesensitizationHandler(annotation.handler()));
return serializer; return serializer;
} }
@ -57,21 +53,17 @@ public class StringDesensitizeSerializer extends StdSerializer<String> implement
gen.writeNull(); gen.writeNull();
return; return;
} }
// 获取序列化字段
// TODO @城抽个 private getField 方法让这个方法的逻辑主干更清晰 Field field = getField(gen);
String currentName = gen.getOutputContext().getCurrentName();
Object currentValue = gen.getCurrentValue();
Class<?> currentValueClass = currentValue.getClass();
Field field = ReflectUtil.getField(currentValueClass, currentName);
// 自定义处理器 // 自定义处理器
Desensitize[] annotations = AnnotationUtil.getCombinationAnnotations(field, Desensitize.class); DesensitizeBy[] annotations = AnnotationUtil.getCombinationAnnotations(field, DesensitizeBy.class);
if (ArrayUtil.isEmpty(annotations)) { if (ArrayUtil.isEmpty(annotations)) {
gen.writeString(value); gen.writeString(value);
return; return;
} }
for (Annotation annotation : field.getAnnotations()) { for (Annotation annotation : field.getAnnotations()) {
if (AnnotationUtil.hasAnnotation(annotation.annotationType(), Desensitize.class)) { if (AnnotationUtil.hasAnnotation(annotation.annotationType(), DesensitizeBy.class)) {
value = this.desensitizationHandler.desensitize(value, annotation); value = this.desensitizationHandler.desensitize(value, annotation);
gen.writeString(value); gen.writeString(value);
return; return;
@ -80,4 +72,17 @@ public class StringDesensitizeSerializer extends StdSerializer<String> implement
gen.writeString(value); gen.writeString(value);
} }
/**
* 获取字段
*
* @param gen JsonGenerator
* @return 字段
*/
private Field getField(JsonGenerator gen) {
String currentName = gen.getOutputContext().getCurrentName();
Object currentValue = gen.getCurrentValue();
Class<?> currentValueClass = currentValue.getClass();
return ReflectUtil.getField(currentValueClass, currentName);
}
} }

View File

@ -1,6 +1,6 @@
package cn.iocoder.yudao.framework.desensitize.core.regex.annotation; package cn.iocoder.yudao.framework.desensitize.core.regex.annotation;
import cn.iocoder.yudao.framework.desensitize.core.base.annotation.Desensitize; import cn.iocoder.yudao.framework.desensitize.core.base.annotation.DesensitizeBy;
import cn.iocoder.yudao.framework.desensitize.core.regex.handler.EmailDesensitizationHandler; import cn.iocoder.yudao.framework.desensitize.core.regex.handler.EmailDesensitizationHandler;
import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
@ -12,13 +12,16 @@ import java.lang.annotation.Target;
/** /**
* 邮箱 * 邮箱
*
* @author gaibu
*/ */
@Documented @Documented
@Target({ElementType.FIELD}) @Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotationsInside @JacksonAnnotationsInside
@Desensitize(desensitizationBy = EmailDesensitizationHandler.class) @DesensitizeBy(handler = EmailDesensitizationHandler.class)
public @interface EmailDesensitize { public @interface EmailDesensitize {
/** /**
* 匹配的正则表达式 * 匹配的正则表达式
*/ */

View File

@ -1,6 +1,6 @@
package cn.iocoder.yudao.framework.desensitize.core.regex.annotation; package cn.iocoder.yudao.framework.desensitize.core.regex.annotation;
import cn.iocoder.yudao.framework.desensitize.core.base.annotation.Desensitize; import cn.iocoder.yudao.framework.desensitize.core.base.annotation.DesensitizeBy;
import cn.iocoder.yudao.framework.desensitize.core.regex.handler.DefaultRegexDesensitizationHandler; import cn.iocoder.yudao.framework.desensitize.core.regex.handler.DefaultRegexDesensitizationHandler;
import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
@ -12,13 +12,16 @@ import java.lang.annotation.Target;
/** /**
* 正则脱敏注解 * 正则脱敏注解
*
* @author gaibu
*/ */
@Documented @Documented
@Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE}) @Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotationsInside @JacksonAnnotationsInside
@Desensitize(desensitizationBy = DefaultRegexDesensitizationHandler.class) @DesensitizeBy(handler = DefaultRegexDesensitizationHandler.class)
public @interface RegexDesensitize { public @interface RegexDesensitize {
/** /**
* 匹配的正则表达式默认匹配所有 * 匹配的正则表达式默认匹配所有
*/ */

View File

@ -0,0 +1,37 @@
package cn.iocoder.yudao.framework.desensitize.core.regex.handler;
import cn.iocoder.yudao.framework.desensitize.core.base.handler.DesensitizationHandler;
import java.lang.annotation.Annotation;
/**
* 正则表达式脱敏处理器抽象类已实现通用的方法
*
* @author gaibu
*/
public abstract class AbstractRegexDesensitizationHandler<T extends Annotation> implements DesensitizationHandler<T> {
@Override
public String desensitize(String origin, T annotation) {
String regex = getRegex(annotation);
String replacer = getReplacer(annotation);
return origin.replaceAll(regex, replacer);
}
/**
* 获取注解上的 regex 参数
*
* @param annotation 注解信息
* @return 正则表达式
*/
abstract String getRegex(T annotation);
/**
* 获取注解上的 replacer 参数
*
* @param annotation 注解信息
* @return 待替换的字符串
*/
abstract String getReplacer(T annotation);
}

View File

@ -3,12 +3,20 @@ package cn.iocoder.yudao.framework.desensitize.core.regex.handler;
import cn.iocoder.yudao.framework.desensitize.core.regex.annotation.RegexDesensitize; import cn.iocoder.yudao.framework.desensitize.core.regex.annotation.RegexDesensitize;
/** /**
* 正则脱敏处理器 * 默认正则脱敏处理器
*
* @author gaibu
*/ */
public class DefaultRegexDesensitizationHandler extends AbstractRegexDesensitizationHandler<RegexDesensitize> { public class DefaultRegexDesensitizationHandler extends AbstractRegexDesensitizationHandler<RegexDesensitize> {
@Override @Override
Object[] getArgs(RegexDesensitize anno) { String getRegex(RegexDesensitize annotation) {
return new Object[]{anno.regex(), anno.replacer()}; return annotation.regex();
}
@Override
String getReplacer(RegexDesensitize annotation) {
return annotation.replacer();
} }
} }

View File

@ -2,10 +2,20 @@ package cn.iocoder.yudao.framework.desensitize.core.regex.handler;
import cn.iocoder.yudao.framework.desensitize.core.regex.annotation.EmailDesensitize; import cn.iocoder.yudao.framework.desensitize.core.regex.annotation.EmailDesensitize;
/**
* 邮箱脱敏处理器
*
* @author gaibu
*/
public class EmailDesensitizationHandler extends AbstractRegexDesensitizationHandler<EmailDesensitize> { public class EmailDesensitizationHandler extends AbstractRegexDesensitizationHandler<EmailDesensitize> {
@Override @Override
Object[] getArgs(EmailDesensitize anno) { String getRegex(EmailDesensitize annotation) {
return new Object[]{anno.regex(), anno.replacer()}; return annotation.regex();
}
@Override
String getReplacer(EmailDesensitize annotation) {
return annotation.replacer();
} }
} }

View File

@ -1,6 +1,6 @@
package cn.iocoder.yudao.framework.desensitize.core.slider.annotation; package cn.iocoder.yudao.framework.desensitize.core.slider.annotation;
import cn.iocoder.yudao.framework.desensitize.core.base.annotation.Desensitize; import cn.iocoder.yudao.framework.desensitize.core.base.annotation.DesensitizeBy;
import cn.iocoder.yudao.framework.desensitize.core.slider.handler.BankCardDesensitization; import cn.iocoder.yudao.framework.desensitize.core.slider.handler.BankCardDesensitization;
import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
@ -12,12 +12,14 @@ import java.lang.annotation.Target;
/** /**
* 银行卡号 * 银行卡号
*
* @author gaibu
*/ */
@Documented @Documented
@Target({ElementType.FIELD}) @Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotationsInside @JacksonAnnotationsInside
@Desensitize(desensitizationBy = BankCardDesensitization.class) @DesensitizeBy(handler = BankCardDesensitization.class)
public @interface BankCard { public @interface BankCard {
/** /**

View File

@ -1,6 +1,6 @@
package cn.iocoder.yudao.framework.desensitize.core.slider.annotation; package cn.iocoder.yudao.framework.desensitize.core.slider.annotation;
import cn.iocoder.yudao.framework.desensitize.core.base.annotation.Desensitize; import cn.iocoder.yudao.framework.desensitize.core.base.annotation.DesensitizeBy;
import cn.iocoder.yudao.framework.desensitize.core.slider.handler.CarLicenseDesensitization; import cn.iocoder.yudao.framework.desensitize.core.slider.handler.CarLicenseDesensitization;
import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
@ -12,12 +12,14 @@ import java.lang.annotation.Target;
/** /**
* 车牌号 * 车牌号
*
* @author gaibu
*/ */
@Documented @Documented
@Target({ElementType.FIELD}) @Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotationsInside @JacksonAnnotationsInside
@Desensitize(desensitizationBy = CarLicenseDesensitization.class) @DesensitizeBy(handler = CarLicenseDesensitization.class)
public @interface CarLicense { public @interface CarLicense {
/** /**

View File

@ -1,6 +1,6 @@
package cn.iocoder.yudao.framework.desensitize.core.slider.annotation; package cn.iocoder.yudao.framework.desensitize.core.slider.annotation;
import cn.iocoder.yudao.framework.desensitize.core.base.annotation.Desensitize; import cn.iocoder.yudao.framework.desensitize.core.base.annotation.DesensitizeBy;
import cn.iocoder.yudao.framework.desensitize.core.slider.handler.ChineseNameDesensitization; import cn.iocoder.yudao.framework.desensitize.core.slider.handler.ChineseNameDesensitization;
import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
@ -12,12 +12,14 @@ import java.lang.annotation.Target;
/** /**
* 中文名 * 中文名
*
* @author gaibu
*/ */
@Documented @Documented
@Target({ElementType.FIELD}) @Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotationsInside @JacksonAnnotationsInside
@Desensitize(desensitizationBy = ChineseNameDesensitization.class) @DesensitizeBy(handler = ChineseNameDesensitization.class)
public @interface ChineseName { public @interface ChineseName {
/** /**

View File

@ -1,6 +1,6 @@
package cn.iocoder.yudao.framework.desensitize.core.slider.annotation; package cn.iocoder.yudao.framework.desensitize.core.slider.annotation;
import cn.iocoder.yudao.framework.desensitize.core.base.annotation.Desensitize; import cn.iocoder.yudao.framework.desensitize.core.base.annotation.DesensitizeBy;
import cn.iocoder.yudao.framework.desensitize.core.slider.handler.FixedPhoneDesensitization; import cn.iocoder.yudao.framework.desensitize.core.slider.handler.FixedPhoneDesensitization;
import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
@ -12,12 +12,14 @@ import java.lang.annotation.Target;
/** /**
* 固定电话 * 固定电话
*
* @author gaibu
*/ */
@Documented @Documented
@Target({ElementType.FIELD}) @Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotationsInside @JacksonAnnotationsInside
@Desensitize(desensitizationBy = FixedPhoneDesensitization.class) @DesensitizeBy(handler = FixedPhoneDesensitization.class)
public @interface FixedPhone { public @interface FixedPhone {
/** /**

View File

@ -1,6 +1,6 @@
package cn.iocoder.yudao.framework.desensitize.core.slider.annotation; package cn.iocoder.yudao.framework.desensitize.core.slider.annotation;
import cn.iocoder.yudao.framework.desensitize.core.base.annotation.Desensitize; import cn.iocoder.yudao.framework.desensitize.core.base.annotation.DesensitizeBy;
import cn.iocoder.yudao.framework.desensitize.core.slider.handler.IdCardDesensitization; import cn.iocoder.yudao.framework.desensitize.core.slider.handler.IdCardDesensitization;
import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
@ -12,12 +12,14 @@ import java.lang.annotation.Target;
/** /**
* 身份证 * 身份证
*
* @author gaibu
*/ */
@Documented @Documented
@Target({ElementType.FIELD}) @Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotationsInside @JacksonAnnotationsInside
@Desensitize(desensitizationBy = IdCardDesensitization.class) @DesensitizeBy(handler = IdCardDesensitization.class)
public @interface IdCard { public @interface IdCard {
/** /**

View File

@ -1,6 +1,6 @@
package cn.iocoder.yudao.framework.desensitize.core.slider.annotation; package cn.iocoder.yudao.framework.desensitize.core.slider.annotation;
import cn.iocoder.yudao.framework.desensitize.core.base.annotation.Desensitize; import cn.iocoder.yudao.framework.desensitize.core.base.annotation.DesensitizeBy;
import cn.iocoder.yudao.framework.desensitize.core.slider.handler.PasswordDesensitization; import cn.iocoder.yudao.framework.desensitize.core.slider.handler.PasswordDesensitization;
import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
@ -12,12 +12,14 @@ import java.lang.annotation.Target;
/** /**
* 密码 * 密码
*
* @author gaibu
*/ */
@Documented @Documented
@Target({ElementType.FIELD}) @Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotationsInside @JacksonAnnotationsInside
@Desensitize(desensitizationBy = PasswordDesensitization.class) @DesensitizeBy(handler = PasswordDesensitization.class)
public @interface Password { public @interface Password {
/** /**

View File

@ -1,6 +1,6 @@
package cn.iocoder.yudao.framework.desensitize.core.slider.annotation; package cn.iocoder.yudao.framework.desensitize.core.slider.annotation;
import cn.iocoder.yudao.framework.desensitize.core.base.annotation.Desensitize; import cn.iocoder.yudao.framework.desensitize.core.base.annotation.DesensitizeBy;
import cn.iocoder.yudao.framework.desensitize.core.slider.handler.PhoneNumberDesensitization; import cn.iocoder.yudao.framework.desensitize.core.slider.handler.PhoneNumberDesensitization;
import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
@ -12,12 +12,14 @@ import java.lang.annotation.Target;
/** /**
* 手机号 * 手机号
*
* @author gaibu
*/ */
@Documented @Documented
@Target({ElementType.FIELD}) @Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotationsInside @JacksonAnnotationsInside
@Desensitize(desensitizationBy = PhoneNumberDesensitization.class) @DesensitizeBy(handler = PhoneNumberDesensitization.class)
public @interface PhoneNumber { public @interface PhoneNumber {
/** /**

View File

@ -1,6 +1,6 @@
package cn.iocoder.yudao.framework.desensitize.core.slider.annotation; package cn.iocoder.yudao.framework.desensitize.core.slider.annotation;
import cn.iocoder.yudao.framework.desensitize.core.base.annotation.Desensitize; import cn.iocoder.yudao.framework.desensitize.core.base.annotation.DesensitizeBy;
import cn.iocoder.yudao.framework.desensitize.core.slider.handler.DefaultDesensitizationHandler; import cn.iocoder.yudao.framework.desensitize.core.slider.handler.DefaultDesensitizationHandler;
import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
@ -12,12 +12,14 @@ import java.lang.annotation.Target;
/** /**
* 滑动脱敏注解 * 滑动脱敏注解
*
* @author gaibu
*/ */
@Documented @Documented
@Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE}) @Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotationsInside @JacksonAnnotationsInside
@Desensitize(desensitizationBy = DefaultDesensitizationHandler.class) @DesensitizeBy(handler = DefaultDesensitizationHandler.class)
public @interface Slider { public @interface Slider {
/** /**

View File

@ -4,14 +4,18 @@ import cn.iocoder.yudao.framework.desensitize.core.base.handler.DesensitizationH
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
/**
* 滑动脱敏处理器抽象类已实现通用的方法
*
* @author gaibu
*/
public abstract class AbstractDesensitizationHandler<T extends Annotation> implements DesensitizationHandler<T> { public abstract class AbstractDesensitizationHandler<T extends Annotation> implements DesensitizationHandler<T> {
@Override @Override
public String desensitize(String origin, T anno) { public String desensitize(String origin, T annotation) {
Object[] args = getArgs(anno); int prefixKeep = getPrefixKeep(annotation);
int prefixKeep = (Integer) args[0]; int suffixKeep = getSuffixKeep(annotation);
int suffixKeep = (Integer) args[1]; String replacer = getReplacer(annotation);
String replacer = (String) args[2];
int length = origin.length(); int length = origin.length();
// 情况一原始字符串长度小于等于保留长度则原始字符串全部替换 // 情况一原始字符串长度小于等于保留长度则原始字符串全部替换
@ -19,26 +23,41 @@ public abstract class AbstractDesensitizationHandler<T extends Annotation> imple
return buildReplacerByLength(replacer, length); return buildReplacerByLength(replacer, length);
} }
// 情况二如果原始字符串小于等于前后缀保留字符串长度则原始字符串全部替换 // 情况二原始字符串长度小于等于前后缀保留字符串长度则原始字符串全部替换
if ((prefixKeep + suffixKeep) >= length) { if ((prefixKeep + suffixKeep) >= length) {
return buildReplacerByLength(replacer, length); return buildReplacerByLength(replacer, length);
} }
// 情况三TODO // 情况三原始字符串长度大于前后缀保留字符串长度则替换中间字符串
int interval = length - prefixKeep - suffixKeep; int interval = length - prefixKeep - suffixKeep;
return origin.substring(0, prefixKeep) + return origin.substring(0, prefixKeep) +
buildReplacerByLength(replacer, interval) + buildReplacerByLength(replacer, interval) +
origin.substring(prefixKeep + interval); origin.substring(prefixKeep + interval);
} }
// TODO @城类似子类直接获取到参数哈
/** /**
* 获取注解的参数 * 前缀保留长度
* *
* @param anno 注解信息 * @param annotation 注解信息
* @return 注解的参数 * @return 前缀保留长度
*/ */
abstract Object[] getArgs(T anno); abstract Integer getPrefixKeep(T annotation);
/**
* 后缀保留长度
*
* @param annotation 注解信息
* @return 后缀保留长度
*/
abstract Integer getSuffixKeep(T annotation);
/**
* 替换符
*
* @param annotation 注解信息
* @return 替换符
*/
abstract String getReplacer(T annotation);
/** /**
* 根据长度循环构建替换符 * 根据长度循环构建替换符
@ -48,7 +67,11 @@ public abstract class AbstractDesensitizationHandler<T extends Annotation> imple
* @return 构建后的替换符 * @return 构建后的替换符
*/ */
private String buildReplacerByLength(String replacer, int length) { private String buildReplacerByLength(String replacer, int length) {
return String.valueOf(replacer).repeat(Math.max(0, length)); StringBuilder builder = new StringBuilder();
for (int i = 0; i < length; i++) {
builder.append(replacer);
}
return builder.toString();
} }
} }

View File

@ -0,0 +1,27 @@
package cn.iocoder.yudao.framework.desensitize.core.slider.handler;
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.BankCard;
/**
* 银行卡脱敏处理器
*
* @author gaibu
*/
public class BankCardDesensitization extends AbstractDesensitizationHandler<BankCard> {
@Override
Integer getPrefixKeep(BankCard annotation) {
return annotation.prefixKeep();
}
@Override
Integer getSuffixKeep(BankCard annotation) {
return annotation.suffixKeep();
}
@Override
String getReplacer(BankCard annotation) {
return annotation.replacer();
}
}

View File

@ -0,0 +1,25 @@
package cn.iocoder.yudao.framework.desensitize.core.slider.handler;
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.CarLicense;
/**
* 车牌号脱敏处理器
*
* @author gaibu
*/
public class CarLicenseDesensitization extends AbstractDesensitizationHandler<CarLicense> {
@Override
Integer getPrefixKeep(CarLicense annotation) {
return annotation.prefixKeep();
}
@Override
Integer getSuffixKeep(CarLicense annotation) {
return annotation.suffixKeep();
}
@Override
String getReplacer(CarLicense annotation) {
return annotation.replacer();
}
}

View File

@ -0,0 +1,25 @@
package cn.iocoder.yudao.framework.desensitize.core.slider.handler;
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.ChineseName;
/**
* 中文姓名脱敏处理器
*
* @author gaibu
*/
public class ChineseNameDesensitization extends AbstractDesensitizationHandler<ChineseName> {
@Override
Integer getPrefixKeep(ChineseName annotation) {
return annotation.prefixKeep();
}
@Override
Integer getSuffixKeep(ChineseName annotation) {
return annotation.suffixKeep();
}
@Override
String getReplacer(ChineseName annotation) {
return annotation.replacer();
}
}

View File

@ -0,0 +1,25 @@
package cn.iocoder.yudao.framework.desensitize.core.slider.handler;
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.Slider;
/**
* 滑动脱敏处理器
*
* @author gaibu
*/
public class DefaultDesensitizationHandler extends AbstractDesensitizationHandler<Slider> {
@Override
Integer getPrefixKeep(Slider annotation) {
return annotation.prefixKeep();
}
@Override
Integer getSuffixKeep(Slider annotation) {
return annotation.suffixKeep();
}
@Override
String getReplacer(Slider annotation) {
return annotation.replacer();
}
}

View File

@ -0,0 +1,25 @@
package cn.iocoder.yudao.framework.desensitize.core.slider.handler;
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.FixedPhone;
/**
* 固定电话脱敏处理器
*
* @author gaibu
*/
public class FixedPhoneDesensitization extends AbstractDesensitizationHandler<FixedPhone> {
@Override
Integer getPrefixKeep(FixedPhone annotation) {
return annotation.prefixKeep();
}
@Override
Integer getSuffixKeep(FixedPhone annotation) {
return annotation.suffixKeep();
}
@Override
String getReplacer(FixedPhone annotation) {
return annotation.replacer();
}
}

View File

@ -0,0 +1,25 @@
package cn.iocoder.yudao.framework.desensitize.core.slider.handler;
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.IdCard;
/**
* 身份证脱敏处理器
*
* @author gaibu
*/
public class IdCardDesensitization extends AbstractDesensitizationHandler<IdCard> {
@Override
Integer getPrefixKeep(IdCard annotation) {
return annotation.prefixKeep();
}
@Override
Integer getSuffixKeep(IdCard annotation) {
return annotation.suffixKeep();
}
@Override
String getReplacer(IdCard annotation) {
return annotation.replacer();
}
}

View File

@ -0,0 +1,25 @@
package cn.iocoder.yudao.framework.desensitize.core.slider.handler;
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.Password;
/**
* 密码脱敏处理器
*
* @author gaibu
*/
public class PasswordDesensitization extends AbstractDesensitizationHandler<Password> {
@Override
Integer getPrefixKeep(Password annotation) {
return annotation.prefixKeep();
}
@Override
Integer getSuffixKeep(Password annotation) {
return annotation.suffixKeep();
}
@Override
String getReplacer(Password annotation) {
return annotation.replacer();
}
}

View File

@ -0,0 +1,26 @@
package cn.iocoder.yudao.framework.desensitize.core.slider.handler;
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.PhoneNumber;
/**
* 手机号脱敏处理器
*
* @author gaibu
*/
public class PhoneNumberDesensitization extends AbstractDesensitizationHandler<PhoneNumber> {
@Override
Integer getPrefixKeep(PhoneNumber annotation) {
return annotation.prefixKeep();
}
@Override
Integer getSuffixKeep(PhoneNumber annotation) {
return annotation.suffixKeep();
}
@Override
String getReplacer(PhoneNumber annotation) {
return annotation.replacer();
}
}