结合SpringBoot实例看Filter和Interceptor的区别

匆匆过客 提交于 2019-12-30 14:00:01

最近遇到一个需求,同事说了一句可以用过滤器或者拦截器实现呀,细想,居然不知道这两个有啥区别,下面就用一个例子来证明。

maven依赖

 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
    </properties>
  
  <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

		<!-- Mock测试使用的json-path依赖 -->
		<dependency>
			<groupId>com.jayway.jsonpath</groupId>
			<artifactId>json-path</artifactId>
		</dependency>
  </dependencies>

创建一个springboot启动类,Application

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

}

创建一个Controller

@RestController
public class TestController {
	
	@RequestMapping("test1")
	public Object test(@RequestParam("ids") List<Integer> ids,HttpServletResponse response) {
		System.out.println("request param:"+ids);
		return ids;
	}
}

创建一个类 实现Filter接口(注意Filter是javax.servlet.Filter)

@Component
public class TestFilter implements Filter{

	//这个方法只是在容器启动的时候执行
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		System.out.println("filter init");
		
	}
	//每个请求 进来和返回 都会经过这
	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		System.out.println("filter doFilter ");
		chain.doFilter(request, response);
	}

//容器销毁的时候调用
	@Override
	public void destroy() {
		System.out.println("filter destory");
		
	}

}

创建类实现接口HandlerInterceptor

@Component
public class TestInterceptor implements HandlerInterceptor{

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

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

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

}

加入拦截器到配置中

@Configuration
public class WebMvcConfiguration extends WebMvcConfigurerAdapter {
    @Autowired
    private TestInterceptor testInterceptor;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //指定拦截器,指定拦截路径
        registry.addInterceptor(testInterceptor).addPathPatterns("/**");
    }
}

运行程序,然后访问http://localhost:8080/test1?ids=1
控制台打印

filter init
filter doFilter 
interceptor preHandle
request param:[1]
interceptor postHandle
interceptor afterCompletion
filter doFilter 

HandlerInterceptor接口三个方法:
preHandle:在执行具体controller方法之前调用
postHandle:在方法的返回之前(return)调用
afterCompletion:方法返回后调用

Filter接口三个方法:
init:容器启动就执行
doFilter:有请求进行或者响应都会执行(如果不执行chain.doFilter(request, response),就表示被过滤后,后面就不会执行,直接返回了)
destory:容器销毁前执行

结合上面的控制台输入,有请求进来时,这六个方法的执行顺序应该是:
Filter#init -> Filter#doFilter -> Interceptor#preHandler ->具体的方法处理 -> Interceptor#postHandle ->Interceptor#afterCompletion ->Filter#doFilter -> Filter#destory

区别总结:
1.Filter是Servlet容器的,Interceptor是SpringMvc实现的(结合springBoot看)
2.Filter对所有请求起作用,Intercptor可以设置拦截规则,而且只对经过DispatchServlet的请求起作用。
3.Filter只能拿到request和response,interceptor可以拿到整个请求上下文,包括request和response。
4.Filter是基于函数回调,Interceptor是基于反射(AOP思想)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!