方便在后台日志直接看到请求参数等信息,可以快速定位bug原因
@Aspect
@Configuration
@Slf4j
public class LogAspect {
@Autowired
private RedisOperate redisOperate;
@Pointcut("execution(* com.dkdy.goodsserver.controller.*Controller.*(..))")
public void excudeService() {
}
@Around("excudeService()")
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
RequestAttributes ra = RequestContextHolder.getRequestAttributes();
ServletRequestAttributes sra = (ServletRequestAttributes) ra;
HttpServletRequest request = sra.getRequest();
Enumeration<String> headerNames = request.getHeaderNames();
Map<String, Object> headerMap = new HashMap<> (10);
do {
String header = headerNames.nextElement();
headerMap.put(header, request.getHeader(header));
} while (headerNames.hasMoreElements());
String token = request.getParameter("token");
if (StringUtil.isEmpty(token)) {
token = request.getHeader("token");
if (StringUtil.isEmpty(token)) {
token = RequestUtil.getCookieValue(request, "token");
}
}
long start=System.currentTimeMillis ();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
log.info("***************************start "+sdf.format(start)+" *************************************************");
Object userObject = null;
userObject = redisOperate.getByKey("token:" + token);
String method = request.getMethod();
String uri = request.getRequestURI();
String paraString = JSON.toJSONString(request.getParameterMap());
String current = sdf.format(System.currentTimeMillis());
log.info("\n" +
"请求地址 >>> {}\n" +
"请求方法 >>> {}\n" +
"请求参数 >>> {}\n" +
"请求来源 >>> {}\n" +
"内容类型 >>> {}\n" +
"用户令牌 >>> {}\n" +
"用户实例 >>> {}\n" +
"请求头部 >>> {}\n",
request.getRequestURI(),
request.getMethod(),
JSONUtil.toJSON(request.getParameterMap()),
request.getRemoteAddr(),
request.getContentType(),
token,
accountId,
JSON.toJSONString(headerMap));
Object result = pjp.proceed();
long end=System.currentTimeMillis ();
log.info("\n"+
"请求结束"+uri+" "+sdf.format(end)+"耗时 "+(end-start)+"ms"+"\n"+
JSON.toJSONString(result,true));
log.info("***************************end "+sdf.format(end)+"耗时 "+(end-start)+"ms");
return result;
}
}
添加全局拦截器校验token
@Slf4j
@Component
public class
RequestTokenInterceptor extends HandlerInterceptorAdapter {
@Autowired
private RedisOperate redisOperate;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (URLFilter.handle(request.getRequestURI())) {
return true;
}
Enumeration<String> headerNames = request.getHeaderNames();
Map<String, Object> headerMap = Maps.newHashMapWithExpectedSize(10);
do {
String header = headerNames.nextElement();
headerMap.put(header, request.getHeader(header));
} while (headerNames.hasMoreElements());
String token = request.getParameter("token");
if (StringUtil.isEmpty(token)) {
token = request.getHeader("token");
if (StringUtil.isEmpty(token)) {
token = RequestUtil.getCookieValue(request, "token");
}
}
Object userObject = null;
userObject = redisOperate.getByKey("token:" + token);
Long accountId = null;
if (userObject != null) {
accountId = ((CurrentUser) userObject).getAccount_id();
}
log.info("\n-=-=-=-=-=-=-=-=-=-=-=-=-=-■GOODS SERVICE■-=-=-=-=-=-=-=-=-=-=-=-=-=-\n" +
"请求地址 >>> {}\n" +
"请求方法 >>> {}\n" +
"请求参数 >>> {}\n" +
"请求来源 >>> {}\n" +
"内容类型 >>> {}\n" +
"用户令牌 >>> {}\n" +
"用户实例 >>> {}\n",
request.getRequestURI(),
request.getMethod(),
JSONUtil.toJSON(request.getParameterMap()),
request.getRemoteAddr(),
request.getContentType(),
token,
accountId
);
return true;
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
}
}