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
package cn.zhicall.web.aspect;
import com.zhicall.framework.core.common.utils.log.LogProxy;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
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.util.Arrays;
/**
- @author hebo@zhicall.cn
- @version 1.0
- @date 2020/6/5 0005 10:48
@Desc 入参前置拦截,借助Spring-AOP
*/
@Component
@Aspect
public class RequestParamsAspect {protected final Logger logger = LogProxy.getLogger("REQUEST_PARAM_LOG");
//为了记录执行时间 方便调试 如果不需要可以去掉
ThreadLocal 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 {