package com.msxf.demo.aspect;
import com.msxf.demo.annotation.Inner;
import lombok.SneakyThrows;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;
/**
* @author: hzq
* @date: 2022/4/24
*/
@Aspect
@Component
public class InnerAspect {
// 类和方法上带有Inner.class注解 都会被拦截
// @within(inner) 注解在类上
// @annotation(inner) 注解在方法上
// 参数inner 传入的后面表达式
@Around("@within(inner) || @annotation(inner)")
@SneakyThrows
public Object around(ProceedingJoinPoint point, Inner inner) {
// 方法上面没有注解则获取类上面的
if (inner == null) {
Class<?> aClass = point.getTarget().getClass();
inner = AnnotationUtils.findAnnotation(aClass, Inner.class);
}
if (inner.value()) {
throw new RuntimeException("无权访问");
}
return point.proceed();
}
@Pointcut("execution(public String com.msxf.demo.controller.TestInnerController.select())")
public void point(){}
@Around("point()")
@SneakyThrows
public Object p(ProceedingJoinPoint point){
System.out.println("触发了");
return point.proceed();
}
}
AOP 拦截注解
于 2022-04-24 10:20:15 首次发布