code review 脱敏组件的代码

This commit is contained in:
YunaiV 2023-01-19 10:25:43 +08:00
parent 42bc0d1519
commit 6593ec4214
26 changed files with 116 additions and 65 deletions

View File

@ -10,7 +10,7 @@
</parent>
<artifactId>yudao-spring-boot-starter-desensitize</artifactId>
<description>脱敏组件</description>
<description>脱敏组件:支持 JSON 返回数据时,将邮箱、手机等字段进行脱敏</description>
<dependencies>
<dependency>

View File

@ -1,5 +1,7 @@
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.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>();
// TODO @唐可以考虑使用 hutool 提供的 Singleton.get()
public static DesensitizationHandler getDesensitizationHandler(Class<? extends DesensitizationHandler> clazz) {
DesensitizationHandler handler = HANDLER_MAP.get(clazz);
if (handler != null) {
return handler;
}
// 不存在则进行创建
synchronized (DesensitizationHandlerFactory.class) {
handler = HANDLER_MAP.get(clazz);
// 双重校验锁

View File

@ -12,7 +12,7 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 顶级脱敏注解自定义注解需要使用此注解
* 顶级脱敏注解自定义注解需要使用此注解
*
* @author gaibu
*/
@ -26,5 +26,7 @@ public @interface DesensitizeBy {
/**
* 脱敏处理器
*/
@SuppressWarnings("rawtypes")
Class<? extends DesensitizationHandler> handler();
}

View File

@ -4,6 +4,8 @@ import java.lang.annotation.Annotation;
/**
* 脱敏处理器接口
*
* @author gaibu
*/
public interface DesensitizationHandler<T extends Annotation> {

View File

@ -24,8 +24,11 @@ import java.lang.reflect.Field;
/**
* 脱敏序列化器
*
* 实现 JSON 返回数据时使用 {@link DesensitizationHandler} 对声明脱敏注解的字段进行脱敏处理
*
* @author gaibu
*/
@SuppressWarnings("rawtypes")
public class StringDesensitizeSerializer extends StdSerializer<String> implements ContextualSerializer {
@Getter
@ -37,17 +40,19 @@ public class StringDesensitizeSerializer extends StdSerializer<String> implement
}
@Override
public JsonSerializer<?> createContextual(SerializerProvider serializerProvider, BeanProperty beanProperty) throws JsonMappingException {
public JsonSerializer<?> createContextual(SerializerProvider serializerProvider, BeanProperty beanProperty) {
DesensitizeBy annotation = beanProperty.getAnnotation(DesensitizeBy.class);
if (annotation == null) {
return this;
}
// 创建一个 StringDesensitizeSerializer 对象使用 DesensitizeBy 对应的处理器
StringDesensitizeSerializer serializer = new StringDesensitizeSerializer();
serializer.setDesensitizationHandler(DesensitizationHandlerFactory.getDesensitizationHandler(annotation.handler()));
return serializer;
}
@Override
@SuppressWarnings("unchecked")
public void serialize(String value, JsonGenerator gen, SerializerProvider serializerProvider) throws IOException {
if (StrUtil.isBlank(value)) {
gen.writeNull();
@ -75,12 +80,12 @@ public class StringDesensitizeSerializer extends StdSerializer<String> implement
/**
* 获取字段
*
* @param gen JsonGenerator
* @param generator JsonGenerator
* @return 字段
*/
private Field getField(JsonGenerator gen) {
String currentName = gen.getOutputContext().getCurrentName();
Object currentValue = gen.getCurrentValue();
private Field getField(JsonGenerator generator) {
String currentName = generator.getOutputContext().getCurrentName();
Object currentValue = generator.getCurrentValue();
Class<?> currentValueClass = currentValue.getClass();
return ReflectUtil.getField(currentValueClass, currentName);
}

View File

@ -0,0 +1,4 @@
/**
* 脱敏组件支持 JSON 返回数据时将邮箱手机等字段进行脱敏
*/
package cn.iocoder.yudao.framework.desensitize.core;

View File

@ -10,8 +10,9 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
// TODO @唐名字还是 Email=EmailDesensitize 合适一点避免和 Validator 的注解有点冲突
/**
* 邮箱
* 邮箱脱敏注解
*
* @author gaibu
*/
@ -28,7 +29,9 @@ public @interface Email {
String regex() default "(^.)[^@]*(@.*$)";
/**
* 替换规则邮箱;比如example@gmail.com脱敏之后为e****@gmail.com
* 替换规则邮箱;
*
* 比如example@gmail.com 脱敏之后 为e****@gmail.com
*/
String replacer() default "$1****$2";
}

View File

@ -29,6 +29,7 @@ public @interface Regex {
/**
* 替换规则会将匹配到的字符串全部替换成 replacer
*
* 例如regex=123; replacer=******
* 原始字符串 123456789
* 脱敏后字符串 ******456789

View File

@ -9,13 +9,13 @@ import java.lang.annotation.Annotation;
*
* @author gaibu
*/
public abstract class AbstractRegexDesensitizationHandler<T extends Annotation> implements DesensitizationHandler<T> {
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);
}
@ -34,4 +34,5 @@ public abstract class AbstractRegexDesensitizationHandler<T extends Annotation>
* @return 待替换的字符串
*/
abstract String getReplacer(T annotation);
}

View File

@ -3,13 +3,12 @@ package cn.iocoder.yudao.framework.desensitize.core.regex.handler;
import cn.iocoder.yudao.framework.desensitize.core.regex.annotation.Regex;
/**
* 默认正则脱敏处理器
* {@link Regex} 正则脱敏处理器
*
* @author gaibu
*/
public class DefaultRegexDesensitizationHandler extends AbstractRegexDesensitizationHandler<Regex> {
@Override
String getRegex(Regex annotation) {
return annotation.regex();

View File

@ -3,7 +3,7 @@ package cn.iocoder.yudao.framework.desensitize.core.regex.handler;
import cn.iocoder.yudao.framework.desensitize.core.regex.annotation.Email;
/**
* 邮箱脱敏处理器
* {@link Email} 脱敏处理器
*
* @author gaibu
*/
@ -18,4 +18,5 @@ public class EmailDesensitizationHandler extends AbstractRegexDesensitizationHan
String getReplacer(Email annotation) {
return annotation.replacer();
}
}

View File

@ -1,7 +1,7 @@
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.slider.handler.PhoneNumberDesensitization;
import cn.iocoder.yudao.framework.desensitize.core.slider.handler.MobileDesensitization;
import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
import java.lang.annotation.Documented;
@ -19,8 +19,8 @@ import java.lang.annotation.Target;
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotationsInside
@DesensitizeBy(handler = PhoneNumberDesensitization.class)
public @interface PhoneNumber {
@DesensitizeBy(handler = MobileDesensitization.class)
public @interface Mobile {
/**
* 前缀保留长度

View File

@ -33,7 +33,9 @@ public @interface Password {
int suffixKeep() default 0;
/**
* 替换规则密码;比如123456脱敏之后为******
* 替换规则密码;
*
* 比如123456脱敏之后为******
*/
String replacer() default "*";

View File

@ -9,7 +9,8 @@ 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
public String desensitize(String origin, T annotation) {
@ -35,6 +36,21 @@ public abstract class AbstractDesensitizationHandler<T extends Annotation> imple
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);
/**
* 根据长度循环构建替换符
*
* @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();
}
}

View File

@ -3,7 +3,7 @@ package cn.iocoder.yudao.framework.desensitize.core.slider.handler;
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.BankCard;
/**
* 银行卡脱敏处理器
* {@link BankCard} 脱敏处理器
*
* @author gaibu
*/
@ -24,4 +24,4 @@ public class BankCardDesensitization extends AbstractDesensitizationHandler<Bank
return annotation.replacer();
}
}
}

View File

@ -3,7 +3,7 @@ package cn.iocoder.yudao.framework.desensitize.core.slider.handler;
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.CarLicense;
/**
* 车牌号脱敏处理器
* {@link CarLicense} 脱敏处理器
*
* @author gaibu
*/

View File

@ -3,11 +3,12 @@ package cn.iocoder.yudao.framework.desensitize.core.slider.handler;
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.ChineseName;
/**
* 中文姓名脱敏处理器
* {@link ChineseName} 脱敏处理器
*
* @author gaibu
*/
public class ChineseNameDesensitization extends AbstractDesensitizationHandler<ChineseName> {
@Override
Integer getPrefixKeep(ChineseName annotation) {
return annotation.prefixKeep();
@ -22,4 +23,5 @@ public class ChineseNameDesensitization extends AbstractDesensitizationHandler<C
String getReplacer(ChineseName annotation) {
return annotation.replacer();
}
}

View File

@ -3,7 +3,7 @@ package cn.iocoder.yudao.framework.desensitize.core.slider.handler;
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.Slider;
/**
* 滑动脱敏处理器
* {@link Slider} 脱敏处理器
*
* @author gaibu
*/

View File

@ -3,7 +3,7 @@ package cn.iocoder.yudao.framework.desensitize.core.slider.handler;
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.FixedPhone;
/**
* 固定电话脱敏处理器
* {@link FixedPhone} 脱敏处理器
*
* @author gaibu
*/

View File

@ -3,7 +3,7 @@ package cn.iocoder.yudao.framework.desensitize.core.slider.handler;
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.IdCard;
/**
* 身份证脱敏处理器
* {@link IdCard} 脱敏处理器
*
* @author gaibu
*/

View File

@ -1,26 +1,26 @@
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
*/
public class PhoneNumberDesensitization extends AbstractDesensitizationHandler<PhoneNumber> {
public class MobileDesensitization extends AbstractDesensitizationHandler<Mobile> {
@Override
Integer getPrefixKeep(PhoneNumber annotation) {
Integer getPrefixKeep(Mobile annotation) {
return annotation.prefixKeep();
}
@Override
Integer getSuffixKeep(PhoneNumber annotation) {
Integer getSuffixKeep(Mobile annotation) {
return annotation.suffixKeep();
}
@Override
String getReplacer(PhoneNumber annotation) {
String getReplacer(Mobile annotation) {
return annotation.replacer();
}
}

View File

@ -3,7 +3,7 @@ package cn.iocoder.yudao.framework.desensitize.core.slider.handler;
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.Password;
/**
* 码脱敏处理器
* {@link Password} 码脱敏处理器
*
* @author gaibu
*/

View File

@ -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.desensitize.core.regex.annotation.Email;
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.CarLicense;
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.IdCard;
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.test.core.ut.BaseMockitoUnitTest;
import lombok.Data;
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 {
@Test
public void test() {
// 准备参数
DesensitizeDemo desensitizeDemo = new DesensitizeDemo();
desensitizeDemo.setUserName("芋道源码");
desensitizeDemo.setNickname("芋道源码");
desensitizeDemo.setBankCard("9988002866797031");
desensitizeDemo.setCarLicense("粤A66666");
desensitizeDemo.setFixedPhone("01086551122");
@ -33,13 +37,16 @@ public class DesensitizeTest extends BaseMockitoUnitTest {
desensitizeDemo.setSlider1("ABCDEFG");
desensitizeDemo.setSlider2("ABCDEFG");
desensitizeDemo.setSlider3("ABCDEFG");
desensitizeDemo.setEmail("1@eamil.com");
desensitizeDemo.setEmail("1@email.com");
desensitizeDemo.setRegex("你好,我是芋道源码");
desensitizeDemo.setAddress("北京市海淀区上地十街10号");
desensitizeDemo.setOrigin("芋道源码");
// 调用
DesensitizeDemo d = JsonUtils.parseObject(JsonUtils.toJsonString(desensitizeDemo), DesensitizeDemo.class);
assertEquals("芋***", d.getUserName());
// 断言
assertNotNull(d);
assertEquals("芋***", d.getNickname());
assertEquals("998800********31", d.getBankCard());
assertEquals("粤A6***6", d.getCarLicense());
assertEquals("0108*****22", d.getFixedPhone());
@ -49,7 +56,7 @@ public class DesensitizeTest extends BaseMockitoUnitTest {
assertEquals("#######", d.getSlider1());
assertEquals("ABC*EFG", d.getSlider2());
assertEquals("*******", d.getSlider3());
assertEquals("1****@eamil.com", d.getEmail());
assertEquals("1****@email.com", d.getEmail());
assertEquals("你好,我是*", d.getRegex());
assertEquals("北京市海淀区上地十街10号*", d.getAddress());
assertEquals("芋道源码", d.getOrigin());
@ -57,8 +64,9 @@ public class DesensitizeTest extends BaseMockitoUnitTest {
@Data
public static class DesensitizeDemo {
@ChineseName
private String userName;
private String nickname;
@BankCard
private String bankCard;
@CarLicense
@ -69,20 +77,22 @@ public class DesensitizeTest extends BaseMockitoUnitTest {
private String idCard;
@Password
private String password;
@PhoneNumber
@Mobile
private String phoneNumber;
@Slider(prefixKeep = 6,suffixKeep = 1,replacer = "#")
@Slider(prefixKeep = 6, suffixKeep = 1, replacer = "#")
private String slider1;
@Slider(prefixKeep = 3,suffixKeep = 3)
@Slider(prefixKeep = 3, suffixKeep = 3)
private String slider2;
@Slider(prefixKeep = 10)
private String slider3;
@Email
private String email;
@Regex(regex = "芋道源码",replacer = "*")
@Regex(regex = "芋道源码", replacer = "*")
private String regex;
@Address
private String address;
private String origin;
}
}

View File

@ -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.slider.handler.AddressHandler;
import cn.iocoder.yudao.framework.desensitize.core.handler.AddressHandler;
import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
import java.lang.annotation.Documented;
@ -13,6 +14,8 @@ import java.lang.annotation.Target;
/**
* 地址
*
* 用于 {@link DesensitizeTest} 测试使用
*
* @author gaibu
*/
@Documented

View File

@ -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.slider.annotation.Address;
import cn.iocoder.yudao.framework.desensitize.core.annotation.Address;
/**
* {@link Address} 的脱敏处理器
*
* 用于 {@link DesensitizeTest} 测试使用
*/
public class AddressHandler implements DesensitizationHandler<Address> {
@Override
public String desensitize(String origin, Address annotation) {
return origin + annotation.replacer();
}
}

View File

@ -1 +1,4 @@
/**
* Web 框架全局异常API 日志等
*/
package cn.iocoder.yudao.framework;