【问题标题】:Equivalent of org.springframework.boot.context.embedded.FilterRegistrationBean for non-Boot Spring project?等效于非 Boot Spring 项目的 org.springframework.boot.context.embedded.FilterRegistrationBean?
【发布时间】:2015-04-30 18:38:26
【问题描述】:

我正在尝试按照this tutorial 在 Spring 中实现外部会话处理。不过,我在添加正确的过滤器时遇到了一些麻烦。 Spring Boot 似乎已经定义了正确的 bean/filter,但我的项目不是 Spring Boot,所以它找不到 FilterRegistrationBean。在 Spring 的非 Boot 版本中是否有某种与此类等效的东西?我也尝试了org.springframework.web.context.embedded.FilterRegistrationBean,但无法正确导入(看起来此文档引用了 SNAPSHOT 版本,因此该软件包可能从未包含在正确版本中)。

【问题讨论】:

    标签: spring spring-mvc servlet-filters spring-bean spring-session


    【解决方案1】:

    根据您的容器配置、web.xml 或ServletContainerInitializer,您可以注册一个DelegatingFilterProxy 过滤器并使其按名称引用您在ApplicationContext 中声明的Filter bean。

    【讨论】:

    • 有道理,但我仍然需要一个Filter bean,这似乎有点像重新发明轮子来编写我自己的Filter类。
    • @JosephBlair 我不明白。如果你想要你自己的Filter,你需要写你自己的Filter。但是,由于Filters 是由 Servlet 容器管理的,所以不能让它成为 Spring bean。 Spring 的解决方案是将 Filter 声明为 bean,然后将 DelegatingFilterProxy 注册为 Servlet <filter>,然后委托给在根应用程序上下文中找到的 Filter bean。我链接到的 javadoc 解释了这一切。
    • 我明白所有这些,但我真的不想要“我自己的”Filter,我只想要FilterRegistrationBean,因为它已经实现了所有逻辑而不是尝试复制它在自定义创建的 Filter 类中的作用。但是,我不能使用它,因为我的项目不是 Spring Boot,所以我正在寻找可以使用的等效项。
    • @JosephBlair DelegatingFilterProxy 是等价的。它不是被声明为 bean,而是在 XML (web.xml) 中或通过 ServletContextWebApplicationInitializer 中声明为 Servlet <filter>
    • @JosephBlair 通常你会有两个上下文,a root and a servlet context。根上下文由ContextLoaderListener 加载。 servlet 上下文由DispatcherServlet 加载。您的 Filter bean 应该在根上下文中声明。这就是DelegatingFilterProxy 会寻找它的地方。
    猜你喜欢
    • 1970-01-01
    • 2017-07-30
    • 1970-01-01
    • 1970-01-01
    • 2019-10-06
    • 2018-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多