Java
运行代码复制代码
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
import org.springframework.stereotype.Component;
//为了记录执行时间 方便调试 如果不需要可以去掉
ThreadLocal<Long> startTime = new ThreadLocal<>();
/*
* 这样可以扫描controller路径下面全部
* 我这里路径是com.test.controller
* .user(包)
* UserController...
* .admin(包)
* AdminController...
*/
@Pointcut("execution(public * cn.web.controller.*.*.*(..))")
public void pointCut() {}
/**
* 参数进行限制或者拦截(后续可数据库存储或ELK)
* @param joinPoint
* @throws Throwable
*/
@Before("pointCut()")
public void before(JoinPoint joinPoint) throws Throwable {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
logger.info("请求来源:" + request.getRemoteAddr());
logger.info("请求URL:" + request.getRequestURL().toString());
logger.info("请求方式:" + request.getMethod());
logger.info("响应方法:" + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
logger.info("请求参数:" + Arrays.toString(joinPoint.getArgs()));
startTime.set(System.currentTimeMillis());
}
/**
* 环绕执行(暂未使用)
* 定义需要匹配的切点表达式,同时需要匹配参数
* @param pjp
* @return
* @throws Throwable
*/
@Around("pointCut()")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
//这句必须有 往下执行方法
Object result = pjp.proceed();
logger.info("耗时(毫秒):" + (System.currentTimeMillis() - startTime.get()));
return result;
}
/**
* 后置通知
* 在方法执行后执行 可以打印返回的数据 判断数据是否是自己需要的或脱敏
* @param point
*/
@After("pointCut()")
public void after(JoinPoint point) {
if (startTime.get() == null) {
startTime.set(System.currentTimeMillis());
}
logger.info("耗时(毫秒):" + (System.currentTimeMillis() - startTime.get()));
// 清理线程池
startTime.remove();
}
}