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(); + } + +}