SpringBoot 的过滤器 Filter 配置的三种方式

SpringBoot 过滤器配置有三种方式

1. @ServletComponentScan + @WebFilter, 可配置过滤路径, 但没有顺序(顺序是由过滤器命名决定)

在启动类上使用 @ServletComponentScan, 在过滤器类上使用 @WebFilter(urlPatterns = {"/test/path"})

1@SpringBootApplication 2@ServletComponentScan 3public class ServicemapServicecenterBackendApplication { 4 5 public static void main(String[] args) { 6 SpringApplication.run(ServicemapServicecenterBackendApplication.class, args); 7 } 8} 9 10@WebFilter(urlPatterns = {"/test"}) 11public class FilterTwo extends OncePerRequestFilter { 12 13 @Override 14 protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { 15 System.out.println("************************** FilterTwo **************************"); 16 filterChain.doFilter(request, response); 17 } 18 19}

2. @Component + @Order(100), 不可配置过滤路径, 可以通过@Order配置顺序

在过滤器类上使用 @Component , @Order(100) 数值越小优先级越高

1@Component 2@Order(100) 3public class FilterOne extends OncePerRequestFilter { 4 5 @Override 6 protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { 7 System.out.println("************************** FilterOne **************************"); 8 filterChain.doFilter(request, response); 9 } 10}

3. @SpringBootConfiguration + @Order, 可配置过滤路径, 可以通过@Order配置顺序, 也可以在代码中setOrder(100)配置 (推荐方式)

1@Order(100) 2public class FilterOne extends OncePerRequestFilter { 3 4 @Override 5 protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { 6 System.out.println("************************** FilterOne **************************"); 7 filterChain.doFilter(request, response); 8 } 9} 10 11@SpringBootConfiguration 12public class FilterConfig { 13 14 @Bean 15 public FilterRegistrationBean<FilterOne> filterOneRegister() { 16 FilterRegistrationBean<FilterOne> filterRegistrationBean = new FilterRegistrationBean<>(); 17 filterRegistrationBean.setFilter(new FilterOne()); 18 filterRegistrationBean.addUrlPatterns("/*"); 19// filterRegistrationBean.setOrder(100); 20 return filterRegistrationBean; 21 } 22}
点赞
收藏

评论区

加载中...

相关推荐

springcloud gateway高级功能之根据参数自定义路由Predicate

背景我们使用了springcloudgateway作为也给路由转发功能,由于历史遗留问题,不仅仅需要根据path转发,还需要根据get或者post中的参数进行转发解决方案这里我们使用自定义的Predicate进行转发简介这里简单介绍下相关术语(1)Filter(过滤器):和Zuul的过滤器在概念上类似,可以使

SpringMVC的拦截器(Interceptor)和过滤器(Filter)的区别与联系

一简介(1)过滤器:依赖于servlet容器。在实现上基于函数回调,可以对几乎所有请求进行过滤,但是缺点是一个过滤器实例只能在容器初始化时调用一次。使用过滤器的目的是用来做一些过滤操作,获取我们想要获取的数据,比如:在过滤器中修改字符编码;在过滤器中修改HttpServletRequest的一些参数,包括:过滤低俗文字、危险字符等关于

SpringBoot学习之路:09.Spring Boot中添加Filter应用

   上篇文章中说了SpringBoot中是如何使用servlet的,本文将讲解在SpringBoot中对过滤器Filter的实现一.编写MyFilter实现Filter接口packagecom.maxbill.core.webbox.filter;importorg.apache.log4j

Eclipse搭建springboot项目(八)拦截器、过滤器、监听器

知识点:1、SpringBoot2.x过滤器Filter和使用Servlet3.0配置自定义Filter(核心知识)  filter简单理解:人检票员(filter)景点  1)SpringBoot启动默认加载的Filter    characterEncodingFilter    

SpringBoot学习之路:09.Spring Boot中添加Filter应用

   上篇文章中说了SpringBoot中是如何使用servlet的,本文将讲解在SpringBoot中对过滤器Filter的实现一.编写MyFilter实现Filter接口packagecom.maxbill.core.webbox.filter;importorg.apache.log4j

Spring MVC 自带的字符编码过滤器以及Tomcat字符编码设置,彻底解决中文参数乱码问题

一、SpringMVC字符编码配置javaWeb项目添加Spring支持后,可使用Spring自带的字符编码过滤器。源码在springweb4.1.0.RELEASE.jar包下的org.springframework.web.filter目录的CharacterEncodingFilter.java。在web.xml文件中配置:

SpringBoot 的过滤器 Filter 配置的三种方式 - HelloWorld