Java Spring MVC 中拦截器和过滤器的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35856454/
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
Difference between Interceptor and Filter in Spring MVC
提问by rpieniazek
I'm a little bit confused about Filter
and Interceptor
purposes.
我对Filter
和Interceptor
目的有点困惑。
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.
正如我从文档中了解到的,Interceptor
在请求之间运行。另一方面Filter
是在渲染视图之前运行,但在控制器渲染响应之后运行。
So where is the difference between postHandle()
in Interceptor and doFilter()
in Filter?
那么postHandle()
Interceptor 和doFilter()
Filter的区别在哪里呢?
What is the best practise in which use cases it should be used?
In this picture where works
Filter
s and Interceptor
s?
采纳答案by Ali Dehghani
From HandlerIntercepter
's javadoc:
从HandlerIntercepter
的javadoc:
HandlerInterceptor
is basically similar to a ServletFilter
, 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 inweb.xml
, aHandlerInterceptor
in the application context.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, aFilter
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
基本上类似于 ServletFilter
,但与后者相反,它只允许自定义预处理和禁止执行处理程序本身的选项,以及自定义后处理。过滤器更强大,例如它们允许交换传递给链的请求和响应对象。请注意,过滤器在应用程序上下文中的web.xml
, aHandlerInterceptor
中配置。作为基本准则,细粒度处理程序相关的预处理任务是
HandlerInterceptor
实现的候选者,尤其是分解出的公共处理程序代码和授权检查。另一方面,aFilter
非常适合请求内容和查看内容处理,例如多部分表单和 GZIP 压缩。这通常显示何时需要将过滤器映射到某些内容类型(例如图像)或所有请求。
With that being said:
话虽如此:
So where is the difference between
Interceptor#postHandle()
andFilter#doFilter()
?
那么
Interceptor#postHandle()
和 之间的区别在Filter#doFilter()
哪里?
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 notchange the HttpServletResponse
since it's already committed.
postHandle
将在处理程序方法调用之后但在呈现视图之前调用。因此,您可以将更多模型对象添加到视图中,但您不能更改HttpServletResponse
它,因为它已经提交。
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.
doFilter
比postHandle
. 您可以更改请求或响应并将其传递给链,甚至阻止请求处理。
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.
此外,在preHandle
和postHandle
方法中,您可以访问HandlerMethod
处理请求的 。因此,您可以根据处理程序本身添加预处理/后处理逻辑。例如,您可以为具有一些注释的处理程序方法添加逻辑。
What is the best practise in which use cases it should be used?
应该在哪些用例中使用它的最佳实践是什么?
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.
正如文档所说,细粒度的处理程序相关的预处理任务是HandlerInterceptor
实现的候选者,尤其是分解出的公共处理程序代码和授权检查。另一方面,aFilter
非常适合请求内容和查看内容处理,例如多部分表单和 GZIP 压缩。这通常显示何时需要将过滤器映射到某些内容类型(例如图像)或所有请求。
回答by Satyam
A HandlerInterceptor gives you more fine-grained control than a filter, because you have access to the actual target "handler" - this means that whatever action you perform can vary depending on what the request is actually doing (whereas the servlet filter is generically applied to all requests - only able to take into account the parameters of each request). The handlerInterceptor also provides 3 different methods, so that you can apply behavior prior to calling a handler, after the handler has completed but prior to view rendering (where you may even bypass view rendering altogether), or after the view itself has been rendered. Also, you can set up different interceptors for different groups of handlers - the interceptors are configured on the handlerMapping, and there may be multiple handlerMappings.
HandlerInterceptor 为您提供了比过滤器更细粒度的控制,因为您可以访问实际的目标“处理程序”——这意味着您执行的任何操作都可以根据请求实际执行的操作而有所不同(而 servlet 过滤器通常应用于到所有请求 - 只能考虑每个请求的参数)。handlerInterceptor 还提供了 3 种不同的方法,以便您可以在调用处理程序之前、在处理程序完成之后但在视图渲染之前(您甚至可以完全绕过视图渲染)或在视图本身被渲染之后应用行为。另外,你可以为不同的handlers组设置不同的拦截器——拦截器是在handlerMapping上配置的,可能有多个handlerMappings。
Therefore, if you have a need to do something completely generic (e.g. log all requests), then a filter is sufficient - but if the behavior depends on the target handler or you want to do something between the request handling and view rendering, then the HandlerInterceptor provides that flexibility.
因此,如果您需要做一些完全通用的事情(例如记录所有请求),那么过滤器就足够了 - 但是如果行为取决于目标处理程序,或者您想在请求处理和视图呈现之间做一些事情,那么HandlerInterceptor 提供了这种灵活性。
Reference: http://static.springframework.org/sp...ng-interceptor
回答by Manas
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 出站响应。
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 sophisticated behavior because can access to all Spring context.
拦截器: - Spring 拦截器类似于 Servlet 过滤器,但它们在 Spring 上下文中起作用,因此在管理 HTTP 请求和响应方面非常强大,但它们可以实现更复杂的行为,因为可以访问所有 Spring 上下文。