mirror of
https://gitee.com/huangge1199_admin/vue-pro.git
synced 2024-11-26 09:11:52 +08:00
code review 脱敏组件的代码
This commit is contained in:
parent
42bc0d1519
commit
6593ec4214
@ -10,7 +10,7 @@
|
|||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<artifactId>yudao-spring-boot-starter-desensitize</artifactId>
|
<artifactId>yudao-spring-boot-starter-desensitize</artifactId>
|
||||||
<description>脱敏组件</description>
|
<description>脱敏组件:支持 JSON 返回数据时,将邮箱、手机等字段进行脱敏</description>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package cn.iocoder.yudao.framework.desensitize.core.base;
|
package cn.iocoder.yudao.framework.desensitize.core.base;
|
||||||
|
|
||||||
|
import cn.hutool.Hutool;
|
||||||
|
import cn.hutool.core.lang.Singleton;
|
||||||
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;
|
||||||
|
|
||||||
@ -18,11 +20,13 @@ public class DesensitizationHandlerFactory {
|
|||||||
*/
|
*/
|
||||||
private static final Map<Class<? extends DesensitizationHandler>, DesensitizationHandler> HANDLER_MAP = new ConcurrentHashMap<Class<? extends DesensitizationHandler>, DesensitizationHandler>();
|
private static final Map<Class<? extends DesensitizationHandler>, DesensitizationHandler> HANDLER_MAP = new ConcurrentHashMap<Class<? extends DesensitizationHandler>, DesensitizationHandler>();
|
||||||
|
|
||||||
|
// TODO @唐:可以考虑,使用 hutool 提供的 Singleton.get()
|
||||||
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 (DesensitizationHandlerFactory.class) {
|
synchronized (DesensitizationHandlerFactory.class) {
|
||||||
handler = HANDLER_MAP.get(clazz);
|
handler = HANDLER_MAP.get(clazz);
|
||||||
// 双重校验锁
|
// 双重校验锁
|
||||||
|
@ -12,7 +12,7 @@ import java.lang.annotation.RetentionPolicy;
|
|||||||
import java.lang.annotation.Target;
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 顶级脱敏注解,自定义注解需要使用此注解。
|
* 顶级脱敏注解,自定义注解需要使用此注解
|
||||||
*
|
*
|
||||||
* @author gaibu
|
* @author gaibu
|
||||||
*/
|
*/
|
||||||
@ -26,5 +26,7 @@ public @interface DesensitizeBy {
|
|||||||
/**
|
/**
|
||||||
* 脱敏处理器
|
* 脱敏处理器
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings("rawtypes")
|
||||||
Class<? extends DesensitizationHandler> handler();
|
Class<? extends DesensitizationHandler> handler();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,8 @@ import java.lang.annotation.Annotation;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 脱敏处理器接口
|
* 脱敏处理器接口
|
||||||
|
*
|
||||||
|
* @author gaibu
|
||||||
*/
|
*/
|
||||||
public interface DesensitizationHandler<T extends Annotation> {
|
public interface DesensitizationHandler<T extends Annotation> {
|
||||||
|
|
||||||
|
@ -24,8 +24,11 @@ import java.lang.reflect.Field;
|
|||||||
/**
|
/**
|
||||||
* 脱敏序列化器
|
* 脱敏序列化器
|
||||||
*
|
*
|
||||||
|
* 实现 JSON 返回数据时,使用 {@link DesensitizationHandler} 对声明脱敏注解的字段,进行脱敏处理。
|
||||||
|
*
|
||||||
* @author gaibu
|
* @author gaibu
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings("rawtypes")
|
||||||
public class StringDesensitizeSerializer extends StdSerializer<String> implements ContextualSerializer {
|
public class StringDesensitizeSerializer extends StdSerializer<String> implements ContextualSerializer {
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@ -37,17 +40,19 @@ public class StringDesensitizeSerializer extends StdSerializer<String> implement
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public JsonSerializer<?> createContextual(SerializerProvider serializerProvider, BeanProperty beanProperty) throws JsonMappingException {
|
public JsonSerializer<?> createContextual(SerializerProvider serializerProvider, BeanProperty beanProperty) {
|
||||||
DesensitizeBy annotation = beanProperty.getAnnotation(DesensitizeBy.class);
|
DesensitizeBy annotation = beanProperty.getAnnotation(DesensitizeBy.class);
|
||||||
if (annotation == null) {
|
if (annotation == null) {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
// 创建一个 StringDesensitizeSerializer 对象,使用 DesensitizeBy 对应的处理器
|
||||||
StringDesensitizeSerializer serializer = new StringDesensitizeSerializer();
|
StringDesensitizeSerializer serializer = new StringDesensitizeSerializer();
|
||||||
serializer.setDesensitizationHandler(DesensitizationHandlerFactory.getDesensitizationHandler(annotation.handler()));
|
serializer.setDesensitizationHandler(DesensitizationHandlerFactory.getDesensitizationHandler(annotation.handler()));
|
||||||
return serializer;
|
return serializer;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
public void serialize(String value, JsonGenerator gen, SerializerProvider serializerProvider) throws IOException {
|
public void serialize(String value, JsonGenerator gen, SerializerProvider serializerProvider) throws IOException {
|
||||||
if (StrUtil.isBlank(value)) {
|
if (StrUtil.isBlank(value)) {
|
||||||
gen.writeNull();
|
gen.writeNull();
|
||||||
@ -75,12 +80,12 @@ public class StringDesensitizeSerializer extends StdSerializer<String> implement
|
|||||||
/**
|
/**
|
||||||
* 获取字段
|
* 获取字段
|
||||||
*
|
*
|
||||||
* @param gen JsonGenerator
|
* @param generator JsonGenerator
|
||||||
* @return 字段
|
* @return 字段
|
||||||
*/
|
*/
|
||||||
private Field getField(JsonGenerator gen) {
|
private Field getField(JsonGenerator generator) {
|
||||||
String currentName = gen.getOutputContext().getCurrentName();
|
String currentName = generator.getOutputContext().getCurrentName();
|
||||||
Object currentValue = gen.getCurrentValue();
|
Object currentValue = generator.getCurrentValue();
|
||||||
Class<?> currentValueClass = currentValue.getClass();
|
Class<?> currentValueClass = currentValue.getClass();
|
||||||
return ReflectUtil.getField(currentValueClass, currentName);
|
return ReflectUtil.getField(currentValueClass, currentName);
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,4 @@
|
|||||||
|
/**
|
||||||
|
* 脱敏组件:支持 JSON 返回数据时,将邮箱、手机等字段进行脱敏
|
||||||
|
*/
|
||||||
|
package cn.iocoder.yudao.framework.desensitize.core;
|
@ -10,8 +10,9 @@ 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 @唐:名字还是 Email=》EmailDesensitize 合适一点,避免和 Validator 的注解有点冲突
|
||||||
/**
|
/**
|
||||||
* 邮箱
|
* 邮箱脱敏注解
|
||||||
*
|
*
|
||||||
* @author gaibu
|
* @author gaibu
|
||||||
*/
|
*/
|
||||||
@ -28,7 +29,9 @@ public @interface Email {
|
|||||||
String regex() default "(^.)[^@]*(@.*$)";
|
String regex() default "(^.)[^@]*(@.*$)";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 替换规则,邮箱;比如:example@gmail.com脱敏之后为e****@gmail.com
|
* 替换规则,邮箱;
|
||||||
|
*
|
||||||
|
* 比如:example@gmail.com 脱敏之后 为e****@gmail.com
|
||||||
*/
|
*/
|
||||||
String replacer() default "$1****$2";
|
String replacer() default "$1****$2";
|
||||||
}
|
}
|
||||||
|
@ -29,6 +29,7 @@ public @interface Regex {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 替换规则,会将匹配到的字符串全部替换成 replacer
|
* 替换规则,会将匹配到的字符串全部替换成 replacer
|
||||||
|
*
|
||||||
* 例如:regex=123; replacer=******
|
* 例如:regex=123; replacer=******
|
||||||
* 原始字符串 123456789
|
* 原始字符串 123456789
|
||||||
* 脱敏后字符串 ******456789
|
* 脱敏后字符串 ******456789
|
||||||
|
@ -9,13 +9,13 @@ import java.lang.annotation.Annotation;
|
|||||||
*
|
*
|
||||||
* @author gaibu
|
* @author gaibu
|
||||||
*/
|
*/
|
||||||
public abstract class AbstractRegexDesensitizationHandler<T extends Annotation> implements DesensitizationHandler<T> {
|
public abstract class AbstractRegexDesensitizationHandler<T extends Annotation>
|
||||||
|
implements DesensitizationHandler<T> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String desensitize(String origin, T annotation) {
|
public String desensitize(String origin, T annotation) {
|
||||||
String regex = getRegex(annotation);
|
String regex = getRegex(annotation);
|
||||||
String replacer = getReplacer(annotation);
|
String replacer = getReplacer(annotation);
|
||||||
|
|
||||||
return origin.replaceAll(regex, replacer);
|
return origin.replaceAll(regex, replacer);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -34,4 +34,5 @@ public abstract class AbstractRegexDesensitizationHandler<T extends Annotation>
|
|||||||
* @return 待替换的字符串
|
* @return 待替换的字符串
|
||||||
*/
|
*/
|
||||||
abstract String getReplacer(T annotation);
|
abstract String getReplacer(T annotation);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -3,13 +3,12 @@ package cn.iocoder.yudao.framework.desensitize.core.regex.handler;
|
|||||||
import cn.iocoder.yudao.framework.desensitize.core.regex.annotation.Regex;
|
import cn.iocoder.yudao.framework.desensitize.core.regex.annotation.Regex;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 默认正则脱敏处理器
|
* {@link Regex} 的正则脱敏处理器
|
||||||
*
|
*
|
||||||
* @author gaibu
|
* @author gaibu
|
||||||
*/
|
*/
|
||||||
public class DefaultRegexDesensitizationHandler extends AbstractRegexDesensitizationHandler<Regex> {
|
public class DefaultRegexDesensitizationHandler extends AbstractRegexDesensitizationHandler<Regex> {
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
String getRegex(Regex annotation) {
|
String getRegex(Regex annotation) {
|
||||||
return annotation.regex();
|
return annotation.regex();
|
||||||
|
@ -3,7 +3,7 @@ package cn.iocoder.yudao.framework.desensitize.core.regex.handler;
|
|||||||
import cn.iocoder.yudao.framework.desensitize.core.regex.annotation.Email;
|
import cn.iocoder.yudao.framework.desensitize.core.regex.annotation.Email;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 邮箱脱敏处理器
|
* {@link Email} 的脱敏处理器
|
||||||
*
|
*
|
||||||
* @author gaibu
|
* @author gaibu
|
||||||
*/
|
*/
|
||||||
@ -18,4 +18,5 @@ public class EmailDesensitizationHandler extends AbstractRegexDesensitizationHan
|
|||||||
String getReplacer(Email annotation) {
|
String getReplacer(Email annotation) {
|
||||||
return annotation.replacer();
|
return annotation.replacer();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
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.DesensitizeBy;
|
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.MobileDesensitization;
|
||||||
import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
|
import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
|
||||||
|
|
||||||
import java.lang.annotation.Documented;
|
import java.lang.annotation.Documented;
|
||||||
@ -19,8 +19,8 @@ import java.lang.annotation.Target;
|
|||||||
@Target({ElementType.FIELD})
|
@Target({ElementType.FIELD})
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
@JacksonAnnotationsInside
|
@JacksonAnnotationsInside
|
||||||
@DesensitizeBy(handler = PhoneNumberDesensitization.class)
|
@DesensitizeBy(handler = MobileDesensitization.class)
|
||||||
public @interface PhoneNumber {
|
public @interface Mobile {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 前缀保留长度
|
* 前缀保留长度
|
@ -33,7 +33,9 @@ public @interface Password {
|
|||||||
int suffixKeep() default 0;
|
int suffixKeep() default 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 替换规则,密码;比如:123456脱敏之后为******
|
* 替换规则,密码;
|
||||||
|
*
|
||||||
|
* 比如:123456脱敏之后为******
|
||||||
*/
|
*/
|
||||||
String replacer() default "*";
|
String replacer() default "*";
|
||||||
|
|
||||||
|
@ -9,7 +9,8 @@ import java.lang.annotation.Annotation;
|
|||||||
*
|
*
|
||||||
* @author gaibu
|
* @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 annotation) {
|
public String desensitize(String origin, T annotation) {
|
||||||
@ -35,6 +36,21 @@ public abstract class AbstractDesensitizationHandler<T extends Annotation> imple
|
|||||||
origin.substring(prefixKeep + interval);
|
origin.substring(prefixKeep + interval);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据长度循环构建替换符
|
||||||
|
*
|
||||||
|
* @param replacer 替换符
|
||||||
|
* @param length 长度
|
||||||
|
* @return 构建后的替换符
|
||||||
|
*/
|
||||||
|
private String buildReplacerByLength(String replacer, int length) {
|
||||||
|
StringBuilder builder = new StringBuilder();
|
||||||
|
for (int i = 0; i < length; i++) {
|
||||||
|
builder.append(replacer);
|
||||||
|
}
|
||||||
|
return builder.toString();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 前缀保留长度
|
* 前缀保留长度
|
||||||
*
|
*
|
||||||
@ -59,19 +75,4 @@ public abstract class AbstractDesensitizationHandler<T extends Annotation> imple
|
|||||||
*/
|
*/
|
||||||
abstract String getReplacer(T annotation);
|
abstract String getReplacer(T annotation);
|
||||||
|
|
||||||
/**
|
|
||||||
* 根据长度循环构建替换符
|
|
||||||
*
|
|
||||||
* @param replacer 替换符
|
|
||||||
* @param length 长度
|
|
||||||
* @return 构建后的替换符
|
|
||||||
*/
|
|
||||||
private String buildReplacerByLength(String replacer, int length) {
|
|
||||||
StringBuilder builder = new StringBuilder();
|
|
||||||
for (int i = 0; i < length; i++) {
|
|
||||||
builder.append(replacer);
|
|
||||||
}
|
|
||||||
return builder.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,7 @@ package cn.iocoder.yudao.framework.desensitize.core.slider.handler;
|
|||||||
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.BankCard;
|
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.BankCard;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 银行卡脱敏处理器
|
* {@link BankCard} 的脱敏处理器
|
||||||
*
|
*
|
||||||
* @author gaibu
|
* @author gaibu
|
||||||
*/
|
*/
|
||||||
@ -24,4 +24,4 @@ public class BankCardDesensitization extends AbstractDesensitizationHandler<Bank
|
|||||||
return annotation.replacer();
|
return annotation.replacer();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,7 @@ package cn.iocoder.yudao.framework.desensitize.core.slider.handler;
|
|||||||
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.CarLicense;
|
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.CarLicense;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 车牌号脱敏处理器
|
* {@link CarLicense} 的脱敏处理器
|
||||||
*
|
*
|
||||||
* @author gaibu
|
* @author gaibu
|
||||||
*/
|
*/
|
||||||
|
@ -3,11 +3,12 @@ package cn.iocoder.yudao.framework.desensitize.core.slider.handler;
|
|||||||
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.ChineseName;
|
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.ChineseName;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 中文姓名脱敏处理器
|
* {@link ChineseName} 的脱敏处理器
|
||||||
*
|
*
|
||||||
* @author gaibu
|
* @author gaibu
|
||||||
*/
|
*/
|
||||||
public class ChineseNameDesensitization extends AbstractDesensitizationHandler<ChineseName> {
|
public class ChineseNameDesensitization extends AbstractDesensitizationHandler<ChineseName> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
Integer getPrefixKeep(ChineseName annotation) {
|
Integer getPrefixKeep(ChineseName annotation) {
|
||||||
return annotation.prefixKeep();
|
return annotation.prefixKeep();
|
||||||
@ -22,4 +23,5 @@ public class ChineseNameDesensitization extends AbstractDesensitizationHandler<C
|
|||||||
String getReplacer(ChineseName annotation) {
|
String getReplacer(ChineseName annotation) {
|
||||||
return annotation.replacer();
|
return annotation.replacer();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,7 @@ package cn.iocoder.yudao.framework.desensitize.core.slider.handler;
|
|||||||
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.Slider;
|
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.Slider;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 滑动脱敏处理器
|
* {@link Slider} 的脱敏处理器
|
||||||
*
|
*
|
||||||
* @author gaibu
|
* @author gaibu
|
||||||
*/
|
*/
|
||||||
|
@ -3,7 +3,7 @@ package cn.iocoder.yudao.framework.desensitize.core.slider.handler;
|
|||||||
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.FixedPhone;
|
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.FixedPhone;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 固定电话脱敏处理器
|
* {@link FixedPhone} 的脱敏处理器
|
||||||
*
|
*
|
||||||
* @author gaibu
|
* @author gaibu
|
||||||
*/
|
*/
|
||||||
|
@ -3,7 +3,7 @@ package cn.iocoder.yudao.framework.desensitize.core.slider.handler;
|
|||||||
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.IdCard;
|
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.IdCard;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 身份证脱敏处理器
|
* {@link IdCard} 的脱敏处理器
|
||||||
*
|
*
|
||||||
* @author gaibu
|
* @author gaibu
|
||||||
*/
|
*/
|
||||||
|
@ -1,26 +1,26 @@
|
|||||||
package cn.iocoder.yudao.framework.desensitize.core.slider.handler;
|
package cn.iocoder.yudao.framework.desensitize.core.slider.handler;
|
||||||
|
|
||||||
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.PhoneNumber;
|
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.Mobile;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 手机号脱敏处理器
|
* {@link Mobile} 的脱敏处理器
|
||||||
*
|
*
|
||||||
* @author gaibu
|
* @author gaibu
|
||||||
*/
|
*/
|
||||||
public class PhoneNumberDesensitization extends AbstractDesensitizationHandler<PhoneNumber> {
|
public class MobileDesensitization extends AbstractDesensitizationHandler<Mobile> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
Integer getPrefixKeep(PhoneNumber annotation) {
|
Integer getPrefixKeep(Mobile annotation) {
|
||||||
return annotation.prefixKeep();
|
return annotation.prefixKeep();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
Integer getSuffixKeep(PhoneNumber annotation) {
|
Integer getSuffixKeep(Mobile annotation) {
|
||||||
return annotation.suffixKeep();
|
return annotation.suffixKeep();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
String getReplacer(PhoneNumber annotation) {
|
String getReplacer(Mobile annotation) {
|
||||||
return annotation.replacer();
|
return annotation.replacer();
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -3,7 +3,7 @@ package cn.iocoder.yudao.framework.desensitize.core.slider.handler;
|
|||||||
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.Password;
|
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.Password;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 密码脱敏处理器
|
* {@link Password} 的码脱敏处理器
|
||||||
*
|
*
|
||||||
* @author gaibu
|
* @author gaibu
|
||||||
*/
|
*/
|
||||||
|
@ -1,29 +1,33 @@
|
|||||||
package cn.iocoder.yudao.framework.desensitize.core.slider;
|
package cn.iocoder.yudao.framework.desensitize.core;
|
||||||
|
|
||||||
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
|
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
|
||||||
import cn.iocoder.yudao.framework.desensitize.core.regex.annotation.Email;
|
import cn.iocoder.yudao.framework.desensitize.core.regex.annotation.Email;
|
||||||
import cn.iocoder.yudao.framework.desensitize.core.regex.annotation.Regex;
|
import cn.iocoder.yudao.framework.desensitize.core.regex.annotation.Regex;
|
||||||
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.Address;
|
import cn.iocoder.yudao.framework.desensitize.core.annotation.Address;
|
||||||
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.BankCard;
|
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.BankCard;
|
||||||
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.CarLicense;
|
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.CarLicense;
|
||||||
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.ChineseName;
|
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.ChineseName;
|
||||||
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.FixedPhone;
|
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.FixedPhone;
|
||||||
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.IdCard;
|
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.IdCard;
|
||||||
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.Password;
|
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.Password;
|
||||||
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.PhoneNumber;
|
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.Mobile;
|
||||||
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.Slider;
|
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.Slider;
|
||||||
import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest;
|
import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@link DesensitizeTest} 的单元测试
|
||||||
|
*/
|
||||||
public class DesensitizeTest extends BaseMockitoUnitTest {
|
public class DesensitizeTest extends BaseMockitoUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void test() {
|
public void test() {
|
||||||
|
// 准备参数
|
||||||
DesensitizeDemo desensitizeDemo = new DesensitizeDemo();
|
DesensitizeDemo desensitizeDemo = new DesensitizeDemo();
|
||||||
desensitizeDemo.setUserName("芋道源码");
|
desensitizeDemo.setNickname("芋道源码");
|
||||||
desensitizeDemo.setBankCard("9988002866797031");
|
desensitizeDemo.setBankCard("9988002866797031");
|
||||||
desensitizeDemo.setCarLicense("粤A66666");
|
desensitizeDemo.setCarLicense("粤A66666");
|
||||||
desensitizeDemo.setFixedPhone("01086551122");
|
desensitizeDemo.setFixedPhone("01086551122");
|
||||||
@ -33,13 +37,16 @@ public class DesensitizeTest extends BaseMockitoUnitTest {
|
|||||||
desensitizeDemo.setSlider1("ABCDEFG");
|
desensitizeDemo.setSlider1("ABCDEFG");
|
||||||
desensitizeDemo.setSlider2("ABCDEFG");
|
desensitizeDemo.setSlider2("ABCDEFG");
|
||||||
desensitizeDemo.setSlider3("ABCDEFG");
|
desensitizeDemo.setSlider3("ABCDEFG");
|
||||||
desensitizeDemo.setEmail("1@eamil.com");
|
desensitizeDemo.setEmail("1@email.com");
|
||||||
desensitizeDemo.setRegex("你好,我是芋道源码");
|
desensitizeDemo.setRegex("你好,我是芋道源码");
|
||||||
desensitizeDemo.setAddress("北京市海淀区上地十街10号");
|
desensitizeDemo.setAddress("北京市海淀区上地十街10号");
|
||||||
desensitizeDemo.setOrigin("芋道源码");
|
desensitizeDemo.setOrigin("芋道源码");
|
||||||
|
|
||||||
|
// 调用
|
||||||
DesensitizeDemo d = JsonUtils.parseObject(JsonUtils.toJsonString(desensitizeDemo), DesensitizeDemo.class);
|
DesensitizeDemo d = JsonUtils.parseObject(JsonUtils.toJsonString(desensitizeDemo), DesensitizeDemo.class);
|
||||||
assertEquals("芋***", d.getUserName());
|
// 断言
|
||||||
|
assertNotNull(d);
|
||||||
|
assertEquals("芋***", d.getNickname());
|
||||||
assertEquals("998800********31", d.getBankCard());
|
assertEquals("998800********31", d.getBankCard());
|
||||||
assertEquals("粤A6***6", d.getCarLicense());
|
assertEquals("粤A6***6", d.getCarLicense());
|
||||||
assertEquals("0108*****22", d.getFixedPhone());
|
assertEquals("0108*****22", d.getFixedPhone());
|
||||||
@ -49,7 +56,7 @@ public class DesensitizeTest extends BaseMockitoUnitTest {
|
|||||||
assertEquals("#######", d.getSlider1());
|
assertEquals("#######", d.getSlider1());
|
||||||
assertEquals("ABC*EFG", d.getSlider2());
|
assertEquals("ABC*EFG", d.getSlider2());
|
||||||
assertEquals("*******", d.getSlider3());
|
assertEquals("*******", d.getSlider3());
|
||||||
assertEquals("1****@eamil.com", d.getEmail());
|
assertEquals("1****@email.com", d.getEmail());
|
||||||
assertEquals("你好,我是*", d.getRegex());
|
assertEquals("你好,我是*", d.getRegex());
|
||||||
assertEquals("北京市海淀区上地十街10号*", d.getAddress());
|
assertEquals("北京市海淀区上地十街10号*", d.getAddress());
|
||||||
assertEquals("芋道源码", d.getOrigin());
|
assertEquals("芋道源码", d.getOrigin());
|
||||||
@ -57,8 +64,9 @@ public class DesensitizeTest extends BaseMockitoUnitTest {
|
|||||||
|
|
||||||
@Data
|
@Data
|
||||||
public static class DesensitizeDemo {
|
public static class DesensitizeDemo {
|
||||||
|
|
||||||
@ChineseName
|
@ChineseName
|
||||||
private String userName;
|
private String nickname;
|
||||||
@BankCard
|
@BankCard
|
||||||
private String bankCard;
|
private String bankCard;
|
||||||
@CarLicense
|
@CarLicense
|
||||||
@ -69,20 +77,22 @@ public class DesensitizeTest extends BaseMockitoUnitTest {
|
|||||||
private String idCard;
|
private String idCard;
|
||||||
@Password
|
@Password
|
||||||
private String password;
|
private String password;
|
||||||
@PhoneNumber
|
@Mobile
|
||||||
private String phoneNumber;
|
private String phoneNumber;
|
||||||
@Slider(prefixKeep = 6,suffixKeep = 1,replacer = "#")
|
@Slider(prefixKeep = 6, suffixKeep = 1, replacer = "#")
|
||||||
private String slider1;
|
private String slider1;
|
||||||
@Slider(prefixKeep = 3,suffixKeep = 3)
|
@Slider(prefixKeep = 3, suffixKeep = 3)
|
||||||
private String slider2;
|
private String slider2;
|
||||||
@Slider(prefixKeep = 10)
|
@Slider(prefixKeep = 10)
|
||||||
private String slider3;
|
private String slider3;
|
||||||
@Email
|
@Email
|
||||||
private String email;
|
private String email;
|
||||||
@Regex(regex = "芋道源码",replacer = "*")
|
@Regex(regex = "芋道源码", replacer = "*")
|
||||||
private String regex;
|
private String regex;
|
||||||
@Address
|
@Address
|
||||||
private String address;
|
private String address;
|
||||||
private String origin;
|
private String origin;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -1,7 +1,8 @@
|
|||||||
package cn.iocoder.yudao.framework.desensitize.core.slider.annotation;
|
package cn.iocoder.yudao.framework.desensitize.core.annotation;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.framework.desensitize.core.DesensitizeTest;
|
||||||
import cn.iocoder.yudao.framework.desensitize.core.base.annotation.DesensitizeBy;
|
import cn.iocoder.yudao.framework.desensitize.core.base.annotation.DesensitizeBy;
|
||||||
import cn.iocoder.yudao.framework.desensitize.core.slider.handler.AddressHandler;
|
import cn.iocoder.yudao.framework.desensitize.core.handler.AddressHandler;
|
||||||
import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
|
import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
|
||||||
|
|
||||||
import java.lang.annotation.Documented;
|
import java.lang.annotation.Documented;
|
||||||
@ -13,6 +14,8 @@ import java.lang.annotation.Target;
|
|||||||
/**
|
/**
|
||||||
* 地址
|
* 地址
|
||||||
*
|
*
|
||||||
|
* 用于 {@link DesensitizeTest} 测试使用
|
||||||
|
*
|
||||||
* @author gaibu
|
* @author gaibu
|
||||||
*/
|
*/
|
||||||
@Documented
|
@Documented
|
@ -1,11 +1,19 @@
|
|||||||
package cn.iocoder.yudao.framework.desensitize.core.slider.handler;
|
package cn.iocoder.yudao.framework.desensitize.core.handler;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.framework.desensitize.core.DesensitizeTest;
|
||||||
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.slider.annotation.Address;
|
import cn.iocoder.yudao.framework.desensitize.core.annotation.Address;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@link Address} 的脱敏处理器
|
||||||
|
*
|
||||||
|
* 用于 {@link DesensitizeTest} 测试使用
|
||||||
|
*/
|
||||||
public class AddressHandler implements DesensitizationHandler<Address> {
|
public class AddressHandler implements DesensitizationHandler<Address> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String desensitize(String origin, Address annotation) {
|
public String desensitize(String origin, Address annotation) {
|
||||||
return origin + annotation.replacer();
|
return origin + annotation.replacer();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -1 +1,4 @@
|
|||||||
|
/**
|
||||||
|
* Web 框架,全局异常、API 日志等
|
||||||
|
*/
|
||||||
package cn.iocoder.yudao.framework;
|
package cn.iocoder.yudao.framework;
|
||||||
|
Loading…
Reference in New Issue
Block a user