自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Spring Boot過濾器的那些事:從配置到應(yīng)用場景

開發(fā) 前端
過濾器由 Servlet 提供,基于函數(shù)回調(diào)實(shí)現(xiàn)鏈?zhǔn)綄W(wǎng)絡(luò)請求和響應(yīng)的攔截與修改。由于基于 Servlet,幾乎可以攔截 Web 服務(wù)器管理的所有資源(JSP、圖片文件、HTML 文件、CSS 文件等)。

1. 什么是過濾器

過濾器是 Web 三大組件之一,也是項(xiàng)目中常用的工具。本文主要介紹過濾器的概念及在 Spring Boot 中的常用使用方法。

過濾器由 Servlet 提供,基于函數(shù)回調(diào)實(shí)現(xiàn)鏈?zhǔn)綄W(wǎng)絡(luò)請求和響應(yīng)的攔截與修改。由于基于 Servlet,幾乎可以攔截 Web 服務(wù)器管理的所有資源(JSP、圖片文件、HTML 文件、CSS 文件等)。

定義過濾器需要實(shí)現(xiàn)javax.servlet.Filter接口。過濾器不是 Servlet,無法直接生成對客戶端的響應(yīng),只能攔截已有請求并預(yù)處理不需要或不一致的信息資源。

2. 過濾器流程原理

圖片

在 Web 應(yīng)用中,可以開發(fā)編寫多個過濾器,這些過濾器組合稱為過濾器鏈。用戶發(fā)起請求后,請求信息會按過濾器鏈中過濾器的順序依次進(jìn)入每個過濾器。經(jīng)過每層過濾器時,需通過過濾器的校驗(yàn)邏輯并放行才能進(jìn)入下一層過濾器,直至服務(wù)器獲取資源。

服務(wù)器成功獲取資源并響應(yīng)過濾器后,會按反向順序經(jīng)過層層過濾器,最終響應(yīng)用戶。

3. 過濾器分類

Servlet 2.5:

  • REQUEST: 用戶直接訪問頁面時,WEB 容器會調(diào)用過濾器鏈。
  • FORWARD: 通過 RequestDispatcher 的 forward 訪問目標(biāo)資源時調(diào)用此過濾器。
  • INCLUDE: 通過 RequestDispatcher 的 include 方法調(diào)用目標(biāo)資源時調(diào)用。
  • ERROR: 通過聲明式異常處理機(jī)制調(diào)用目標(biāo)資源時調(diào)用過濾器鏈。

Servlet 3.0:

  • ASYNC: 支持異步處理。

4. 過濾器中需要實(shí)現(xiàn)的方法

  • public void doFilter(ServletRequest, ServletResponse, FilterChain): 實(shí)現(xiàn)實(shí)際過濾操作。當(dāng)客戶端請求方法與過濾器設(shè)置的 URL 匹配時,Servlet 容器會先調(diào)用過濾器的 doFilter 方法。FilterChain 用于訪問后續(xù)過濾器。
  • public void init(FilterConfig filterConfig): Web 應(yīng)用啟動時,Web 服務(wù)器創(chuàng)建過濾器實(shí)例對象并調(diào)用其 init 方法完成對象初始化(過濾器對象僅創(chuàng)建一次,init 方法僅執(zhí)行一次)。開發(fā)者可通過 init 方法參數(shù)執(zhí)行讀取配置文件等初始化操作。
  • public void destroy(): Servlet 容器銷毀過濾器實(shí)例前調(diào)用此方法。用于釋放過濾器占用的資源。

5. 創(chuàng)建過濾器的兩種方式

方法一:注解方式

創(chuàng)建步驟:

  • 實(shí)現(xiàn)Filter接口,添加@WebFilter@Order注解配置過濾器:
@Order(1)
@WebFilter(filterName = "myFilter", urlPatterns = {"*"})
public class MyCustomFilter1 implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {}

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) {}

    @Override
    public void destroy() {}
}
  • 在啟動類添加@ServletComponentScan注解:
@SpringBootApplication
@ServletComponentScan
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

方法二:將過濾器注冊為 Bean

創(chuàng)建步驟:

  • 創(chuàng)建實(shí)現(xiàn) Filter 接口的過濾器類:
public class MyCustomFilter2 implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {}

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain){}

    @Override
    public void destroy() {}
}
  • 創(chuàng)建過濾器配置類,注冊過濾器:
@Configuration
public class FilterConfig {

    @Bean
    public FilterRegistrationBean customFilter(){
        FilterRegistrationBean<MyCustomFilter1> filterBean = new FilterRegistrationBean<>();
        filterBean.setFilter(new MyCustomFilter1());
        filterBean.setName("FilterController");
        filterBean.addUrlPatterns("/*");
        return filterBean;
    }
}

6. 注解方式的參數(shù)說明與使用示例

(1)參數(shù)說明

@WebFilter可配置多個參數(shù),部分參數(shù)說明如下:

參數(shù)名稱

參數(shù)類型

參數(shù)描述

filterName

String

過濾器的名稱

displayName

String

要顯示的過濾器名稱

asyncSupported

boolean

設(shè)置過濾器是否支持異步模式

initParams

WebInitParam[]

你可以在初始化時配置一些參數(shù)

servletNames

String[]

設(shè)置要過濾的 Servlets

urlPatterns

String[]

指定要攔截的路徑

value

String[]

urlPatterns 屬性與 urlPatterns 屬性的作用相同,兩者都指定要攔截的路徑

dispatcherTypes

DispatcherType[]

設(shè)置過濾器過濾的請求類型,支持以下屬性:ASYNC, ERROR, FORWARD, INCLUDE, REQUEST(默認(rèn)情況下,它過濾所有類型的請求)

(2)使用示例

  • 創(chuàng)建 Controller 類
@RestController
publicclass TestController {

    @GetMapping("/a/hello")
    public String hello1(){
        return"hello world! a";
    }

    @GetMapping("/b/hello")
    public String hello2(){
        return"hello world! b";
    }

    @GetMapping("/c/hello")
    public String hello3(){
        return"hello world! c";
    }
}
  • 創(chuàng)建過濾器類
@Order(1)
@WebFilter(filterName = "myFilter", urlPatterns = {"/a/*", "/b/*"}, description = "自定義過濾器")
publicclass MyCustomFilter2 implements Filter {

    @Override
    public void init(FilterConfig filterConfig) {
        System.out.println("過濾器初始化");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 
        throws IOException, ServletException {
        System.out.println("請求處理前...");
        chain.doFilter(request, response);  // 放行請求
        System.out.println("請求處理后...");
    }

    @Override
    public void destroy() {
        System.err.println("過濾器銷毀");
    }
}
  • 創(chuàng)建啟動類
@SpringBootApplication
@ServletComponentScan
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

7. 過濾器注冊 Bean 方式的參數(shù)說明與使用示例

(1)方法參數(shù)說明

方法名稱

方法描述

setName()

設(shè)置過濾器的名稱。

setFilter()

設(shè)置要注冊的過濾器。

setOrder()

設(shè)置過濾器的順序位置。

setAsyncSupported()

設(shè)置過濾器是否支持異步模式。

addUrlPatterns()

添加攔截的路徑。

setUrlPatterns()

設(shè)置攔截的路徑。

addServletNames()

添加過濾器的 servlet 名稱。

setServletNames()

設(shè)置注冊過濾器的 servlet 名稱。

setInitParameters()

設(shè)置初始化參數(shù)。

addInitParameter()

添加初始化參數(shù)。

setMatchAfter()

設(shè)置是否在 Servlet 上下文中聲明的任何過濾器映射之后匹配過濾器映射。

setDispatcherTypes()

設(shè)置過濾器過濾的請求類型。支持的屬性如下:ASYNC, ERROR, FORWARD, INCLUDE, REQUEST(默認(rèn)情況下,它過濾所有類型的請求)。

(2)使用示例

  • 創(chuàng)建 Controller 類
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
publicclass TestController {

    @GetMapping("/a/hello")
    public String hello1(){
        return"hello world! a";
    }

    @GetMapping("/b/hello")
    public String hello2(){
        return"hello world! b";
    }

    @GetMapping("/c/hello")
    public String hello3(){
        return"hello world! c";
    }

}
  • 創(chuàng)建過濾器類
public class MyCustomFilter1 implements Filter {

    @Override
    public void init(FilterConfig filterConfig) {
        System.out.println("過濾器初始化");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 
        throws IOException, ServletException {
        System.out.println("請求處理前...");
        chain.doFilter(request, response);  // 放行請求
        System.out.println("請求處理后...");
    }

    @Override
    public void destroy() {
        System.err.println("過濾器銷毀");
    }
}
  • 創(chuàng)建過濾器配置類
@Configuration
public class FilterConfig {

    @Bean
    public FilterRegistrationBean customFilter(){
        FilterRegistrationBean<MyCustomFilter1> filterBean = new FilterRegistrationBean<>();
        filterBean.setFilter(new MyCustomFilter1());
        filterBean.setName("FilterController");
        filterBean.addUrlPatterns("/c/*","/b/*");
        return filterBean;
    }
}
  • 創(chuàng)建啟動類
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

@SpringBootApplication
@ServletComponentScan
public class Application {

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

}

8. 過濾器使用場景

(1)日志記錄

過濾器可記錄請求和響應(yīng)的關(guān)鍵信息,便于排查問題和分析系統(tǒng)運(yùn)行狀態(tài)。例如記錄請求 URL、參數(shù)、響應(yīng)狀態(tài)碼等。

(2)數(shù)據(jù)統(tǒng)計(jì)

用于采集各類數(shù)據(jù)指標(biāo),如統(tǒng)計(jì)請求次數(shù)、響應(yīng)時間分布等,為系統(tǒng)優(yōu)化提供依據(jù)。

(3)數(shù)據(jù)格式轉(zhuǎn)換

不同系統(tǒng)交互時,過濾器可轉(zhuǎn)換數(shù)據(jù)格式,例如將 JSON 轉(zhuǎn)為 XML。

(4)為數(shù)據(jù)設(shè)置默認(rèn)值

檢查輸入數(shù)據(jù),為缺失字段設(shè)置默認(rèn)值,保證數(shù)據(jù)完整性。

(5)權(quán)限認(rèn)證、黑白名單

實(shí)現(xiàn)用戶權(quán)限認(rèn)證和訪問控制,限制特定 IP 或用戶的訪問。

(6)數(shù)據(jù)加解密、簽名驗(yàn)證

對敏感數(shù)據(jù)加解密以保證安全,同時進(jìn)行簽名驗(yàn)證確保數(shù)據(jù)完整性。

責(zé)任編輯:武曉燕 來源: 程序猿技術(shù)充電站
相關(guān)推薦

2020-10-29 07:16:26

布隆過濾器場景

2023-04-26 08:32:45

Redis布隆過濾器

2009-06-18 10:13:00

Hibernate過濾

2009-07-08 16:07:04

Servlet過濾器配

2021-01-14 08:13:39

Spring Clou應(yīng)用內(nèi)置過濾器

2022-02-16 23:58:41

Spring過濾器驗(yàn)證碼

2024-10-09 15:54:38

布隆過濾器函數(shù)

2017-04-12 14:43:01

Spring ClouZuul過濾器

2023-01-26 01:41:27

核心全局過濾器

2021-07-05 15:22:03

Servlet過濾器客戶端

2022-11-28 11:45:30

Go應(yīng)用場景

2022-02-21 23:58:49

Spring過濾器順序值

2024-01-05 09:04:35

隆過濾器數(shù)據(jù)結(jié)構(gòu)哈希函數(shù)

2009-07-08 17:33:37

Servlet過濾器

2024-11-04 08:45:48

布隆過濾器元數(shù)據(jù)指紋值

2017-05-04 22:30:17

Zuul過濾器微服務(wù)

2009-07-08 15:30:56

Servlet過濾器

2009-09-29 13:55:23

Hibernate設(shè)置

2009-07-14 09:09:08

Swing模型過濾器

2011-06-29 16:14:59

Qt 事件 過濾器
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號