Spring AOP 和 拦截器 获取类上与方法上的注解

在做一个跨过目标注解的鉴权功能时,想到了AOP与拦截器两种方式,其中 @HasPermission 是我自定义的注解,以下分别为AOP与拦截器获取访问目标类与方法上的注解的方法。由于我的系统在拦截器上配置了拦截过则,所以我选的是拦截器的方式,读者可根据自己的需求来。

一、Spring AOP

先通过ProceedingJoinPoint对象的 joinPoint.getSignature()方法获取到 Signature 的对象并强制类型转换为一个MethodSignature对象,通过 signature.getClass()方法获取到一个 Class 对象,最后通过 AnnotationUtils.findAnnotation()方法获取目标类上的目标注解;
同理,通过 signature.getMethod()方法获取到一个 Method 对象,最后通过 AnnotationUtils.findAnnotation()方法获取目标方法上的目标注解。

@Aspect
@Component
public class PermissionAop {

    @Pointcut("execution(public * com.xxx.yyy.controller.*.*(..))")
    public void pointcut(){

    }

    @Around("pointcut()")
    public ApiResult doAround(ProceedingJoinPoint joinPoint) throws Throwable {
        Object[] args = joinPoint.getArgs();
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        //1.获取目标类上的目标注解(可判断目标类是否存在该注解)
        HasPermission annotationInClass = AnnotationUtils.findAnnotation(signature.getClass(), HasPermission.class);
        //2.获取目标方法上的目标注解(可判断目标方法是否存在该注解)
        HasPermission annotationInMethod = AnnotationUtils.findAnnotation(signature.getMethod(), HasPermission.class);
        //ps:如果1.无法获取类上的注解时
        //使用反射的方式
        /* 
         * MethodSignature signature = (MethodSignature) joinPoint.getSignature();
         * Class<?> tagClass = signatureInMethod.getDeclaringType();
         * boolean annotation = tagClass.isAnnotationPresent(HasPermission.class);
         * HasPermission annotationInClass=null;
         * if(annotation){
         *     annotationInClass = tagClass.getAnnotation(HasPermission.class);
         * }
         * 
         */
        
        //....
        //具体业务逻辑
        //....
        
        return (ApiResult) joinPoint.proceed(args);
    }

}

二、拦截器  

handler 强制转换为 HandlerMetho 对象,再通过 HandlerMethod 对象的handlerMethod.getBeanType() 方法获取到 Bean 对象,最后通过 AnnotationUtils.findAnnotation()方法获取目标类上的目标注解;
同理,通过handlerMethod.getMethod() 方法获取到 Method 对象,最后通过 AnnotationUtils.findAnnotation()方法获取目标方法上的目标注解。

@Component
public class PermissionInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
	    HandlerMethod handlerMethod = (HandlerMethod) handler;
        //1.获取目标类上的目标注解(可判断目标类是否存在该注解)
        HasPermission annotationInClass = AnnotationUtils.findAnnotation(handlerMethod.getBeanType(), HasPermission.class);
        //2.获取目标方法上的目标注解(可判断目标方法是否存在该注解)
        HasPermission annotationInMethod = AnnotationUtils.findAnnotation(handlerMethod.getMethod(), HasPermission.class);

    	//....
    	//..业务逻辑代码
    	//..
  }
}

  • 17
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 18
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 18
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值