当前位置: 首页 > news >正文

springboot使用aop或Jackson进行数据脱敏

1.aop

启动类加@EnableAspectJAutoProxy

自定义注解,在实体类中使用表示被脱敏字段

建立aop切面类

可能这里gpt会建议你用@Pointcut("execution(public * com.xx.aop..*.get*(..))")这种方式拦截,这种我试了,拦截不住。猜测在mvc返回的时候,已经不被aop拦住了,除非手动调用。并且get方式还要user成为bean,不值当。直接拦截controller包吧。

2.Jackson

序列化类

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.ContextualSerializer;import java.io.IOException;public class DesensitizeSerializer extends JsonSerializer<String> implements ContextualSerializer {private SensitiveType type;private int startInclude;private int endExclude;public DesensitizeSerializer() {this.type = SensitiveType.COMMON;}public DesensitizeSerializer(SensitiveType type) {this.type = type;}@Overridepublic void serialize(String value, JsonGenerator gen, SerializerProvider serializers) throws IOException, IOException {switch (type) {case COMMON:gen.writeString(MsgDesensitizedUtil.commonStr(value));break;case ID_CARD:gen.writeString(MsgDesensitizedUtil.idCardNum(value));break;case PHONE_NUMBER:gen.writeString(MsgDesensitizedUtil.mobilePhone(value));break;case EMAIL:gen.writeString(MsgDesensitizedUtil.email(value));break;default:throw new RuntimeException("未知脱敏类型");}}@Overridepublic JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty property) {if (property != null) {SensitiveData annotation = property.getAnnotation(SensitiveData.class);if (annotation != null) {this.type = annotation.value();}}return this;}}

针对多种类型的脱敏枚举类

在实体中添加就行了,不需要把自定义序列化加载到SimpleModule里

http://www.lryc.cn/news/427053.html

相关文章:

  • 【Solidity】基础介绍
  • 【SpringBoot3】双向实时通讯 websocket
  • 搭建内网开发环境(一)|基于docker快速部署开发环境
  • MATLAB R2023b配置Fortran编译器
  • 2024新型数字政府综合解决方案(七)
  • 搭建高可用k8s集群
  • 完美解决html2canvas + jsPDF导出pdf分页内容截断问题
  • 14 地址映射
  • Java Resilience4j-RateLimiter学习
  • Nginx--地址重写Rewrite
  • webflux源码解析(1)-主流程
  • ipad作为扩展屏的最简单方式
  • 【卡码网Python基础课 17.判断集合成员】
  • 生物研究新范式!AI语言模型在生物研究中的应用
  • python语言day08 属性装饰器和property函数 异常关键字 约束
  • day01JS-数据类型-01
  • MATLAB 手动实现一种高度覆盖值提取建筑物点云的方法(74)
  • git的下载与安装(Windows)
  • 腾讯云AI代码助手 —— 编程新体验,智能编码新纪元
  • 使用 ESP32 和 TFT 屏幕显示实时天气信息 —— 基于 OpenWeatherMap API
  • 高阶数据结构——B树
  • Vue2中watch与Vue3中watch对比和踩坑
  • 在Java程序中执行Linux命令
  • 微信小程序在不同移动设备上的差异导致原因
  • 快速体验fastllm安装部署并支持AMD ROCm推理加速
  • 报错:java: javacTask: 源发行版 8 需要目标发行版 1.8
  • 【数据结构篇】~单链表(附源码)
  • 旋转图像(LeetCode)
  • 入门 - vue中v-model的实现原理和完整用法详解
  • 【区块链+金融服务】港融区域股权服务平台 | FISCO BCOS应用案例