<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Spring Boot 使用 Filter 的正確姿勢!

          共 5393字,需瀏覽 11分鐘

           ·

          2021-09-18 11:54

          點擊關注公眾號,Java干貨及時送達

          Filter 是 JavaEE 中 Servlet 規(guī)范的一個組件,位于包javax.servlet 中,它可以在 HTTP 請求到達 Servlet 之前,被一個或多個Filter處理。

          它的工作流程如圖:

          Filter的這個特性在生產(chǎn)環(huán)境中有很廣泛的應用,如:修改請求和響應、防止xss攻擊、包裝二進制流使其可以多次讀,等等。

          實際工作中,我們都是使用 SpringBoot 進行業(yè)務開發(fā),本文總結(jié)三種 Filter 用法,SpringBoot 版本采用目前最新的 v2.3.1.RELEASE

          1. 編寫Filter

          要編寫 Filter ,只需要實現(xiàn)javax.servlet.Filter接口就可以了

          public class MyFilter implements Filter {
              @Override
              public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
                  System.out.println("MyFilter");
                  // 要繼續(xù)處理請求,必須添加 filterChain.doFilter()
                  filterChain.doFilter(servletRequest,servletResponse);
              }
          }

          Filter 接口有三個方法:init(),doFilter(),destroy()

          其中doFilter()需要自己實現(xiàn),其余兩個是default的,可以不用實現(xiàn)。

          Spring Boot 基礎就不介紹了,推薦下這個實戰(zhàn)教程:https://github.com/javastacks/spring-boot-best-practice

          注意:如果Filter要使請求繼續(xù)被處理,就一定要調(diào)用filterChain.doFilter()!


          2. 配置Filter被 Spring 管理

          讓自定義的 Filter 被 Spring 的 IOC 容器管理,有三種實現(xiàn)方式,各有優(yōu)缺點。下面課代表給大家總結(jié)一下:

          1. 使用@Component+@Order

          在剛剛定義的MyFilter類上加上@Component和@Order注解,即可被Spring管理

          @Component
          @Order(1)
          public class MyFilter implements Filter {
              @Override
              public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
                  System.out.println("MyFilter");
                  // 要繼續(xù)處理請求,必須添加 filterChain.doFilter()
                  filterChain.doFilter(servletRequest,servletResponse);
              }
          }

          沒錯就這么簡單,這樣 MyFilter 就生效了,寫個Controller 調(diào)用一下就可以看到效果。

          當有多個Filter時,這里的@Order(1)注解會指定執(zhí)行順序,數(shù)字越小,越優(yōu)先執(zhí)行,如果只寫@Order,默認順序值是Integer.MAX_VALUE

          @Component + @Order 注解方式配置簡單,支持自定義 Filter 順序。缺點是只能攔截所有URL,不能通過配置去攔截指定的 URL。

          2.@WebFilter+@ServletComponentScan

          MyFilter上添加@WebFilter注解,并在啟動類上增加@ServletComponentScan("com.zhengxl.filterdemo.filter")注解,參數(shù)就是Filter所在的包路徑,相當于告訴 SpringBoot,去哪里掃描 Filter

          @WebFilter(urlPatterns = "/*")
          public class MyFilter implements Filter {
              @Override
              public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
                  System.out.println("MyFilter");
                  // 要繼續(xù)處理請求,必須添加 filterChain.doFilter()
                  filterChain.doFilter(servletRequest,servletResponse);
              }
          }
          @SpringBootApplication
          @ServletComponentScan("com.zhengxl.filterdemo.filter")
          public class FilterDemoApplication {

              public static void main(String[] args) {
                  SpringApplication.run(FilterDemoApplication.class, args);
              }

          }

          @WebFilter+@ServletComponentScan 注解方式支持對 Filter 匹配指定URL,但是不支持指定 Filter 的執(zhí)行順序。

          3. JavaConfig 配置方式

          @Configuration
          public class FilterConfig {
              @Bean
              public FilterRegistrationBean registerMyFilter(){
                  FilterRegistrationBean<MyFilter> bean = new FilterRegistrationBean<>();
                  bean.setOrder(1);
                  bean.setFilter(new MyFilter());
                  // 匹配"/hello/"下面的所有url
                  bean.addUrlPatterns("/hello/*");
                  return bean;
              }
              @Bean
              public FilterRegistrationBean registerMyAnotherFilter(){
                  FilterRegistrationBean<MyAnotherFilter> bean = new FilterRegistrationBean<>();
                  bean.setOrder(2);
                  bean.setFilter(new MyAnotherFilter());
                  // 匹配所有url
                  bean.addUrlPatterns("/*");
                  return bean;
              }
          }

          通過 Java 代碼顯式配置 Filter ,功能強大,配置靈活。只需要把每個自定義的 Filter 聲明成 Bean 交給 Spring 管理即可,還可以設置匹配的 URL 、指定 Filter 的先后順序。

          3. 三種方式對比

          以上介紹完 SpringBoot 中三種 Filter的使用姿勢,非常簡單,下面列個表格總結(jié)一下:

          使用方式排序指定URL
          @Component
          @Order
          10
          @WebFilter
          @ServletComponentScan
          01
          JavaConfig11

          實際使用過程中,可以按照業(yè)務需求選擇合適的使用方式,比如:如果編寫的過濾器要攔截所有請求,不需要指定URL,那選擇最簡單的 @Component+@Order 就非常合適。

          PS:其實還有第四種,web.xml配置,不過這都2020年了,用 SpringBoot 的自動裝配或者 JavaConfig 不是更方便嗎?






          關注Java技術棧看更多干貨



          獲取 Spring Boot 實戰(zhàn)筆記!
          瀏覽 51
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  亚洲综合娱乐网 | 亚洲色无码A片中文字幕 | 九九九在线视频观看 | 青青草视频在线网站 | 理论在线观看视频 |