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

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

问题描述

我对 FilterInterceptor 的用途有点困惑.

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

我从文档中了解到,Interceptor 在请求之间运行.另一方面 Filter 在渲染视图之前运行,但在控制器渲染响应之后运行.

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中的postHandle()和Filter中的doFilter()的区别在哪里?

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

应该在哪些用例中使用它的最佳实践是什么?在这张图片中,Filters 和 Interceptors 在哪里工作?

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

推荐答案

来自 HandlerIntercepterjavadoc:

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.

doFilterpostHandle 更通用.您可以更改请求或响应并将其传递给链,甚至阻止请求处理.

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天全站免登陆