gpt4 book ai didi

java - GenericFilterBean vs OncePerRequestFilter 何时分别使用?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:27:45 24 4
gpt4 key购买 nike

我已经发现了一些与此主题相关的问题,但我发现没有一个问题能阐明此主题。

显然 OncePerRequestFilter 确保请求在过滤器链中仅通过一次过滤器,但我不清楚何时会发生相反的情况。

很高兴看到在某些场景中使用其中一种。还有一个关于何时以及如何在过滤器链上多次应用过滤器的示例。

例如

  1. 对于 JWT 过滤器应该使用哪种实现,为什么?
  2. 对于 CORS 过滤器应该使用哪种实现,为什么?等

最佳答案

OncePerRequestFilter 状态的 javadoc

As of Servlet 3.0, a filter may be invoked as part of a javax.servlet.DispatcherType REQUEST or javax.servlet.DispatcherType ASYNC dispatches that occur in separate threads. A filter can be configured in web.xml whether it should be involved in async dispatches. However, in some cases servlet containers assume different default configuration. Therefore sub-classes can override the method shouldNotFilterAsyncDispatch() to declare statically if they should indeed be invoked, once, during both types of dispatches in order to provide thread initialization, logging, security, and so on. This mechanism complements and does not replace the need to configure a filter in web.xml with dispatcher types.

所以它是在 Spring 中实现的一个额外的“安全”特性,以确保无论环境如何,事情都能正常工作。如果您查看扩展它的类,您会发现有很多;包括 CorsFilter。不确定是否有 扩展它的 Spring 过滤器,可能没有。

关于java - GenericFilterBean vs OncePerRequestFilter 何时分别使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50410901/

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