简单拦截器日志打印两种实现

    xiaoxiao2022-07-04  138

    简单拦截器日志打印两种实现

    AOP拦截

    package com.per.demo.configuration; import com.alibaba.fastjson.JSONObject; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import org.springframework.web.servlet.HandlerInterceptor; import javax.servlet.http.HttpServletRequest; import java.util.Arrays; import java.util.Enumeration; /** * @author LingYin.Fan * @version 1.0.0 * @ClassName LoggerConfiguration.java * @Description 拦截请求-打印日志 * @createTime 2019年05月22日 15:02:00 */ @Aspect @Component public class LoggerConfiguration{ protected final Logger logger = LoggerFactory.getLogger(this.getClass()); @Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping) " + "|| @annotation(org.springframework.web.bind.annotation.PostMapping) " + "||@annotation(org.springframework.web.bind.annotation.GetMapping)") // @Pointcut("execution(* com.jianlc.open.controller.*(..)) ") public void controllerLog() { } @Around("controllerLog()") public Object interception(ProceedingJoinPoint joinPoint) throws Throwable { //接收到请求,记录请求内容 ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); // 设置请求开始时间 Long startTime = System.currentTimeMillis(); // 提取全部参数 paramJson Enumeration<String> paramNames = request.getParameterNames(); JSONObject paramJson = new JSONObject(); while (paramNames.hasMoreElements()) { String paramName = paramNames.nextElement(); paramJson.put(paramName, request.getParameter(paramName)); } //controller中接收参数的实体类 StringBuilder param = new StringBuilder(); if (joinPoint.getArgs() != null) { // param.append(StringUtils.join(joinPoint.getArgs(),',')); Arrays.asList(joinPoint.getArgs()).forEach(obj -> param.append(",").append(obj)); } //相应参数类和抛出异常处理 Object result = null; Throwable exception = null; try { result = joinPoint.proceed(); } catch (Throwable throwable) { exception = throwable; throw throwable; } finally { logger.info(request.getMethod() + " " + request.getRequestURL() + " 参数:" + paramJson.toJSONString() + param + " ," + (System.currentTimeMillis() - startTime) + "ms," + " 响应结果:" + result, exception); } return result; } }

    实现HandlerInterceptor

    package com.per.demo.configuration; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * @author LingYin.Fan * @version 1.0.0 * @ClassName LoggerConfiguration1.java * @Description HandlerInterceptor * @createTime 2019年05月22日 15:13:00 */ public class LoggerConfiguration1 implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { return false; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { } }

    在三个方法针对自己想处理的先后,写上自己代码就ok


    最后说一下aop这几个注解的先后顺序

    Aound ->before->proceed->after->afterReturning->afterThrowing

    正常情况

    异常情况

    最新回复(0)