springboot的攔截器和過濾器實現

来源:https://www.cnblogs.com/han-sun/archive/2020/03/10/12459042.html
-Advertisement-
Play Games

springboot攔截器 說明:和springmvc一樣去實現HandlerInterceptor介面 import org.springframework.stereotype.Component; import org.springframework.web.servlet.HandlerIn ...


  • springboot攔截器
說明:和springmvc一樣去實現HandlerInterceptor介面

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 〈一句話功能簡述〉<br>
 * TODO(參數攔截器1)
 *
 * @author sunhan
 * @version 1.0.0
 * @since 2020/3/7 10:06 PM
 */
@Component
public class ParameterInterceptor1 implements HandlerInterceptor {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
                             Object handler) throws Exception {
        System.out.println("進入了攔截器-----11111");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response,
                           Object handler, ModelAndView modelAndView) {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
                                Object handler, Exception ex) {

    }
}
View Code

 

把自定義攔截器註冊到WebMvcConfigurer配置介面中:

重寫addInterceptors方法,添加自定義攔截器的order,pathPatterns

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 〈一句話功能簡述〉<br>
 * TODO()
 *
 * @author sunhan
 * @version 1.0.0
 * @since 2020/3/7 10:25 PM
 */
@Configuration
public class InterceptConfiguration implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new ParameterInterceptor1()).addPathPatterns("/get*");
        registry.addInterceptor(new ParameterInterceptor2()).addPathPatterns("/save*");
    }
}
View Code

 

  • springboot過濾器

說明:註解方式和註冊方式

  1、註解方式:

import org.springframework.core.annotation.Order;

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

/**
 * 〈一句話功能簡述〉<br>
 * TODO()
 *
 * @author sunhan
 * @version 1.0.0
 * @since 2020/3/6 5:28 PM
 */
@WebFilter(filterName = "requestParamFilter",urlPatterns = "/*")
@Order(3)
public class RequestParamFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
                         FilterChain filterChain) throws IOException, ServletException {
        System.out.println("sunhan---請求參數過濾器!---test1");
        filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {

    }
}
View Code

  註意:

  1.1、註解方式需要@WebFilter和@Order放在Filter實現類上。

  1.2、註解方式需要@ServletComponentScan放在SpringBootApplication啟動類上

  

  2、註冊方式:

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

public class MyFilter implements Filter {

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

    @Override
    public void doFilter(ServletRequest srequest, ServletResponse sresponse, FilterChain filterChain)
            throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) srequest;
        System.out.println("this is MyFilter,url :"+request.getRequestURI());
        filterChain.doFilter(srequest, sresponse);
    }

    @Override
    public void destroy() {
    }

}
View Code

  註意:@Bean放在一個方法中,然後通過springboot的FilterRegistrationBean註冊到過濾鏈中

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

@Configuration
public class WebConfiguration {

    @Bean
    public FilterRegistrationBean testFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new MyFilter());
        registration.addUrlPatterns("/*");
        registration.setName("MyFilter");
        registration.setOrder(6);
        return registration;
    }

    @Bean
    public FilterRegistrationBean test2FilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new MyFilter2());
        registration.addUrlPatterns("/*");
        registration.setName("MyFilter2");
        registration.setOrder(1);
        return registration;
    }
}
View Code

 

 

 

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 獲取系統硬體 裡面的參數是啥意思自己去查吧,沒怎麼深入去瞭解 ...
  • 一、使用UML表示InputStream和OutputStream的繼承結構圖 二、使用UML表示Reader和Writer的繼承結構圖 三、FileInputStream舉例 package com.bjpowernode.java_learning; import java.io.*; ​ pu ...
  • 前言 考慮一個功能業務,在 程式中向指定的某個用戶進行實時通訊 在Web運用的 通訊功能中(如線上客服),為保證點對點通訊.而這個看似簡單的根據用戶尋到起 通道實際會碰到不少問題 1. web程式中的 協議是無狀態的 2. 一般項目中 服務和 項目是獨立部署的 3. 連接存在重連的情況,而 對象每次 ...
  • 1. Spring依賴註入方式 今天這裡主要講解一下Spring框架中的依賴註入的多種方式及相關的一系列配置,這也是Spring這款輕量級DI、IOC的Bean容器框架的核心魅力所在。 Spring依賴註入方式這裡著重說明一下構造器參數註入、多類型(Arrays、Set、List、Map、Prope ...
  • 1 簡介 判斷一個字元串是否包含某個特定子串是常見的場景,比如判斷一篇文章是否包含敏感辭彙、判斷日誌是否有 信息等。本文將介紹四種方法併進行性能測試。 2 四種方法 2.1 JDK原生方法String.indexOf 在 的函數中,提供了 方法,返回子串 第一次出現的位置,如果不存在則返回 1。例子 ...
  • Python廣告,鋪天蓋地,小白們霧裡看花,Python無限美好。作為會20幾種語言的編程愛好者,我先說說如果入門python應該想好三大問題,並且在此戳穿三大謊言。 ...
  • 結構體 用一組變數定義一個事物 struct student //student 是一種數據類型 { int id; char name[20]; char sex; int age; }mike,bob;變數定義除了跟在結構體定義後面,還可以:student std1,std2;跟int a,b; ...
  • uwsgi uWSGI是一個Web伺服器,它實現了WSGI協議、uwsgi、http等協議。Nginx中HttpUwsgiModule的作用是與uWSGI伺服器進行交換。 WSGI是一種Web伺服器網關介面。它是一個Web伺服器(如nginx,uWSGI等伺服器)與web應用(如用Flask框架寫的 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...