• java一个切面如何作用做个注解


    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Pointcut;
    import org.aspectj.lang.reflect.MethodSignature;
    import org.springframework.core.annotation.Order;
    import org.springframework.stereotype.Component;
    
    import cn.dev33.satoken.strategy.SaStrategy;
    import cn.dev33.satoken.util.SaTokenConsts;
    
    /**
     * sa-token 基于 Spring Aop 的注解鉴权
     * 
     * @author kong
     */
    @Aspect
    @Component
    @Order(SaTokenConsts.ASSEMBLY_ORDER)
    public class SaCheckAspect {
       
       /**
        * 构建
        */
       public SaCheckAspect() {
       }
    
       /**
        * 定义AOP签名 (切入所有使用sa-token鉴权注解的方法)
        */
       public static final String POINTCUT_SIGN = 
             "@within(cn.dev33.satoken.annotation.SaCheckLogin) || @annotation(cn.dev33.satoken.annotation.SaCheckLogin) || "
             + "@within(cn.dev33.satoken.annotation.SaCheckRole) || @annotation(cn.dev33.satoken.annotation.SaCheckRole) || "
             + "@within(cn.dev33.satoken.annotation.SaCheckPermission) || @annotation(cn.dev33.satoken.annotation.SaCheckPermission) || "
             + "@within(cn.dev33.satoken.annotation.SaCheckSafe) || @annotation(cn.dev33.satoken.annotation.SaCheckSafe) || "
             + "@within(cn.dev33.satoken.annotation.SaCheckBasic) || @annotation(cn.dev33.satoken.annotation.SaCheckBasic)";
    
       /**
        * 声明AOP签名
        */
       @Pointcut(POINTCUT_SIGN)
       public void pointcut() {
       }
    
       /**
        * 环绕切入
        * 
        * @param joinPoint 切面对象
        * @return 底层方法执行后的返回值
        * @throws Throwable 底层方法抛出的异常
        */
       @Around("pointcut()")
       public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
          
          // 注解鉴权
          MethodSignature signature = (MethodSignature) joinPoint.getSignature();
          SaStrategy.me.checkMethodAnnotation.accept(signature.getMethod());
    
          try {
             // 执行原有逻辑
             Object obj = joinPoint.proceed();
             return obj;
          } catch (Throwable e) {
             throw e;
          }
       }
    
    }
    
  • 相关阅读:
    React_Refs转发
    拿到这份Java面试文档“狂刷”3周,成功拿到京东的offer
    sort 排序使用介绍
    代码规范HelloWorld
    《网络安全笔记》第八章:计算机网络基本概念
    使用GitHub来Merge代码
    C/C++进程超详细详解【下部分】(系统性学习day8)
    Tomcat9的安装以及配置环境
    大学生餐饮主题网页制作 美食网页设计模板 学生静态网页作业成品 dreamweaver美食甜品蛋糕HTML网站制作
    Redis 学习-上
  • 原文地址:https://blog.csdn.net/qq_42093488/article/details/125603100