【问题标题】:Spring boot filter always mapped to /*Spring Boot 过滤器始终映射到 /*
【发布时间】:2018-09-13 19:58:43
【问题描述】:

在我的 Spring Boot 应用程序中,我有以下过滤器类

public class UserIdAuthenticationFilter implements Filter {
    // all the handling
}

我没有在这个类上使用任何注释,我使用 applicationContext.xml 来定义这个 bean,如下所示:

<bean id="filterChainProxy" class="org.springframework.security.web.FilterChainProxy">
    <security:filter-chain-map request-matcher="ant">
                <security:filter-chain  pattern="/lms/s/reward/*" filters="UIDAuthenticationFilter"/>
    </security:filter-chain-map>
</bean>
<bean id="UIDAuthenticationFilter" class="com.mycompany.security.filter.UserIdAuthenticationFilter" >
</bean>

我创建了另一个用于加载此 xml 配置的类:

@Configuration
@ImportResource({"classpath:applicationContext.xml"})
public class XMLConfiguration {
}

现在,当我启动我的应用程序时,我可以在日志中看到过滤器正在从 applicationContext.xml 加载:

15:06:01.516 [localhost-startStop-1] INFO - Mapping filter: 'UIDAuthenticationFilter' to: [/*]

明确的说过滤器映射到/* 所以每个请求都会调用过滤器,我不想要这种行为,我希望我的过滤器应该只为我在 applicationContext.xml 中提供的模式调用。 (/lms/s/reward/*)

我也不想要this 解决方案。因为我必须在applicationContext.xml中配置过滤器,而不是在java代码中。

任何帮助将不胜感激,谢谢

【问题讨论】:

  • 是的,您确实想要this 解决方案...它是用Java 编写的,并不意味着您不能在XML 中翻译它...只需为FilterRegistration 定义一个bean豆子。
  • @M.Deinum,谢谢。工作:)

标签: java spring spring-boot filter


【解决方案1】:

我得到了解决方案。我已将 applicationContext.xml 更改如下:

<bean id="MyFilter" class="com.mycompany.security.filter.MyFilter">
</bean>
<bean id="filterRegistrationBean"
    class="org.springframework.boot.web.servlet.FilterRegistrationBean">
    <property name="filter" ref="MyFilter">
    </property>
    <property name="urlPatterns" value="/lms/s/*">
    </property>
</bean>

现在我的过滤器被映射到/lms/s/*,非常感谢@M.Deinum

【讨论】:

  • 谢谢。我添加了一些关键字来帮助后代。 OncePerRequestFilter 实现 Ordered org.springframework.boot.web.servlet.FilterRegistrationBean 自定义过滤器
猜你喜欢
  • 1970-01-01
  • 2018-07-12
  • 2019-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-17
  • 2019-05-19
  • 2015-05-22
相关资源
最近更新 更多