| | |
| | | import com.fasterxml.jackson.databind.SerializerProvider; |
| | | import com.fasterxml.jackson.databind.ser.ContextualSerializer; |
| | | import com.ruoyi.common.annotation.Sensitive; |
| | | import com.ruoyi.common.core.service.SensitiveService; |
| | | import com.ruoyi.common.enums.SensitiveStrategy; |
| | | import com.ruoyi.common.utils.SecurityUtils; |
| | | import com.ruoyi.common.utils.spring.SpringUtils; |
| | | |
| | | import java.io.IOException; |
| | | import java.util.Objects; |
| | | |
| | | /** |
| | | * 数据脱敏json序列化工具 |
| | | * |
| | | * @author Yjoioooo |
| | | */ |
| | | public class SensitiveJsonSerializer extends JsonSerializer<String> implements ContextualSerializer { |
| | |
| | | |
| | | @Override |
| | | public void serialize(String value, JsonGenerator gen, SerializerProvider serializers) throws IOException { |
| | | if (SecurityUtils.isAdmin(SecurityUtils.getLoginUser().getUserId()) || SecurityUtils.getLoginUser().getMenuPermissions().contains("Sensitive")){ |
| | | SensitiveService sensitiveService = SpringUtils.getBean(SensitiveService.class); |
| | | if (sensitiveService.isSensitive()) { |
| | | gen.writeString(value); |
| | | } else { |
| | | 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())) { |
| | | if (Objects.nonNull(annotation) && Objects.equals(String.class, property.getType().getRawClass())) { |
| | | this.strategy = annotation.strategy(); |
| | | return this; |
| | | } |