在我的 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 代码中。
如有任何帮助,我们将不胜感激,谢谢
最佳答案
我找到了解决方案。我更改了 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
关于java - Spring Boot 过滤器始终映射到/*,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49647905/