我的springboot项目正常使用
此时我想注册Filter,我发现我需要同时使用@Component和@WebFilter, 请求才会被这个过滤器拦截
@Component
@WebFilter(urlPatterns = "/*", filterName = "xssFilter")
public class XssFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
Filter.super.init(filterConfig);
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
FilterChain filterChain) throws IOException, ServletException {
filterChain.doFilter(servletRequest, servletResponse);
}
@Override
public void destroy() {
Filter.super.destroy();
}
}
但是我发现网上的一些博客的springboot有些都是只写了@WebFilter,也可以注册成filter,有点不解
然后我又写了个普通的SSM项目,不用Springboot,发现加不加@Component都一样可以走用@WebFilter注解注册的过滤器
然后我又试了重新写Springboot的项目,但是跟之前不一样,我排除了Springboot内置的tomcat,使用外置tomcat运行
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
此时我同时加上@Component与@WebFilter, 发生报错
多次注册filter?
Failed to register 'filter xssFilter' on the servlet context. Possibly already registered?
然后此时我把@Component去掉,只是用@WebFilter,程序正常运行,而且请求也走了过滤器
我看了网上的很多帖子,都还是理解不了
求解惑