SpringBoot 使用 Interceptor 拦截器

 

package com.example.interceptor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
@Component
public class DemoInterceptor implements HandlerInterceptor {
    private static final Logger logger = LoggerFactory.getLogger(DemoInterceptor.class);
    //controller方法之前调用
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        logger.info("=========DemoInterceptor=======preHandle==============");
        request.setAttribute("startTime",new Date().getTime());
        logger.info("=========DemoInterceptor=======preHandle======类名========"+((HandlerMethod)handler).getBean().getClass().getName());
        logger.info("=========DemoInterceptor=======preHandle======方法名========"+((HandlerMethod)handler).getMethod().getName());
        return true;//false 表示之后的Controller类方法不需要执行,true正常执行
    }
    //controller方法正常执行之后调用
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        logger.info("=========DemoInterceptor=======postHandle==============");
        Long startTime = (Long) request.getAttribute("startTime");
        logger.info("=========DemoInterceptor=======postHandle=======耗时======="+(new Date().getTime()-startTime));
    }
    //controller方法不管正常执行还是异常都之后调用
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        logger.info("=========DemoInterceptor=======afterCompletion=============="+ex);//正常执行时ex是null
        Long startTime = (Long) request.getAttribute("startTime");
        logger.info("=========DemoInterceptor=======postHandle=======耗时======="+(new Date().getTime()-startTime));
    }
}

 

 

package com.example.config;

import com.example.interceptor.DemoFilter;
import com.example.interceptor.DemoInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.ArrayList;
import java.util.List;

@Configuration//说明这是一个配置类
public class WebConfig extends WebMvcConfigurerAdapter {

    @Autowired
    private DemoInterceptor demoInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
//        super.addInterceptors(registry);
        registry.addInterceptor(demoInterceptor).addPathPatterns("/**");//将自己定义的拦截器添加进去,将拦截所有url
    }

    @Bean  //Bean注入,相当于原本在web.xml中写配置注入类
    public FilterRegistrationBean demoFilter(){
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();

        //在DemoFilter类上不用@Compent注入则可以用如下方法注入
        DemoFilter demoFilter = new DemoFilter();
        registrationBean.setFilter(demoFilter);
        List<String> urls = new ArrayList<>();
        urls.add("/*");//对所有url起作用
        registrationBean.setUrlPatterns(urls);

        return  registrationBean;
    }
}

 

.addPathPatterns("/**");//将自己定义的拦截器添加进去,将拦截所有url
posted @ 2021-04-05 10:49  残星  阅读(138)  评论(0编辑  收藏  举报