logo 范 · 拾光录
网址收集 关于作者 Github Gitee
杂文随笔4
Hexo博客:基础使用Hexo博客:Next主题Hexo博客:Next进阶使用Hexo博客:Next高级配置
前端编程3
前端基础知识16
UniApp15
Vue框架19
Java编程7
Java基础知识14
SpringBoot31
SpringMVC14
MyBatis8
SpringCloud15
RocketMQ1
MyCat1
数据库4
MySQL6
Redis7
MongoDB10
H21
Java技术栈4
知识整理分布式锁的实现方案MySQL事务MySQL的锁
软件工具箱14
IDEAGitMavenGradleNginx安装Nginx配置JMeter压测OllamaPicGoRustFSVSCodeDockerObsidianObs录制
Linux知识11
树莓派安装及使用ArchLinux:常用软件ArchLinux:基础系统安装ArchLinux:深度优化ArchLInux:图形化界面安装ArchLinux:Nirifrp内网穿透Jar启动脚本Linux常用命令VirtualBox安装CentOSVirtualBox安装Ubuntu
Python编程6
Python基础知识Python语法yolo目标检测OpenCV的使用及树莓派平台condauv
创意设计2
Blender:入门知识UI设计基础知识
AI相关9
Claude CodeHermes AgentOpenAI基本使用OpenAI工具调用OpenAI记忆管理OpenAI推理执行OpenAI开发框架Langchainllama.cpp

定义脱敏注解

import java.lang.annotation.*;

@Documented
@Target({ElementType.FIELD,ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface EncryptField {

}

定义脱敏AOP

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.Arrays;

/**
 * 使用AOP进行脱敏
 */
@Slf4j
@Aspect
@Component
public class EncryptHandler {

    /**
     * 环绕通知
     * 异常不做处理,不影响controller的逻辑
     */
    @Around("execution(* com.gxzn.controller..*.*(..))")
    public Object logUpdateUser(ProceedingJoinPoint joinPoint) throws Throwable {
        // 目标参数
        Object[] args = joinPoint.getArgs();
        log.info(Arrays.toString(args));
        for (Object arg : args) {
            Field[] fields = arg.getClass().getDeclaredFields();
            for (Field field : fields) {
                if (field.isAnnotationPresent(EncryptField.class)) {
                    log.info("脱敏字段:{}", field);
                    field.setAccessible(true);
                    String value = (String) field.get(arg);
                    if (value != null) {
                        log.info("脱敏数据:{}", value);
                    }
                }
            }
        }
        // 调用目标方法,获得返回值
        Object result = joinPoint.proceed();
        log.info("响应结果:{}", result);
        return result;
    }

}

使用方式

正常写controller

import com.gxzn.entity.SystemAdminUser;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 测试
 */
@RestController
@RequestMapping("/test")
public class TestController {

    @RequestMapping("/demo")
    public String demo(@RequestBody SystemAdminUser systemAdminUser) {
        System.out.println(systemAdminUser);
        Assert.isTrue(false, "方法内出现异常");
        return "成功";
    }

}
定义脱敏注解
定义脱敏AOP
使用方式