Spring HandlerInterceptor 与 Servlet 过滤器
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
org.springframework.web.servlet.HanderInterceptor
接口JavaDoc 本身有两段讨论这个问题:The
org.springframework.web.servlet.HanderInterceptor
Interface JavaDoc itself has a two paragraphs that discuss this question: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.
Servlet 过滤器:
顾名思义,过滤器是由 servlet 容器针对每个传入的 http 请求和每个 http 响应执行的 Java 类。通过这种方式,可以在 HTTP 传入请求到达资源(例如 JSP 页面、servlet 或简单的静态页面)之前对其进行管理;以同样的方式可以在资源执行后管理 HTTP 出站响应。
此行为允许实现在许多不同上下文中重用的通用功能。
如上图所示,过滤器在Web容器中运行,因此其定义也将包含在web.xml文件中
过滤器包括三个主要方法:
过滤器定义。
“url 模式”。
拦截器:
Spring拦截器与Servlet过滤器类似,但它们在Spring上下文中运行,因此在管理HTTP请求和响应方面功能强大,但它们可以实现更复杂的行为,因为可以访问所有 Spring 上下文。
Spring拦截器在SpringMVC上下文中执行,因此它们已在rest-servlet.xml文件中定义:
拦截器包括三个主要方法:
(渲染视图后)。
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.
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:
filter definition.
"url-pattern".
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.
The Spring interceptor are execute in SpringMVC context so they have be defined in rest-servlet.xml file:
The interceptor include three main methods:
(after rendering the view).
使用 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.