Spring HandlerInterceptor 与 Servlet 过滤器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8000844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 04:44:04  来源:igfitidea点击:

Spring HandlerInterceptor vs Servlet Filters

springspring-mvcservlet-filtersinterceptor

提问by aces.

HandlerInterceptors in Spring can now be configured to be invoked only on certain URLs using <mvc:interceptors>.

Spring 中的HandlerInterceptor现在可以配置为使用<mvc:interceptors>.

Servlet Filters can achieve same functionality (logging, security etc). So which one should be used?

Servlet 过滤器可以实现相同的功能(日志记录、安全性等)。那么应该使用哪一种呢?

I think with Interceptors, one can use ModelAndViewobject to work with Models so it has more advantages. Can anyone draw out scenarios where Filters or Interceptors have advantages over the other?

我认为使用拦截器,可以使用ModelAndView对象来处理模型,因此它具有更多优势。任何人都可以画出过滤器或拦截器比另一个更具优势的场景吗?

采纳答案by Ralph

The org.springframework.web.servlet.HanderInterceptorInterface JavaDocitself has a two paragraphs that discuss this question:

org.springframework.web.servlet.HanderInterceptor接口的JavaDoc本身有两个段落讨论这个问题:

HandlerInterceptor is basically similar to a Servlet 2.3 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. Note that a filter gets configured in web.xml, a HandlerInterceptor in the 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.

HandlerInterceptor 基本上类似于 Servlet 2.3 过滤器,但与后者相反,它只允许自定义预处理和禁止执行处理程序本身的选项,以及自定义后处理。过滤器更强大,例如它们允许交换传递给链的请求和响应对象。请注意,过滤器在 web.xml 中配置,即应用程序上下文中的 HandlerInterceptor。

作为基本准则,与细粒度处理程序相关的预处理任务是 HandlerInterceptor 实现的候选者,尤其是分解出的公共处理程序代码和授权检查。另一方面,过滤器非常适合请求内容和视图内容处理,例如多部分表单和 GZIP 压缩。这通常显示何时需要将过滤器映射到某些内容类型(例如图像)或所有请求。

回答by skaffman

Spring Handler interceptors allow you to hook into more parts of the request lifecycle, and get access to more information in the process. They're often more intimately coupled to the request/response cycle than filters.

Spring Handler 拦截器允许您钩入请求生命周期的更多部分,并在该过程中获取更多信息。与过滤器相比,它们通常与请求/响应周期更紧密地耦合。

Filters are more suitable when treating your request/response as a black box system. They'll work regardless of how the servlet is implemented.

将您的请求/响应视为黑盒系统时,过滤器更合适。无论 servlet 是如何实现的,它们都可以工作。

If you're using Spring MVC, there's little reason to write new logic as a servlet filter. Everything filters can do, interceptors can do more easily and more elegantly.

如果您使用的是 Spring MVC,则几乎没有理由将新逻辑编写为 servlet 过滤器。过滤器可以做的一切,拦截器可以更轻松、更优雅地完成。

Remember also, servlet filters have been around for much longer than interceptors.

还要记住,servlet 过滤器比拦截器存在的时间要长得多。

回答by smp7d

With a Spring interceptor, you have access to the Handler which may be useful. Also, with a Spring interceptor, you have access to execute logic before the view renders and after the view is rendered.

使用 Spring 拦截器,您可以访问可能有用的 Handler。此外,使用 Spring 拦截器,您可以在视图呈现之前和视图呈现之后访问执行逻辑。

回答by Noman Akhtar

Servlet Filter:

Servlet Filter:

A filter as the name suggests is a Java class executed by the servlet container for each incoming http request and for each http response. This way, is possible to manage HTTP incoming requests before them reach the resource, such as a JSP page, a servlet or a simple static page; in the same way is possible to manage HTTP outbound response after resource execution.

顾名思义,过滤器是 servlet 容器为每个传入的 http 请求和每个 http 响应执行的 Java 类。通过这种方式,可以在 HTTP 传入请求到达资源之前对其进行管理,例如 JSP 页面、servlet 或简单的静态页面;以同样的方式可以在资源执行后管理 HTTP 出站响应。

This behaviour allow to implement common functionality reused in many different contexts.

此行为允许实现在许多不同上下文中重用的通用功能。

enter image description here

在此处输入图片说明

As shown in the figure above, the filter runs in the web container so its definition will also be contained in the web.xml file.

如上图所示,过滤器在 web 容器中运行,因此它的定义也将包含在 web.xml 文件中。

The filter include three main methods:

过滤器包括三种主要方法:

  1. init: Executed to initialize filter using init-param element in filter definition.
  2. doFilter: Executed for all HTTP incoming request that satisfy "url-pattern".
  3. destroy: Release resources used by the filter.
  1. init:执行以使用过滤器定义中的 init-param 元素初始化过滤器。
  2. doFilter:对所有满足“url-pattern”的 HTTP 传入请求执行。
  3. destroy:释放过滤器使用的资源。

Interceptor:

Interceptor:

Spring Interceptors are similar to Servlet Filters but they acts in Spring Context so are many powerful to manage HTTP Request and Response but they can implement more sofisticated behaviour because can access to all Spring context.

Spring 拦截器类似于 Servlet 过滤器,但它们在 Spring 上下文中起作用,因此在管理 HTTP 请求和响应方面非常强大,但它们可以实现更复杂的行为,因为可以访问所有 Spring 上下文。

enter image description here

在此处输入图片说明

The Spring interceptor are execute in SpringMVC context so they have be defined in rest-servlet.xml file:

Spring 拦截器在 SpringMVC 上下文中执行,因此它们已在 rest-servlet.xml 文件中定义:

The interceptor include three main methods:

拦截器包括三个主要方法:

  1. preHandle: Executed before the execution of the target resource.
  2. afterCompletion: Executed after the execution of the target resource (after rendering the view).
  3. postHandle: Intercept the execution of a handler.
  1. preHandle:在执行目标资源之前执行。
  2. afterCompletion:在目标资源执行之后(渲染视图之后)执行。
  3. postHandle:拦截处理程序的执行。