springboot AOP中,通过解析SpEL 表达式动态获取参数值

切面注解

bash 复制代码
import com.bn.document.constants.FmDeptCatalogueConstants;

import java.lang.annotation.*;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface FmDeptCatalogueAopAnnotation {

    /**
     * 权限类型
     */
    FmDeptCatalogueConstants value();

    /**
     * 目录id
     */
    String catalogueId() ;
}

切面类 (整个类) 重点代码在下面

bash 复制代码
package com.bn.document.aop;

import com.bn.document.constants.FmDeptCatalogueConstants;
import com.bn.document.po.FmDeptCataloguePo;
import com.bn.document.po.LoginUser;
import com.bn.document.service.FmDeptCatalogueService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.DefaultParameterNameDiscoverer;
import org.springframework.core.ParameterNameDiscoverer;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.List;

@Aspect
@Component
public class MyAspect {

    private final ExpressionParser parser = new SpelExpressionParser();
    private final ParameterNameDiscoverer parameterNameDiscoverer = new DefaultParameterNameDiscoverer();

    @Autowired
    private FmDeptCatalogueService fmDeptCatalogueService;

    /**
     * 定义切入点:拦截带有 @MyCustomAnnotation 的方法
     */
    @Pointcut("@annotation(com.bn.document.aop.FmDeptCatalogueAopAnnotation)")
    public void cataloguePointcut() {}

    /**
     * 环绕增强:获取注解中的权限类型,检查用户权限
     */
    @Around("cataloguePointcut()")
    public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
        // 获取方法上的注解
        FmDeptCatalogueAopAnnotation annotation = ((MethodSignature) joinPoint.getSignature())
                .getMethod()
                .getAnnotation(FmDeptCatalogueAopAnnotation.class);

        if (annotation == null) {
            return joinPoint.proceed(); // 没有注解直接放行
        }

        // 获取权限类型
        FmDeptCatalogueConstants permissionType = annotation.value();

        // 获取方法参数值
        Object[] args = joinPoint.getArgs();
        LoginUser loginUser = null;

        // 遍历参数列表,提取 LoginUser 和 catalogueId
        for (Object arg : args) {
            if (arg instanceof LoginUser) {
                loginUser = (LoginUser) arg;
            }
        }

        // 开始解析  SpEL 表达式
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        // 获取方法参数名 + 参数值,用于构建 SpEL 上下文
        String[] paramNames = parameterNameDiscoverer.getParameterNames(method);

        // 构建 EvaluationContext,并绑定参数
        EvaluationContext context = new StandardEvaluationContext();
        if (paramNames != null && args.length > 0) {
            for (int i = 0; i < paramNames.length; i++) {
                context.setVariable(paramNames[i], args[i]); // 绑定参数
            }
        }
        // 解析表达式中的目录ID
        String catalogueIdExpr = annotation.catalogueId();
        Long catalogueId = parser.parseExpression(catalogueIdExpr).getValue(context, Long.class);

        // 判断参数是否为空
        if (loginUser == null || loginUser.getDeptId() == null) {
            throw new SecurityException("登录信息错误");
        }

        if (catalogueId == null || catalogueId <= 0) {
            throw new SecurityException("目录ID不能为空");
        }

        // 调用服务层查询权限
        List<FmDeptCataloguePo> result = fmDeptCatalogueService.getFmDeptCataloguePoWithDeptId(
                loginUser.getDeptId(),
                catalogueId,
                permissionType
        );

        // 如果权限不存在
        if (result == null || result.isEmpty()) {
            throw new SecurityException("您没有【" + permissionType.getDes() + "】权限");
        }

        // 否则放行
        return joinPoint.proceed();
    }


}

重点【开始解析 SpEL 表达式】

bash 复制代码
// 开始解析  SpEL 表达式
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        // 获取方法参数名 + 参数值,用于构建 SpEL 上下文
        String[] paramNames = parameterNameDiscoverer.getParameterNames(method);

        // 构建 EvaluationContext,并绑定参数
        EvaluationContext context = new StandardEvaluationContext();
        if (paramNames != null && args.length > 0) {
            for (int i = 0; i < paramNames.length; i++) {
                context.setVariable(paramNames[i], args[i]); // 绑定参数
            }
        }
        // 解析表达式中的目录ID
        String catalogueIdExpr = annotation.catalogueId();
        Long catalogueId = parser.parseExpression(catalogueIdExpr).getValue(context, Long.class);

使用

1.获取对象中的属性值

bash 复制代码
 @Override
    @Transactional(rollbackFor = Exception.class)
    @FmDeptCatalogueAopAnnotation(value = FmDeptCatalogueConstants.MODIFY_PERMISSION, catalogueId = "#sysFileCataloguePo.parentId")
    public CommonResponse insertSub(SysFileCataloguePo sysFileCataloguePo,LoginUser loginUser) {
}

1.1 结果

2.直接取值

bash 复制代码
 @Override
    @FmDeptCatalogueAopAnnotation(value = FmDeptCatalogueConstants.DETAILS_PERMISSION, catalogueId = "#id")
    public CommonResponse getSub(Long id,  LoginUser loginUser) {
       
    }

2.1 结果

完结!!!

相关推荐
腻害兔1 小时前
【若依项目-产品经理视角】深度拆解 RuoYi-Vue-Pro 商城模块:从商品管理到交易引擎,50 张表撑起一整套电商系统
java·大数据·vue.js·产品经理·ai编程
GetcharZp1 小时前
抛弃低效SSH!这套Ansible+Web可视化神器,让你天天准点下班!
后端
码智社2 小时前
AES加密原理详解及Java实现加解密实战
java·开发语言
萧瑟余晖3 小时前
JDK 26 新特性详解
java·开发语言
马优晨3 小时前
Freemarker 完整讲解(后端 Java 模板引擎)
java·开发语言·freemarker·freemarker 完整讲解·freemarker模板引擎
weixin_446260855 小时前
HACO:面向动态部署环境的对冲式智能计算可靠多智能体调度框架
后端·python·flask
ttwuai6 小时前
Cursor 生成 CRUD 后,Go 后台接口别只测 200:JWT、RBAC 和 tenant_id 怎么验
开发语言·后端·golang
用户8356290780516 小时前
Python 实现 Excel 页面布局与打印设置自动化
后端·python
维天说6 小时前
CLI-Switch 2026年3月版历史设计:Hook、TTY 隔离与 JSON 状态
java·服务器·json