美文网首页
基于springBoot的拦截器和过滤器

基于springBoot的拦截器和过滤器

作者: _FireFly_ | 来源:发表于2020-12-01 08:40 被阅读0次
过滤器和拦截器的执行链路

过滤器和拦截器的异同

  • 过滤器(Filter):当你有一堆东西的时候,你只希望选择符合你要求的某一些东西。定义这些 要求的工具,就是过滤器。
  • 拦截器(Interceptor):在一个流程正在进行的时候,你希望干预它的进展,甚至终止它进行, 这是拦截器做的事情。
  • 相同点:都是aop编程思想的体现,可以在程序执行前后做一些操作
  • 不同点:过滤器依赖于servlet容器,拦截器不依赖 过滤器的执行由Servlet容器回调完成,而拦截器通常通过动态代理的方式来执行。 触发时机不一样,过滤器是在请求进入Tomcat容器后,而进入servlet前进行预处理的;拦 截器是在进入servlet之后,而进入controller之前处理的。 拦截器可以获取IOC容器中的各个bean,而过滤器就不行,拦截器归Spring管理。

过滤器一

package com.duing.filter;

import javax.servlet.*;
import java.io.IOException;

public class CustomFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println(".............CustomFilter init............");
    }

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

    @Override
    public void destroy() {
        System.out.println(".............CustomFilter destroy............");
    }
}

过滤器二

package com.duing.filter;


import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

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

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

    @Override
    public void destroy() {
        System.out.println(".............CustomFilter2 destroy............");
    }
}

过滤器配置类

FilterConfig

import com.duing.filter.CustomFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * web.xml
 * <filter>
 *     <filter-name>struts2</filter-name>
 *     <filter-class>com.duing.filter.CustomFilter</filter-class>
 * </filter>
 *
 * <filter-mapping>
 *     <filter-name>struts2</filter-name>
 *     <url-pattern>/*</url-pattern>
 * </filter-mapping>
 */
@Configuration
public class FilterConfig {

    @Bean
    public FilterRegistrationBean<CustomFilter> filterRegistrationBean(){
        FilterRegistrationBean<CustomFilter> filterFilterRegistrationBean=
                new FilterRegistrationBean<>();
        filterFilterRegistrationBean.setFilter(new CustomFilter());
        filterFilterRegistrationBean.addUrlPatterns("/*");
//        filterFilterRegistrationBean.setOrder(0); //决定注册的优先级
        return filterFilterRegistrationBean;
    }
}

拦截器

package com.duing.interceptor;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Service
public class CustomInterceptor implements HandlerInterceptor {

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

    @Override
    public void postHandle(HttpServletRequest request,
                    HttpServletResponse response, Object handler,
                    @Nullable ModelAndView modelAndView) throws Exception {

        System.out.println("...........CustomInterceptor postHandle...........");
    }

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

        System.out.println("...........CustomInterceptor afterCompletion...........");

    }

}

拦截器配置类

import com.duing.interceptor.CustomInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class InterceptorConfig implements WebMvcConfigurer{

    @Autowired
    private CustomInterceptor customInterceptor;


    /**
     * 注册自定义的拦截器,并且定义拦截规则
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(customInterceptor).addPathPatterns("/**");

    }
}

controller

package com.duing.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ServletController {

    @RequestMapping("/servlet")
    public String servlet(){
        System.out.println("ServletController servlet........");
        return "Hello Servlet";
    }
}

springboot启动类

package com.duing;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

@SpringBootApplication  
@ServletComponentScan   //扫描@WebFilter(filterName = "customFilter2",urlPatterns = {"/*"})过滤器注解
public class ServletApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServletApplication.class, args);
    }

}

相关文章

  • java拦截器和过滤器的区别

    过滤器和拦截器的区别: ①拦截器是基于java的反射机制的,而过滤器是基于函数回调。②拦截器不依赖与servlet...

  • 拦截器和过滤器详解

    过滤器和拦截器的区别: 拦截器是基于java的反射机制的,而过滤器是基于函数回调。 拦截器不依赖与servlet容...

  • 过滤器,拦截器,监听器的区别

    过滤器和拦截器的区别: ①拦截器是基于java的反射机制的,而过滤器是基于函数回调。②拦截器不依赖与servlet...

  • Spring-Interceptor

    过滤器和拦截器的区别: ①拦截器是基于java的反射机制的,而过滤器是基于函数回调。 ②拦截器不依赖与servle...

  • 拦截器和过滤器

    拦截器和过滤器的区别: 1.拦截器是基于java反射机制的,过滤器是基于函数回调2.拦截器不依赖于servlet,...

  • 拦截器(Interceptor)和过滤器(Filter)的区别和

    1、拦截器与过滤器的区别 1)拦截器是基于java的反射机制的,而过滤器是基于函数回调。 2)拦截器是依赖于web...

  • 拦截器(interceptor)与过滤器(filter)的区别

    1.拦截器是基于java反射机制,过滤器基于函数的回调 2 拦截器不依赖Servlet 容器 ,过滤器依赖 3 拦...

  • Kafka消费者JoinGroupRequest流程解析

    过滤器(Filter)和拦截器(Interceptor)都是基于 AOP(Aspect Oriented Prog...

  • Spring MVC 过滤器和拦截器

    1.区别: 拦截器是基于Java的反射机制的,而过滤器是基于函数回调。 拦截器不依赖与servlet容器,过滤器依...

  • 拦截器与过滤器

    主要区别: 拦截器是基于Java的反射机制的,而过滤器是基于函数回调。 拦截器不依赖于servlet容器,过滤器依...

网友评论

      本文标题:基于springBoot的拦截器和过滤器

      本文链接:https://www.haomeiwen.com/subject/hytjwktx.html