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 {
//这句必须有 往下执行方法
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();
}
}