springboot(五)过滤器和拦截器


前言

       过滤器和拦截器二者都是AOP编程思想的提现,都能实现诸如权限检查、日志记录等。二者有一定的相似之处,不同的地方在于:

  • Filter是servlet规范,只能用在Web程序中,而拦截器是Spring规范,可以用在Web程序中,也可以用在Application程序中。
  • Filter是servlet中定义的,依赖servlet容器。而拦截器在Spring中定义,依赖Spring容器。
  • 拦截器是一个Spring组件,归Spring管理,配置在Spring的配置文件中,因此它可使用Spring的任何资源。比如Service、数据源等,通过IOC容器注入到拦截器即可,而Filter则不行。
  • Filter只在servlet前后起作用,而拦截器则能深入到方法前后,异常抛出前后。使用深度更大一些。

Spring中实现过滤器Filter

方法1:使用springboot提供的 FilterRegistrationBean注册自定义过滤器

public class MyFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("MyFilter init...");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //站点图标/favicon.ico  filter会执行2次
        HttpServletRequest request=(HttpServletRequest) servletRequest;
        System.out.println(request.getRequestURI());
        System.out.println("MyFilter dofilter...");
        filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {

    }
}

 在springboot中注册Filter

@Configuration
public class FilterConfig {

    @Bean
    public FilterRegistrationBean registrationBean(){
        FilterRegistrationBean myfilter=new FilterRegistrationBean(new MyFilter());
        myfilter.addUrlPatterns("/*");

        return myfilter;
    }

   到这里运行demo时会发现do filter执行了2次,debug发现这是因为浏览器请求时站点图标管理,通过uri能发现。可以根据自己的需求用正则表达式适当控制。

方法2:servlet注解定义Filter

@Component
@WebFilter(filterName = "myFilter2",urlPatterns = "/*")
public class MyFilter2 implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("myFilter2 init...");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("myFilter2 dofilter ...");
        filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {

    }
}

 运行结果

 使用servleta注解声明的filter,执行时只有一次请求。和使用spring配置filter这里不同。

Spring中实现拦截器

    拦截器主要使用自定义类集成HandlerInterceptor。preHandle返回true时程序才会继续向下执行,返回false则中断请求。

public class MyInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("/preHandler");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) 
throws Exception { System.out.println("postHandler"); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception { System.out.println("afterCompletion"); } }

 在程序中配置拦截器并声明拦截规则

@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new MyInterceptor()).addPathPatterns("/*");
    }
}

 运行结果