package com.zyp.aspect;
import com.zyp.common.NoLogin;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
@Aspect
@Component
@Order(2)
@Slf4j
public class AopTest {
@Pointcut("execution(public * com.zyp.controller..*.*(..))")
public void pointCut(){}
@Before("pointCut()")
public void before(){
log.info("前置通知");
}
@After("pointCut()")
public void after(){
log.info("后置通知");
}
@Around("pointCut()")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
log.info("环绕通知");
Object[] args = joinPoint.getArgs();
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
Method method = methodSignature.getMethod();
NoLogin annotation = method.getAnnotation(NoLogin.class);
if (annotation != null) {
log.info("免密登录");
}else{
ServletRequestAttributes servletRequestAttributes =
(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = servletRequestAttributes.getRequest();
request.getHeader("token");
}
Class returnType = methodSignature.getReturnType();
Object result = joinPoint.proceed();
log.info("环绕通知");
return result;
}
@AfterReturning("pointCut()")
public void afterReturning(JoinPoint joinPoint) throws Throwable {
log.info("成功返回通知");
}
@AfterThrowing("pointCut()")
public void afterThrowing(JoinPoint joinPoint){
log.info("异常通知");
}
}

- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92