Java项目使用AOP打印请求参数和响应信息

方便在后台日志直接看到请求参数等信息,可以快速定位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));
//        log.info("请求开始, URI: {}, method: {}, params: {}", uri, method, paraString);
        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("请求结束,controller的返回值是 " + result);
//        log.info("请求结束,controller的返回值是 " + JSON.toJSON (result));
        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
                //JSON.toJSONString(headerMap)
        );
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值