From 42bc0d1519783e877c3524c5296c31fab61b63a0 Mon Sep 17 00:00:00 2001 From: gaibu <1016771049@qq.com> Date: Tue, 17 Jan 2023 11:28:33 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=84=B1=E6=95=8F=E7=BB=84=E4=BB=B6?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../{EmailDesensitize.java => Email.java} | 2 +- .../{RegexDesensitize.java => Regex.java} | 2 +- .../DefaultRegexDesensitizationHandler.java | 8 +- .../handler/EmailDesensitizationHandler.java | 8 +- .../core/slider/DesensitizeTest.java | 88 +++++++++++++++++++ .../core/slider/annotation/Address.java | 27 ++++++ .../core/slider/handler/AddressHandler.java | 11 +++ 7 files changed, 136 insertions(+), 10 deletions(-) rename yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/{EmailDesensitize.java => Email.java} (96%) rename yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/{RegexDesensitize.java => Regex.java} (96%) create mode 100644 yudao-framework/yudao-spring-boot-starter-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/core/slider/DesensitizeTest.java create mode 100644 yudao-framework/yudao-spring-boot-starter-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/Address.java create mode 100644 yudao-framework/yudao-spring-boot-starter-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/AddressHandler.java diff --git a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/EmailDesensitize.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/Email.java similarity index 96% rename from yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/EmailDesensitize.java rename to yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/Email.java index 303320b70..02d7043fe 100644 --- a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/EmailDesensitize.java +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/Email.java @@ -20,7 +20,7 @@ import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @JacksonAnnotationsInside @DesensitizeBy(handler = EmailDesensitizationHandler.class) -public @interface EmailDesensitize { +public @interface Email { /** * 匹配的正则表达式 diff --git a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/RegexDesensitize.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/Regex.java similarity index 96% rename from yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/RegexDesensitize.java rename to yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/Regex.java index 6c457fd0c..e5d2558fa 100644 --- a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/RegexDesensitize.java +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/annotation/Regex.java @@ -20,7 +20,7 @@ import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @JacksonAnnotationsInside @DesensitizeBy(handler = DefaultRegexDesensitizationHandler.class) -public @interface RegexDesensitize { +public @interface Regex { /** * 匹配的正则表达式(默认匹配所有) diff --git a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/handler/DefaultRegexDesensitizationHandler.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/handler/DefaultRegexDesensitizationHandler.java index b3a788aa9..9fa5d80d7 100644 --- a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/handler/DefaultRegexDesensitizationHandler.java +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/handler/DefaultRegexDesensitizationHandler.java @@ -1,22 +1,22 @@ 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.Regex; /** * 默认正则脱敏处理器 * * @author gaibu */ -public class DefaultRegexDesensitizationHandler extends AbstractRegexDesensitizationHandler { +public class DefaultRegexDesensitizationHandler extends AbstractRegexDesensitizationHandler { @Override - String getRegex(RegexDesensitize annotation) { + String getRegex(Regex annotation) { return annotation.regex(); } @Override - String getReplacer(RegexDesensitize annotation) { + String getReplacer(Regex annotation) { return annotation.replacer(); } } diff --git a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/handler/EmailDesensitizationHandler.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/handler/EmailDesensitizationHandler.java index 5313381c3..b6692d448 100644 --- a/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/handler/EmailDesensitizationHandler.java +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/main/java/cn/iocoder/yudao/framework/desensitize/core/regex/handler/EmailDesensitizationHandler.java @@ -1,21 +1,21 @@ 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.Email; /** * 邮箱脱敏处理器 * * @author gaibu */ -public class EmailDesensitizationHandler extends AbstractRegexDesensitizationHandler { +public class EmailDesensitizationHandler extends AbstractRegexDesensitizationHandler { @Override - String getRegex(EmailDesensitize annotation) { + String getRegex(Email annotation) { return annotation.regex(); } @Override - String getReplacer(EmailDesensitize annotation) { + String getReplacer(Email annotation) { return annotation.replacer(); } } diff --git a/yudao-framework/yudao-spring-boot-starter-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/core/slider/DesensitizeTest.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/core/slider/DesensitizeTest.java new file mode 100644 index 000000000..03db9cb72 --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/core/slider/DesensitizeTest.java @@ -0,0 +1,88 @@ +package cn.iocoder.yudao.framework.desensitize.core.slider; + +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.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.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; + +public class DesensitizeTest extends BaseMockitoUnitTest { + + @Test + public void test() { + DesensitizeDemo desensitizeDemo = new DesensitizeDemo(); + desensitizeDemo.setUserName("芋道源码"); + desensitizeDemo.setBankCard("9988002866797031"); + desensitizeDemo.setCarLicense("粤A66666"); + desensitizeDemo.setFixedPhone("01086551122"); + desensitizeDemo.setIdCard("530321199204074611"); + desensitizeDemo.setPassword("123456"); + desensitizeDemo.setPhoneNumber("13248765917"); + desensitizeDemo.setSlider1("ABCDEFG"); + desensitizeDemo.setSlider2("ABCDEFG"); + desensitizeDemo.setSlider3("ABCDEFG"); + desensitizeDemo.setEmail("1@eamil.com"); + desensitizeDemo.setRegex("你好,我是芋道源码"); + desensitizeDemo.setAddress("北京市海淀区上地十街10号"); + desensitizeDemo.setOrigin("芋道源码"); + + DesensitizeDemo d = JsonUtils.parseObject(JsonUtils.toJsonString(desensitizeDemo), DesensitizeDemo.class); + assertEquals("芋***", d.getUserName()); + assertEquals("998800********31", d.getBankCard()); + assertEquals("粤A6***6", d.getCarLicense()); + assertEquals("0108*****22", d.getFixedPhone()); + assertEquals("530321**********11", d.getIdCard()); + assertEquals("******", d.getPassword()); + assertEquals("132****5917", d.getPhoneNumber()); + assertEquals("#######", d.getSlider1()); + assertEquals("ABC*EFG", d.getSlider2()); + assertEquals("*******", d.getSlider3()); + assertEquals("1****@eamil.com", d.getEmail()); + assertEquals("你好,我是*", d.getRegex()); + assertEquals("北京市海淀区上地十街10号*", d.getAddress()); + assertEquals("芋道源码", d.getOrigin()); + } + + @Data + public static class DesensitizeDemo { + @ChineseName + private String userName; + @BankCard + private String bankCard; + @CarLicense + private String carLicense; + @FixedPhone + private String fixedPhone; + @IdCard + private String idCard; + @Password + private String password; + @PhoneNumber + private String phoneNumber; + @Slider(prefixKeep = 6,suffixKeep = 1,replacer = "#") + private String slider1; + @Slider(prefixKeep = 3,suffixKeep = 3) + private String slider2; + @Slider(prefixKeep = 10) + private String slider3; + @Email + private String email; + @Regex(regex = "芋道源码",replacer = "*") + private String regex; + @Address + private String address; + private String origin; + } +} diff --git a/yudao-framework/yudao-spring-boot-starter-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/Address.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/Address.java new file mode 100644 index 000000000..0cd338e1f --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/core/slider/annotation/Address.java @@ -0,0 +1,27 @@ +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.AddressHandler; +import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * 地址 + * + * @author gaibu + */ +@Documented +@Target({ElementType.FIELD}) +@Retention(RetentionPolicy.RUNTIME) +@JacksonAnnotationsInside +@DesensitizeBy(handler = AddressHandler.class) +public @interface Address { + + String replacer() default "*"; + +} diff --git a/yudao-framework/yudao-spring-boot-starter-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/AddressHandler.java b/yudao-framework/yudao-spring-boot-starter-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/AddressHandler.java new file mode 100644 index 000000000..ef1a2bbf9 --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-desensitize/src/test/java/cn/iocoder/yudao/framework/desensitize/core/slider/handler/AddressHandler.java @@ -0,0 +1,11 @@ +package cn.iocoder.yudao.framework.desensitize.core.slider.handler; + +import cn.iocoder.yudao.framework.desensitize.core.base.handler.DesensitizationHandler; +import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.Address; + +public class AddressHandler implements DesensitizationHandler
{ + @Override + public String desensitize(String origin, Address annotation) { + return origin + annotation.replacer(); + } +}