Spring MVC中的拦截器和过滤器之间的区别 [英] Difference between Interceptor and Filter in Spring MVC

查看:323
本文介绍了Spring MVC中的拦截器和过滤器之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于FilterInterceptor的用途,我有些困惑.

I'm a little bit confused about Filter and Interceptor purposes.

据我从文档中了解到,Interceptor在请求之间运行.另一方面,Filter在呈现视图之前运行,但在Controller呈现响应之后运行.

As I understood from docs, Interceptor is run between requests. On the other hand Filter is run before rendering view, but after Controller rendered response.

那么,拦截器中的postHandle()和过滤器中的doFilter()之间的区别在哪里?

So where is the difference between postHandle() in Interceptor and doFilter() in Filter?

应在哪些用例中使用的最佳实践是什么? 在这张图片中,Filter s和Interceptor s在哪里工作?

What is the best practise in which use cases it should be used? In this picture where works Filters and Interceptors?

推荐答案

来自HandlerIntercepter

HandlerInterceptor基本上类似于Servlet Filter,但是在 与后者相反,它仅允许使用 禁止执行处理程序本身的选项和自定义 后期处理.过滤器功能更强大,例如,它们允许 交换流传下来的请求和响应对象 链.请注意,在web.xml中配置了一个过滤器, HandlerInterceptor在应用程序上下文中.

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. Note that a filter gets configured in web.xml, a HandlerInterceptor in the application context.

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

As a basic guideline, fine-grained handler-related pre-processing 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.

话虽这么说

那么Interceptor#postHandle()Filter#doFilter()?

postHandle将在处理程序方法调用之后但呈现视图之前被调用.因此,您可以向视图中添加更多模型对象,但是不能更改HttpServletResponse,因为它已经提交.

postHandle will be called after handler method invocation but before the view being rendered. So, you can add more model objects to the view but you can not change the HttpServletResponse since it's already committed.

doFilter比​​postHandle具有更多的用途.您可以更改请求或响应并将其传递到链上,甚至阻止请求处理.

doFilter is much more versatile than the postHandle. You can change the request or response and pass it to the chain or even block the request processing.

此外,在preHandlepostHandle方法中,您可以访问处理请求的HandlerMethod.因此,您可以基于处理程序本身添加预处理/后处理逻辑.例如,您可以为具有某些注释的处理程序方法添加逻辑.

Also, in preHandle and postHandle methods, you have access to the HandlerMethod that processed the request. So, you can add pre/post-processing logic based on the handler itself. For example, you can add a logic for handler methods that have some annotations.

在哪些用例中应该使用的最佳实践是什么?

What is the best practise in which use cases it should be used?

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

As the doc said, fine-grained handler-related pre-processing 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.

这篇关于Spring MVC中的拦截器和过滤器之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆