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

This commit is contained in:
gaibu 2023-01-19 10:35:54 +08:00
parent 6593ec4214
commit 2aa208f441
5 changed files with 10 additions and 54 deletions

View File

@ -1,42 +0,0 @@
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;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* 脱敏处理器 Holder
*
* @author gaibu
*/
public class DesensitizationHandlerFactory {
/**
* handler 缓存默认初始化内置的处理器
*/
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);
// 双重校验锁
if (handler != null) {
return handler;
}
handler = ReflectUtil.newInstanceIfPossible(clazz);
HANDLER_MAP.put(clazz, handler);
}
return handler;
}
}

View File

@ -1,15 +1,14 @@
package cn.iocoder.yudao.framework.desensitize.core.base.serializer;
import cn.hutool.core.annotation.AnnotationUtil;
import cn.hutool.core.lang.Singleton;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
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.DesensitizationHandlerFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.ContextualSerializer;
@ -47,7 +46,7 @@ public class StringDesensitizeSerializer extends StdSerializer<String> implement
}
// 创建一个 StringDesensitizeSerializer 对象使用 DesensitizeBy 对应的处理器
StringDesensitizeSerializer serializer = new StringDesensitizeSerializer();
serializer.setDesensitizationHandler(DesensitizationHandlerFactory.getDesensitizationHandler(annotation.handler()));
serializer.setDesensitizationHandler(Singleton.get(annotation.handler()));
return serializer;
}

View File

@ -10,7 +10,6 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
// TODO @唐名字还是 Email=EmailDesensitize 合适一点避免和 Validator 的注解有点冲突
/**
* 邮箱脱敏注解
*
@ -21,7 +20,7 @@ import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotationsInside
@DesensitizeBy(handler = EmailDesensitizationHandler.class)
public @interface Email {
public @interface EmailDesensitize {
/**
* 匹配的正则表达式

View File

@ -1,21 +1,21 @@
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.EmailDesensitize;
/**
* {@link Email} 的脱敏处理器
* {@link EmailDesensitize} 的脱敏处理器
*
* @author gaibu
*/
public class EmailDesensitizationHandler extends AbstractRegexDesensitizationHandler<Email> {
public class EmailDesensitizationHandler extends AbstractRegexDesensitizationHandler<EmailDesensitize> {
@Override
String getRegex(Email annotation) {
String getRegex(EmailDesensitize annotation) {
return annotation.regex();
}
@Override
String getReplacer(Email annotation) {
String getReplacer(EmailDesensitize annotation) {
return annotation.replacer();
}

View File

@ -1,7 +1,7 @@
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.EmailDesensitize;
import cn.iocoder.yudao.framework.desensitize.core.regex.annotation.Regex;
import cn.iocoder.yudao.framework.desensitize.core.annotation.Address;
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.BankCard;
@ -85,7 +85,7 @@ public class DesensitizeTest extends BaseMockitoUnitTest {
private String slider2;
@Slider(prefixKeep = 10)
private String slider3;
@Email
@EmailDesensitize
private String email;
@Regex(regex = "芋道源码", replacer = "*")
private String regex;