Spring HandlerInterceptor 与 Servlet 过滤器

发布于 2024-12-14 02:51:25 字数 379 浏览 4 评论 0原文

Spring 中的 HandlerInterceptor 现在可以配置仅使用 在某些 URL 上调用。

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

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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

十二 2024-12-21 02:51:25

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

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

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

The org.springframework.web.servlet.HanderInterceptor Interface JavaDoc itself has a two paragraphs that discuss this question:

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.

人心善变 2024-12-21 02:51:25

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

当将您的请求/响应视为黑匣子系统时,过滤器更合适。无论 servlet 如何实现,它们都会起作用。

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

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

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.

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

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.

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

赠意 2024-12-21 02:51:25

Servlet 过滤器:

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

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

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

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

  1. init:使用 init-param 元素执行初始化过滤器
    过滤器定义。
  2. doFilter:对所有满足条件的HTTP传入请求执行
    “url 模式”。
  3. 销毁:释放过滤器使用的资源。

拦截器:

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

输入图像描述这里

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

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

  1. preHandle : 在执行之前执行目标资源。
  2. afterCompletion:目标资源执行后执行
    (渲染视图后)。
  3. postHandle:拦截处理程序的执行。

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.

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.

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.

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.

enter image description here

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

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.
知你几分 2024-12-21 02:51:25

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文