本文介绍了在 spring boot 上添加一个带有 urlMapping 的过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堂这样的课

@WebFilter(urlPatterns={"/message/*","/private_message"})
@Component
public class TokenAuthenticationFilter extends FilterRegistrationBean {

而且我认为 Spring Boot 会尊重 urlPatterns 设置,但出于某种原因,每个请求都会到达这个过滤器.我想我可能做错了什么.
另外,我没有使用 Spring Security

and I thought that Spring Boot would respect the urlPatterns setted, but for some reason, every request gets to this filter. I think I may be doing something wrong.
Also, I'm not using Spring Security

推荐答案

您混合使用了 @WebFilterFilterRegistrationBean.前者应该用在 Filter 的类上,并与 @ServletComponentScan 结合使用.后者应该作为 bean 发布(正如您通过使用 @Component 进行注释所做的那样)和使用其 setUrlPatterns 方法配置的 url 映射.

You're mixing use of @WebFilter and FilterRegistrationBean. The former should be used on a class that is a Filter and in conjunction with @ServletComponentScan. The latter should be published as a bean (as you are doing by annotating with @Component) and the url mappings configured using its setUrlPatterns method.

这篇关于在 spring boot 上添加一个带有 urlMapping 的过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 17:50