From 1eec8f570ffdb49120ea6e2bba0f2681986fdf0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90li?= <15040126243@163.com> Date: Fri, 29 Jul 2022 14:25:14 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E4=BF=AE=E5=A4=8D=20=E8=84=B1=E6=95=8F?= =?UTF-8?q?=E6=B2=A1=E6=9C=89=E5=AE=9E=E7=8E=B0=E7=B1=BB=E5=AF=BC=E8=87=B4?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E6=95=B0=E6=8D=AE=E5=BC=82=E5=B8=B8=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/jackson/SensitiveJsonSerializer.java | 17 +++++++++---- .../service/impl/SensitiveServiceImpl.java | 25 +++++++++++++++++++ 2 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 ruoyi-example/ruoyi-demo/src/main/java/com/ruoyi/demo/service/impl/SensitiveServiceImpl.java diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/jackson/SensitiveJsonSerializer.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/jackson/SensitiveJsonSerializer.java index 487bc4c9..946c5c32 100644 --- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/jackson/SensitiveJsonSerializer.java +++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/jackson/SensitiveJsonSerializer.java @@ -11,6 +11,8 @@ import com.ruoyi.common.core.annotation.Sensitive; import com.ruoyi.common.core.enums.SensitiveStrategy; import com.ruoyi.common.core.service.SensitiveService; import com.ruoyi.common.core.utils.SpringUtils; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.BeansException; import java.io.IOException; import java.util.Objects; @@ -20,19 +22,24 @@ import java.util.Objects; * * @author Yjoioooo */ +@Slf4j public class SensitiveJsonSerializer extends JsonSerializer implements ContextualSerializer { private SensitiveStrategy strategy; @Override public void serialize(String value, JsonGenerator gen, SerializerProvider serializers) throws IOException { - SensitiveService sensitiveService = SpringUtils.getBean(SensitiveService.class); - if (ObjectUtil.isNotNull(sensitiveService) && sensitiveService.isSensitive()) { - gen.writeString(strategy.desensitizer().apply(value)); - } else { + try { + SensitiveService sensitiveService = SpringUtils.getBean(SensitiveService.class); + if (ObjectUtil.isNotNull(sensitiveService) && sensitiveService.isSensitive()) { + gen.writeString(strategy.desensitizer().apply(value)); + } else { + gen.writeString(value); + } + } catch (BeansException e) { + log.error("脱敏实现不存在, 采用默认处理 => {}", e.getMessage()); gen.writeString(value); } - } @Override diff --git a/ruoyi-example/ruoyi-demo/src/main/java/com/ruoyi/demo/service/impl/SensitiveServiceImpl.java b/ruoyi-example/ruoyi-demo/src/main/java/com/ruoyi/demo/service/impl/SensitiveServiceImpl.java new file mode 100644 index 00000000..ff48be7d --- /dev/null +++ b/ruoyi-example/ruoyi-demo/src/main/java/com/ruoyi/demo/service/impl/SensitiveServiceImpl.java @@ -0,0 +1,25 @@ +package com.ruoyi.demo.service.impl; + +import com.ruoyi.common.core.service.SensitiveService; +import com.ruoyi.common.satoken.utils.LoginHelper; +import org.springframework.stereotype.Service; + +/** + * 脱敏服务 + * 默认管理员不过滤 + * 需自行根据业务重写实现 + * + * @author Lion Li + */ +@Service +public class SensitiveServiceImpl implements SensitiveService { + + /** + * 是否脱敏 + */ + @Override + public boolean isSensitive() { + return !LoginHelper.isAdmin(); + } + +}