Differences of HandlerInterceptor & Filter

Differences of HandlerInterceptor & Filter

HandlerInterceptor is basically similar to a Servlet Filter, but in contrast to the latter it just allows custom pre-processing with the option of prohibiting the execution of the handler itself, and custom post-processing.
Filters are more powerful, for example they allow for exchanging the request and response objects that are handed down the chain.

HandlerInterceptor与Servlet Filter基本上是相似的,不同的是,HandlerInterceptor仅允许进行pre-processing和post-processing。在pre-processing阶段可以阻止handler本身的执行。

Note that a filter gets configured in web.xml, a HandlerInterceptor in the application context.

Fliter在web.xml中进行配置,HandlerInterceptor在application context中进行配置。

As a basic guideline, fine-grained handler-related preprocessing tasks are candidates for HandlerInterceptor implementations, especially factored-out common handler code and authorization checks.
On the other hand, a Filter is well-suited for request content and view content handling, like multipart forms and GZIP compression. This typically shows when one needs to map the filter to certain content types (e.g. images), or to all requests.

作为一个基本准则,与handler相关的细粒度的预处理任务是HandlerInterceptor实现的候选对象,尤其是抽取的handler公共处理代码和授权检查。
另一方面,Filter非常适合处理请求内容和视图内容,比如multipart表单和GZIP压缩。典型应用于需要将Filter映射到某些内容类型(比如图片)或所有请求的场景。

参考

Spring Interceptor vs Filter 拦截器和过滤器区别
SpringMVC中的Interceptor拦截器及与Filter区别
Spring Boot实战:拦截器与过滤器

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值