From d73dc04af4603634c0c188669d022e6059cd09dc Mon Sep 17 00:00:00 2001 From: "M.Mehranfar" Date: Tue, 30 Apr 2024 16:15:06 +0330 Subject: [PATCH] add Date serializer to mask dates in log --- .../http/server/sample/TestConfiguration.java | 1 + .../server/starter/logger/DateSerializer.java | 22 +++++++++++++++++++ .../server/starter/util/ToStringJsonUtil.java | 3 +++ 3 files changed, 26 insertions(+) create mode 100644 tosan-httpserver-spring-boot-starter/src/main/java/com/tosan/http/server/starter/logger/DateSerializer.java diff --git a/tosan-httpserver-spring-boot-sample/src/main/java/com/tosan/http/server/sample/TestConfiguration.java b/tosan-httpserver-spring-boot-sample/src/main/java/com/tosan/http/server/sample/TestConfiguration.java index 0cbdda4..e35a020 100644 --- a/tosan-httpserver-spring-boot-sample/src/main/java/com/tosan/http/server/sample/TestConfiguration.java +++ b/tosan-httpserver-spring-boot-sample/src/main/java/com/tosan/http/server/sample/TestConfiguration.java @@ -49,6 +49,7 @@ public SecureParametersConfig secureParametersConfig() { securedParameters.add(new SecureParameter("age", UserMaskType.LEFT, ComparisonType.EQUALS_IGNORE_CASE)); securedParameters.add(new SecureParameter("average", UserMaskType.RIGHT, ComparisonType.LIKE)); securedParameters.add(new SecureParameter("length", UserMaskType.SEMI, ComparisonType.LIKE)); + securedParameters.add(new SecureParameter("date", UserMaskType.COMPLETE)); return new SecureParametersConfig(securedParameters); } diff --git a/tosan-httpserver-spring-boot-starter/src/main/java/com/tosan/http/server/starter/logger/DateSerializer.java b/tosan-httpserver-spring-boot-starter/src/main/java/com/tosan/http/server/starter/logger/DateSerializer.java new file mode 100644 index 0000000..a30185f --- /dev/null +++ b/tosan-httpserver-spring-boot-starter/src/main/java/com/tosan/http/server/starter/logger/DateSerializer.java @@ -0,0 +1,22 @@ +package com.tosan.http.server.starter.logger; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.JsonSerializer; +import com.fasterxml.jackson.databind.SerializerProvider; + +import java.io.IOException; +import java.util.Date; + +public class DateSerializer extends JsonSerializer { + + private final SerializerUtility serializerUtility; + + public DateSerializer(SerializerUtility serializerUtility) { + this.serializerUtility = serializerUtility; + } + + @Override + public void serialize(Date value, JsonGenerator jsonGenerator, SerializerProvider serializers) throws IOException { + serializerUtility.serialize(value.toString(), jsonGenerator); + } +} \ No newline at end of file diff --git a/tosan-httpserver-spring-boot-starter/src/main/java/com/tosan/http/server/starter/util/ToStringJsonUtil.java b/tosan-httpserver-spring-boot-starter/src/main/java/com/tosan/http/server/starter/util/ToStringJsonUtil.java index 157c19b..ba5698c 100644 --- a/tosan-httpserver-spring-boot-starter/src/main/java/com/tosan/http/server/starter/util/ToStringJsonUtil.java +++ b/tosan-httpserver-spring-boot-starter/src/main/java/com/tosan/http/server/starter/util/ToStringJsonUtil.java @@ -11,6 +11,7 @@ import org.springframework.web.multipart.MultipartFile; import java.text.SimpleDateFormat; +import java.util.Date; /** * @author Mostafa Abdollahi @@ -43,6 +44,8 @@ private void customizeObjectMapper() { module.addSerializer(double.class, numberMaskSerializer); module.addSerializer(short.class, numberMaskSerializer); module.addSerializer(byte[].class, new ByteArraySerializer()); + DateSerializer dateSerializer = new DateSerializer(serializerUtility); + module.addSerializer(Date.class, dateSerializer); module.addSerializer(MultipartFile.class, multipartMaskSerializer); mapper.registerModule(module); mapper.findAndRegisterModules();