gpt4 book ai didi

Spring HandlerInterceptor vs Servlet 过滤器

转载 作者:IT老高 更新时间:2023-10-28 13:01:55 24 4
gpt4 key购买 nike

HandlerInterceptor Spring 中的 s 现在可以使用 <mvc:interceptors> 配置为仅在某些 URL 上调用.

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

我认为对于拦截器,可以使用 ModelAndView对象与模型一起工作,因此它具有更多优势。谁能画出过滤器或拦截器比另一个有优势的场景?

最佳答案

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

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.

关于Spring HandlerInterceptor vs Servlet 过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8000844/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com