From dd2a82d8137a81b1a08630cd02d50b01729426a3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E9=A9=AC=E5=BA=9C=E5=BC=BA?=
<8569561+fuqiangma@user.noreply.gitee.com>
Date: Wed, 8 Mar 2023 22:23:21 +0800
Subject: [PATCH] =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E8=BF=94=E5=9B=9E=E6=95=B0?=
=?UTF-8?q?=E6=8D=AE=E8=84=B1=E6=95=8F?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
shujutuominsensitive/pom.xml | 107 +++++++++++++
.../feign/TLogFeignConsumerRunner.java | 19 +++
.../example/feign/annotation/Sensitive.java | 26 ++++
.../SensitiveJsonSerializer.java | 42 ++++++
.../feign/controller/TestController.java | 20 +++
.../tlog/example/feign/entity/Person.java | 30 ++++
.../feign/enumnew/SensitiveStrategy.java | 36 +++++
.../src/main/resources/application.properties | 14 ++
.../src/main/resources/logback.xml | 47 ++++++
.../tlog-example-logback-feign-consumer.iml | 141 ++++++++++++++++++
10 files changed, 482 insertions(+)
create mode 100644 shujutuominsensitive/pom.xml
create mode 100644 shujutuominsensitive/src/main/java/com/yomahub/tlog/example/feign/TLogFeignConsumerRunner.java
create mode 100644 shujutuominsensitive/src/main/java/com/yomahub/tlog/example/feign/annotation/Sensitive.java
create mode 100644 shujutuominsensitive/src/main/java/com/yomahub/tlog/example/feign/configuration/SensitiveJsonSerializer.java
create mode 100644 shujutuominsensitive/src/main/java/com/yomahub/tlog/example/feign/controller/TestController.java
create mode 100644 shujutuominsensitive/src/main/java/com/yomahub/tlog/example/feign/entity/Person.java
create mode 100644 shujutuominsensitive/src/main/java/com/yomahub/tlog/example/feign/enumnew/SensitiveStrategy.java
create mode 100644 shujutuominsensitive/src/main/resources/application.properties
create mode 100644 shujutuominsensitive/src/main/resources/logback.xml
create mode 100644 shujutuominsensitive/tlog-example-logback-feign-consumer.iml
diff --git a/shujutuominsensitive/pom.xml b/shujutuominsensitive/pom.xml
new file mode 100644
index 0000000..bf6147c
--- /dev/null
+++ b/shujutuominsensitive/pom.xml
@@ -0,0 +1,107 @@
+
+
+
+
+
+ 8
+ 8
+ 2.1.8.RELEASE
+ Greenwich.SR3
+ 2.1.0.RELEASE
+ 1.8
+ 1.7.25
+ 2.12.0
+ 1.2.17
+ 1.6.1
+ 2.7.6
+ 2.8.4
+ 4.0.1
+ 3.4.6
+ 0.1
+ 1.0.0
+ 1.5.0
+
+
+ 4.0.0
+ com.yomahub.tlog.example
+ tlog-example-logback-feign-consumer
+ 1.0
+
+
+
+ org.springframework.boot
+ spring-boot-starter-web
+
+
+
+ org.springframework.cloud
+ spring-cloud-starter-openfeign
+
+
+ org.springframework.boot
+ spring-boot-starter-logging
+
+
+ com.alibaba.cloud
+ spring-cloud-starter-alibaba-nacos-discovery
+
+
+ com.yomahub
+ tlog-feign-spring-boot-starter
+ ${tlog.version}
+
+
+ com.yomahub
+ tlog-web-spring-boot-starter
+ ${tlog.version}
+
+
+ org.projectlombok
+ lombok
+
+
+
+
+
+
+
+ org.springframework.boot
+ spring-boot-dependencies
+ ${spring-boot.version}
+ pom
+ import
+
+
+
+ org.springframework.cloud
+ spring-cloud-dependencies
+ ${spring-cloud.version}
+ pom
+ import
+
+
+ com.alibaba.cloud
+ spring-cloud-alibaba-dependencies
+ ${spring-cloud-alibaba.version}
+ pom
+ import
+
+
+
+
+ log4j
+ log4j
+ ${log4j.version}
+
+
+ org.slf4j
+ slf4j-api
+ ${slf4j.version}
+
+
+
+
+
+
diff --git a/shujutuominsensitive/src/main/java/com/yomahub/tlog/example/feign/TLogFeignConsumerRunner.java b/shujutuominsensitive/src/main/java/com/yomahub/tlog/example/feign/TLogFeignConsumerRunner.java
new file mode 100644
index 0000000..48233ef
--- /dev/null
+++ b/shujutuominsensitive/src/main/java/com/yomahub/tlog/example/feign/TLogFeignConsumerRunner.java
@@ -0,0 +1,19 @@
+package com.yomahub.tlog.example.feign;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
+import org.springframework.cloud.openfeign.EnableFeignClients;
+import org.springframework.scheduling.annotation.EnableAsync;
+
+@SpringBootApplication
+@EnableFeignClients
+@EnableDiscoveryClient
+@EnableAsync
+public class TLogFeignConsumerRunner {
+
+ public static void main(String[] args) {
+ SpringApplication.run(TLogFeignConsumerRunner.class, args);
+
+ }
+}
diff --git a/shujutuominsensitive/src/main/java/com/yomahub/tlog/example/feign/annotation/Sensitive.java b/shujutuominsensitive/src/main/java/com/yomahub/tlog/example/feign/annotation/Sensitive.java
new file mode 100644
index 0000000..704f4d6
--- /dev/null
+++ b/shujutuominsensitive/src/main/java/com/yomahub/tlog/example/feign/annotation/Sensitive.java
@@ -0,0 +1,26 @@
+package com.yomahub.tlog.example.feign.annotation;
+
+import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+import com.yomahub.tlog.example.feign.configuration.SensitiveJsonSerializer;
+import com.yomahub.tlog.example.feign.enumnew.SensitiveStrategy;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * 接口返回数据如何优雅的实现脱敏?
+ * @className: Sensitive
+ * @author: woniu
+ * @date: 2023/3/8
+ **/
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.FIELD)
+@JacksonAnnotationsInside
+@JsonSerialize(using = SensitiveJsonSerializer.class)
+public @interface Sensitive {
+ //脱敏策略
+ SensitiveStrategy strategy();
+}
diff --git a/shujutuominsensitive/src/main/java/com/yomahub/tlog/example/feign/configuration/SensitiveJsonSerializer.java b/shujutuominsensitive/src/main/java/com/yomahub/tlog/example/feign/configuration/SensitiveJsonSerializer.java
new file mode 100644
index 0000000..dc9a06b
--- /dev/null
+++ b/shujutuominsensitive/src/main/java/com/yomahub/tlog/example/feign/configuration/SensitiveJsonSerializer.java
@@ -0,0 +1,42 @@
+package com.yomahub.tlog.example.feign.configuration;
+
+
+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;
+import com.yomahub.tlog.example.feign.annotation.Sensitive;
+import com.yomahub.tlog.example.feign.enumnew.SensitiveStrategy;
+
+import java.io.IOException;
+import java.util.Objects;
+
+/**
+ * 序列化注解自定义实现
+ * JsonSerializer:指定String 类型,serialize()方法用于将修改后的数据载入
+ */
+public class SensitiveJsonSerializer extends JsonSerializer implements ContextualSerializer {
+ private SensitiveStrategy strategy;
+
+ @Override
+ public void serialize(String value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
+ gen.writeString(strategy.desensitizer().apply(value));
+ }
+
+ /**
+ * 获取属性上的注解属性
+ */
+ @Override
+ public JsonSerializer> createContextual(SerializerProvider prov, BeanProperty property) throws JsonMappingException {
+
+ Sensitive annotation = property.getAnnotation(Sensitive.class);
+ if (Objects.nonNull(annotation) && Objects.equals(String.class, property.getType().getRawClass())) {
+ this.strategy = annotation.strategy();
+ return this;
+ }
+ return prov.findValueSerializer(property.getType(), property);
+
+ }
+}
\ No newline at end of file
diff --git a/shujutuominsensitive/src/main/java/com/yomahub/tlog/example/feign/controller/TestController.java b/shujutuominsensitive/src/main/java/com/yomahub/tlog/example/feign/controller/TestController.java
new file mode 100644
index 0000000..e1e18ec
--- /dev/null
+++ b/shujutuominsensitive/src/main/java/com/yomahub/tlog/example/feign/controller/TestController.java
@@ -0,0 +1,20 @@
+package com.yomahub.tlog.example.feign.controller;
+
+
+import com.yomahub.tlog.example.feign.entity.Person;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+public class TestController {
+
+ @GetMapping("/test")
+ public Person test() {
+ Person user = new Person();
+ user.setRealName("蜗牛");
+ user.setPhoneNumber("1685935467874");
+ user.setAddress("江苏省南京市....");
+ user.setIdCard("4333333333334334333");
+ return user;
+ }
+}
\ No newline at end of file
diff --git a/shujutuominsensitive/src/main/java/com/yomahub/tlog/example/feign/entity/Person.java b/shujutuominsensitive/src/main/java/com/yomahub/tlog/example/feign/entity/Person.java
new file mode 100644
index 0000000..9223c5d
--- /dev/null
+++ b/shujutuominsensitive/src/main/java/com/yomahub/tlog/example/feign/entity/Person.java
@@ -0,0 +1,30 @@
+package com.yomahub.tlog.example.feign.entity;
+
+
+import com.yomahub.tlog.example.feign.annotation.Sensitive;
+import com.yomahub.tlog.example.feign.enumnew.SensitiveStrategy;
+import lombok.Data;
+
+@Data
+public class Person {
+ /**
+ * 真实姓名
+ */
+ @Sensitive(strategy = SensitiveStrategy.USERNAME)
+ private String realName;
+ /**
+ * 地址
+ */
+ @Sensitive(strategy = SensitiveStrategy.ADDRESS)
+ private String address;
+ /**
+ * 电话号码
+ */
+ @Sensitive(strategy = SensitiveStrategy.PHONE)
+ private String phoneNumber;
+ /**
+ * 身份证号码
+ */
+ @Sensitive(strategy = SensitiveStrategy.ID_CARD)
+ private String idCard;
+}
\ No newline at end of file
diff --git a/shujutuominsensitive/src/main/java/com/yomahub/tlog/example/feign/enumnew/SensitiveStrategy.java b/shujutuominsensitive/src/main/java/com/yomahub/tlog/example/feign/enumnew/SensitiveStrategy.java
new file mode 100644
index 0000000..80e5543
--- /dev/null
+++ b/shujutuominsensitive/src/main/java/com/yomahub/tlog/example/feign/enumnew/SensitiveStrategy.java
@@ -0,0 +1,36 @@
+package com.yomahub.tlog.example.feign.enumnew;
+
+import java.util.function.Function;
+
+/**
+ * 脱敏策略,枚举类,针对不同的数据定制特定的策略
+ */
+public enum SensitiveStrategy {
+ /**
+ * 用户名
+ */
+ USERNAME(s -> s.replaceAll("(\\S)\\S(\\S*)", "$1*$2")),
+ /**
+ * 身份证
+ */
+ ID_CARD(s -> s.replaceAll("(\\d{4})\\d{10}(\\w{4})", "$1****$2")),
+ /**
+ * 手机号
+ */
+ PHONE(s -> s.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2")),
+ /**
+ * 地址
+ */
+ ADDRESS(s -> s.replaceAll("(\\S{3})\\S{2}(\\S*)\\S{2}", "$1****$2****"));
+
+
+ private final Function desensitizer;
+
+ SensitiveStrategy(Function desensitizer) {
+ this.desensitizer = desensitizer;
+ }
+
+ public Function desensitizer() {
+ return desensitizer;
+ }
+}
\ No newline at end of file
diff --git a/shujutuominsensitive/src/main/resources/application.properties b/shujutuominsensitive/src/main/resources/application.properties
new file mode 100644
index 0000000..236100d
--- /dev/null
+++ b/shujutuominsensitive/src/main/resources/application.properties
@@ -0,0 +1,14 @@
+spring.application.name=tlog-logback-feign-consumer
+server.port=3111
+#eureka.client.service-url.defaultZone=http://127.0.0.1:1111/eureka/
+
+tlog.pattern=[$currIp][$spanId][$traceId]
+spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
+
+http.connectTimeOutMillis=8000
+http.readTimeOutMillis=8000
+
+spring.task.execution.pool.core-size=5
+spring.task.execution.pool.max-size=50
+spring.task.execution.pool.queue-capacity=200
+spring.task.execution.thread-name-prefix=woniugege-
\ No newline at end of file
diff --git a/shujutuominsensitive/src/main/resources/logback.xml b/shujutuominsensitive/src/main/resources/logback.xml
new file mode 100644
index 0000000..02bc9ab
--- /dev/null
+++ b/shujutuominsensitive/src/main/resources/logback.xml
@@ -0,0 +1,47 @@
+
+
+
+
+
+
+
+
+
+
+ %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
+
+
+
+
+ ${LOG_HOME}/${APP_NAME}.log
+
+
+ ${LOG_HOME}/${APP_NAME}.log.%d{yyyy-MM-dd}.%i.log
+
+ 30
+
+ 1000MB
+
+
+
+ %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
+
+
+
+
+
+
+
+
+
diff --git a/shujutuominsensitive/tlog-example-logback-feign-consumer.iml b/shujutuominsensitive/tlog-example-logback-feign-consumer.iml
new file mode 100644
index 0000000..f04e74f
--- /dev/null
+++ b/shujutuominsensitive/tlog-example-logback-feign-consumer.iml
@@ -0,0 +1,141 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file