国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Spring boot 中的過濾器

這篇具有很好參考價值的文章主要介紹了Spring boot 中的過濾器。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

`javax.servlet.Filter`接口定義了幾個方法:

其中一些經(jīng)常在過濾器的實現(xiàn)中使用。以下是常用的幾個方法:

1. `doFilter()`: 這是過濾器的核心方法,用于實現(xiàn)過濾器的邏輯。在該方法中,您可以對請求進行預處理、修改請求參數(shù)、驗證身份、記錄日志等操作,然后通過調(diào)用`FilterChain`的`doFilter()`方法將請求傳遞給下一個過濾器或目標資源。在響應返回客戶端之前,也可以在此方法中執(zhí)行一些操作。

```java
void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
? ? throws IOException, ServletException;
```

2. `init()`: `init()`方法在過濾器初始化時調(diào)用,用于執(zhí)行一些初始化操作。這個方法在過濾器的生命周期中只會被調(diào)用一次。您可以在這里進行一些設置和準備工作。

```java
void init(FilterConfig filterConfig) throws ServletException;
```

3. `destroy()`: `destroy()`方法在過濾器被銷毀時調(diào)用,用于釋放資源和執(zhí)行一些清理操作。這個方法在過濾器的生命周期結束時被調(diào)用,通常在應用程序關閉時。

```java
void destroy();
```

這些方法提供了過濾器的基本功能和生命周期管理。您可以根據(jù)需要在這些方法中編寫自定義邏輯來處理請求和響應。

除了上述方法外,`FilterConfig`接口還提供了其他一些方法,用于獲取過濾器的初始化參數(shù)和配置信息。例如:

- `getFilterName()`: 獲取過濾器的名稱。
- `getInitParameter(String name)`: 獲取指定名稱的初始化參數(shù)值。
- `getInitParameterNames()`: 獲取所有初始化參數(shù)的名稱。

這些方法可以在`init()`方法中使用,以獲取過濾器的配置信息。


實例1:自定義過濾器來驗證身份:

在Spring Boot中,您可以使用自定義過濾器來驗證身份。以下是一個示例,演示如何在過濾器中實現(xiàn)身份驗證邏輯:

import org.springframework.util.StringUtils;
import org.springframework.web.filter.GenericFilterBean;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class AuthenticationFilter extends GenericFilterBean {

? ? @Override
? ? public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
? ? ? ? ? ? throws IOException, ServletException {
? ? ? ? HttpServletRequest httpRequest = (HttpServletRequest) request;
? ? ? ? HttpServletResponse httpResponse = (HttpServletResponse) response;

? ? ? ? // 從請求頭中獲取授權信息
? ? ? ? String authorizationHeader = httpRequest.getHeader("Authorization");

? ? ? ? // 檢查授權信息是否存在且以Bearer開頭
? ? ? ? if (StringUtils.hasText(authorizationHeader) && authorizationHeader.startsWith("Bearer ")) {
? ? ? ? ? ? // 提取令牌
? ? ? ? ? ? String token = authorizationHeader.substring(7);

? ? ? ? ? ? // 在此處執(zhí)行身份驗證邏輯
? ? ? ? ? ? if (validateToken(token)) {
? ? ? ? ? ? ? ? // 身份驗證成功,繼續(xù)處理請求
? ? ? ? ? ? ? ? chain.doFilter(request, response);
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? // 身份驗證失敗,返回未授權錯誤
? ? ? ? ? ? ? ? httpResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
? ? ? ? ? ? }
? ? ? ? } else {
? ? ? ? ? ? // 授權信息不合法,返回未授權錯誤
? ? ? ? ? ? httpResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
? ? ? ? }
? ? }

? ? private boolean validateToken(String token) {
? ? ? ? // 在此處進行身份驗證邏輯,例如驗證JWT的有效性
? ? ? ? // 如果驗證通過,返回true;否則返回false
? ? ? ? // 根據(jù)實際情況進行實現(xiàn)
? ? ? ? // ...
? ? }
}

在上述示例中,我們創(chuàng)建了一個自定義的過濾器`AuthenticationFilter`,繼承了`GenericFilterBean`類。在`doFilter()`方法中,我們獲取請求頭中的授權信息,并驗證授權信息是否合法。

在`validateToken()`方法中,您可以執(zhí)行實際的身份驗證邏輯,例如驗證JWT的有效性、檢查數(shù)據(jù)庫中的用戶憑證等。根據(jù)實際情況進行實現(xiàn)。

如果身份驗證成功,我們調(diào)用`chain.doFilter(request, response)`繼續(xù)處理請求。如果身份驗證失敗,我們設置響應狀態(tài)為`SC_UNAUTHORIZED`表示未授權。

要將該過濾器應用于Spring Boot應用程序,您需要在配置類中注冊該過濾器:

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FilterConfig {

? ? @Bean
? ? public FilterRegistrationBean<AuthenticationFilter> authenticationFilterRegistration() {
? ? ? ? FilterRegistrationBean<AuthenticationFilter> registrationBean = new FilterRegistrationBean<>();
? ? ? ? registrationBean.setFilter(new AuthenticationFilter());
? ? ? ? registrationBean.addUrlPatterns("/api/*"); // 設置需要進行身份驗證的URL模式
? ? ? ? return registrationBean;
? ? }
}

在上述示例中,我們使用`FilterRegistrationBean`將自定義過濾器`AuthenticationFilter`注冊到應用程序上下文,并指定要進行身份驗證的URL模式(例如`/api/*`)。

請根據(jù)您的實

際需求和身份驗證邏輯進行適當?shù)男薷暮蛿U展。

這個示例僅演示了如何在自定義過濾器中進行身份驗證,您可以根據(jù)您的具體要求進行進一步的自定義和優(yōu)化,例如從數(shù)據(jù)庫中獲取用戶信息進行驗證、使用JWT驗證令牌、使用Spring Security進行身份驗證等。


實例2:Spring Boot中使用多個過濾器:

在Spring Boot中使用多個過濾器的步驟如下:

1. 創(chuàng)建多個過濾器類:為每個過濾器創(chuàng)建一個實現(xiàn)`javax.servlet.Filter`接口的類,并實現(xiàn)其`doFilter()`方法來定義每個過濾器的邏輯。例如,我們創(chuàng)建兩個過濾器類:`Filter1`和`Filter2`。

import javax.servlet.*;
import java.io.IOException;

public class Filter1 implements Filter {
? ? @Override
? ? public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
? ? ? ? ? ? throws IOException, ServletException {
? ? ? ? // 過濾器1的邏輯
? ? ? ? // ...

? ? ? ? // 調(diào)用下一個過濾器或目標資源
? ? ? ? chain.doFilter(request, response);

? ? ? ? // 在響應返回給客戶端之前的邏輯
? ? ? ? // ...
? ? }

? ? // 其他方法...
}

public class Filter2 implements Filter {
? ? @Override
? ? public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
? ? ? ? ? ? throws IOException, ServletException {
? ? ? ? // 過濾器2的邏輯
? ? ? ? // ...

? ? ? ? // 調(diào)用下一個過濾器或目標資源
? ? ? ? chain.doFilter(request, response);

? ? ? ? // 在響應返回給客戶端之前的邏輯
? ? ? ? // ...
? ? }

? ? // 其他方法...
}

2. 注冊多個過濾器:在Spring Boot應用程序的配置類中,使用`FilterRegistrationBean`為每個過濾器類注冊一個過濾器。在注冊過濾器時,可以指定不同的URL模式和順序。

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FilterConfig {

? ? @Bean
? ? public FilterRegistrationBean<Filter1> filter1Registration() {
? ? ? ? FilterRegistrationBean<Filter1> registrationBean = new FilterRegistrationBean<>();
? ? ? ? registrationBean.setFilter(new Filter1());
? ? ? ? registrationBean.addUrlPatterns("/url1"); // 設置過濾器1要過濾的URL模式
? ? ? ? registrationBean.setOrder(1); // 設置過濾器1的順序
? ? ? ? return registrationBean;
? ? }

? ? @Bean
? ? public FilterRegistrationBean<Filter2> filter2Registration() {
? ? ? ? FilterRegistrationBean<Filter2> registrationBean = new FilterRegistrationBean<>();
? ? ? ? registrationBean.setFilter(new Filter2());
? ? ? ? registrationBean.addUrlPatterns("/url2"); // 設置過濾器2要過濾的URL模式
? ? ? ? registrationBean.setOrder(2); // 設置過濾器2的順序
? ? ? ? return registrationBean;
? ? }
}

在上述示例中,我們?yōu)閮蓚€過濾器類`Filter1`和`Filter2`分別創(chuàng)建了一個`FilterRegistrationBean`。在每個`FilterRegistrationBean`中,我們設置了過濾器實例、要過濾的URL模式和過濾器的順序。根據(jù)需要,您可以設置不同的URL模式和順序。

請注意,過濾器的執(zhí)行順序由`setOrder()`方法指定,較小的值表示較高的優(yōu)先級,較高的值表示較低的優(yōu)先級。

通過上述步驟,成功創(chuàng)建了多個過濾器并將它們注冊到Spring Boot應用程序中。每個過濾器將根據(jù)其設置的順序在請求到達控制器之前進行處理。

請注意,在多個過濾器中的`chain.doFilter(request, response)`語句的調(diào)用順序?qū)Q定它們的執(zhí)行順序。第一個過濾器在調(diào)用`chain.doFilter()`之前執(zhí)行自己的邏輯,然后將請求傳遞給下一個過濾器或目標資源。然后,第二個過濾器執(zhí)行自己的邏輯,以此類推。

通過適當設置過濾器的順序,您可以控制它們的執(zhí)行順序,并確保按預期處理請求和響應。文章來源地址http://www.zghlxwxcb.cn/news/detail-529816.html

到了這里,關于Spring boot 中的過濾器的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領支付寶紅包贊助服務器費用

相關文章

  • 在Spring Boot中注冊過濾器幾種方式

    1. 實現(xiàn) Filter 接口并使用 @WebFilter 注解:(適用于Servlet API項目) 2. 使用Spring的 FilterRegistrationBean (推薦) 3. 定義為 Spring Bean 并手動配置到 Servlet 容器中 注意:這種方法通常在需要部署為 WAR包 到非嵌入式Servlet容器時采用。而在大多數(shù)Spring Boot應用中,建議使用前兩種注冊過

    2024年01月19日
    瀏覽(20)
  • 在Spring boot中 使用JWT和過濾器實現(xiàn)登錄認證

    在Spring boot中 使用JWT和過濾器實現(xiàn)登錄認證

    在navicat中運行如下sql,準備一張user表 導入pom.xml坐標 在工utils包下創(chuàng)建一個用于生成和解析JWT 令牌的工具類 在pojo包下創(chuàng)建user類 在mapper包下添加 UserMapper接口 在service包下添加 UserService類 在utils包下添加統(tǒng)一響應結果封裝類 在controller包下添加LoginController類 這樣登錄獲取toke

    2024年02月06日
    瀏覽(18)
  • Spring中的攔截器與過濾器:原理、區(qū)別與案例解析

    Spring中的攔截器與過濾器:原理、區(qū)別與案例解析

    前言 在Web應用中,我們經(jīng)常需要對用戶的請求進行某種處理,比如權限驗證、日志記錄等。 Spring框架提供了兩種機制來實現(xiàn)這一需求:攔截器和過濾器。雖然它們的目標相似,但在使用上存在一些差異。本篇文章我們將詳細探討這兩種機制的原理、區(qū)別,希望能給各位大佬

    2024年04月23日
    瀏覽(23)
  • Spring Security 中的過濾器鏈是什么?它的作用是什么

    Spring Security 中的過濾器鏈是什么?它的作用是什么

    Spring Security是一個安全框架,它提供了強大的安全保護功能,可以幫助開發(fā)者更加方便地實現(xiàn)應用程序的安全性。Spring Security中的過濾器鏈是其中一個非常重要的部分,它起到了非常重要的作用。本文將介紹什么是Spring Security中的過濾器鏈,以及它的作用和如何使用它。同時

    2024年02月06日
    瀏覽(22)
  • Spring Cloud Gateway 過濾器

    Spring Cloud Gateway 過濾器

    Spring Cloud Gateway 過濾器的種類有30多種。 官文文檔地址: Spring Cloud Gateway https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/#gatewayfilter-factories Spring Cloud Gateway大體可以分為下面兩種類型的過濾器: 1、內(nèi)置的過濾器 ? ? ? ? 1.1、內(nèi)置的局部過濾器 ? ? ? ? 1.2、內(nèi)置的全

    2024年03月28日
    瀏覽(21)
  • Spring Security內(nèi)置過濾器詳解

    Spring Security內(nèi)置過濾器詳解

    相關文章: OAuth2的定義和運行流程 Spring Security OAuth實現(xiàn)Gitee快捷登錄 Spring Security OAuth實現(xiàn)GitHub快捷登錄 Spring Security的過濾器鏈機制 Spring Security OAuth Client配置加載源碼分析 根據(jù)前面的示例,我們已經(jīng)知道啟動時會加載18個過濾器,并且已經(jīng)知道了請求會匹配到DefaultSecurityF

    2024年02月05日
    瀏覽(27)
  • Spring Cloud Gateway 過濾器詳解

    Spring Cloud Gateway 過濾器詳解

    Spring Cloud Gateway根據(jù)作用范圍劃分為:GatewayFilter和GlobalFilter 由filter工作流程點,可以知道filter有著非常重要的作用,在“pre”類型的過濾器可以做參數(shù)校驗、權限校驗、流量監(jiān)控、日志輸出、協(xié)議轉(zhuǎn)換等,在“post”類型的過濾器中可以做響應內(nèi)容、響應頭的修改,日志的輸

    2023年04月08日
    瀏覽(24)
  • Spring Cloud GateWay 全局過濾器

    這是一個自定義的 Spring Cloud Gateway 全局過濾器(Global Filter)。在 Spring Cloud Gateway 中,全局過濾器可以在請求被路由到目標服務之前或之后執(zhí)行一些操作。這個過濾器實現(xiàn)了 GlobalFilter 接口和 Ordered 接口,這兩個接口的作用如下: GlobalFilter 接口: 這是一個 Spring Cloud Gateway 提

    2024年02月11日
    瀏覽(20)
  • Spring/SpringBoot 過濾器修改、獲取http 請求request中的參數(shù) 和 response返回值,比如修改請求體和響應體的字符編碼

    通過自定義filter,RequestWrapper,ResponseWrapper 處理請求和響應數(shù)據(jù),比如修改請求體和響應體的字符編碼 1.request 和 response 中的數(shù)據(jù)都是 存在流中的(緩存中)獲取一次就沒有了,需要重新寫回去。所以需要兩個包裝類分別繼承HttpServletRequestWrapper 和 HttpServletResponseWrapper 對 r

    2024年02月15日
    瀏覽(37)
  • Spring Cloud Gateway快速入門(三)——過濾器

    Gateway過濾器是Spring Cloud Gateway提供的一種機制,用于對進入網(wǎng)關的請求和返回進行處理和轉(zhuǎn)換。它可以用于實現(xiàn)各種功能,如請求鑒權、請求轉(zhuǎn)發(fā)、請求限流、請求重試等。 網(wǎng)關過濾器是Spring Cloud Gateway提供的一種機制,用于在請求進入網(wǎng)關和響應離開網(wǎng)關時進行一些預處理

    2024年02月04日
    瀏覽(47)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領取紅包,優(yōu)惠每天領

二維碼1

領取紅包

二維碼2

領紅包