Spring Boot(五)拦截器创建和注册

一、拦截器

public class MyInterceptorimplements HandlerInterceptor {

private Loggerlogger = LoggerFactory.getLogger ( MyInterceptor.class );

    private NamedThreadLocalstartTimeThreadLocal =new NamedThreadLocal("StopWatch-StartTime");

    @Override

    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o)throws Exception {

//对option请求的处理

        httpServletResponse.setHeader("Access-Control-Allow-Origin", "*");

        httpServletResponse.setHeader("Access-Control-Allow-Methods", "*");

        httpServletResponse.setHeader("Access-Control-Max-Age", "3600");

        httpServletResponse.setHeader("Access-Control-Allow-Headers",

                "Origin, X-Requested-With, Content-Type, Accept");

        // 打印请求日志

//开始时间

        long beginTime = System.currentTimeMillis();

        startTimeThreadLocal.set(beginTime);

        logger.info ("URL : " + httpServletRequest.getRequestURL ().toString () );

        logger.info ("请求方式 : " + httpServletRequest.getMethod () );

        logger.info ("IP : " + httpServletRequest.getRemoteAddr () );

//        HandlerMethod handlerMethod = (HandlerMethod)o;

//        logger.info ( "请求的接口方法 : " + handlerMethod.getMethod());

//        Enumeration enumAttr = httpServletRequest.getParameterNames ();

//        StringBuilder sb = new StringBuilder ();

//        while (enumAttr.hasMoreElements ()) {

//            String attr = enumAttr.nextElement ().toString ();

//            sb.append (attr+":"+ httpServletRequest.getParameter ( attr ) ).append ( ", " );

//        }

//        logger.info("请求参数:{}",sb);

//        logger.info ( "Cookies: " + httpServletRequest.getCookies ());

//参数

        Enumeration enums = httpServletRequest.getParameterNames ();

        while (enums.hasMoreElements ()) {

String paraName = enums.nextElement ();

            logger.info ( paraName +":" + httpServletRequest.getParameter ( paraName ) );

        }

return true;

    }

@Override

    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView)throws Exception {

}

@Override

    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e)throws Exception {

long endTime = System.currentTimeMillis();

        long beginTime =startTimeThreadLocal.get();

        long consumeTime = endTime - beginTime;

        logger.info("请求时间:{}ms",consumeTime);

    }

二、注册

@Configuration

public class MyControllerAdviceextends WebMvcConfigurerAdapter {

@Bean  //拦截器注入为bean

    public HandlerInterceptorgetMyInterceptor(){

return new MyInterceptor();

    }

@Bean  //拦截器注入为bean

    public HandlerInterceptorgetOperateLogInterceptor(){

return new OperateLogInterceptor();

    }

@Override

    public void addInterceptors(InterceptorRegistry registry) {

//注册拦截器1

        registry.addInterceptor(getMyInterceptor()).addPathPatterns("/**");

        //注册拦截器2

        registry.addInterceptor(getOperateLogInterceptor()).addPathPatterns("/**");

        super.addInterceptors(registry);

    }

}

你可能感兴趣的:(Spring Boot(五)拦截器创建和注册)