日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

SpringBoot中过滤器和监听器

發(fā)布時(shí)間:2025/4/5 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringBoot中过滤器和监听器 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

在很多Web項(xiàng)目中,都會(huì)用到過濾器(Filter),如參數(shù)過濾、防止SQL注入、防止頁(yè)面攻擊、空參數(shù)矯正、Token驗(yàn)證、Session驗(yàn)證、點(diǎn)擊率統(tǒng)計(jì)等。

認(rèn)識(shí)過濾器

1.為什么要使用過濾器
在Web開發(fā)中,常常會(huì)有這樣的需求:在所有接口中去除用戶輸入的非法字符,以防止引起業(yè)務(wù)異常。要實(shí)現(xiàn)這個(gè)功能,可以有很多方法,比如

方法內(nèi)容
方法1在前端參數(shù)傳入時(shí)進(jìn)行校驗(yàn),先過濾掉非法字符,然后返回用戶界面提示用戶重新輸入
方法2后端接收前端沒有過濾的數(shù)據(jù),然后過濾非法字符
方法3利用Filter處理項(xiàng)目中所有非法字符

前兩種方法實(shí)現(xiàn)時(shí)會(huì)存在重復(fù)代碼,因?yàn)槊總€(gè)前端頁(yè)面或后端都需要處理,這樣會(huì)導(dǎo)致代碼難以維護(hù)。如果用過濾器來(lái)實(shí)現(xiàn),則只需要用過濾器對(duì)所有接口進(jìn)行過濾處理。這樣既方便,又不會(huì)產(chǎn)生冗余代碼。

2.使用Filter的步驟
1)新建類,實(shí)現(xiàn)Filter抽象類
2)重寫init、doFilter、destroy方法
3)在Spring Boot入口中添加注解@ServletComponentScan,以注冊(cè)Filter

下面說明一下編寫過濾器類的具體邏輯
如果有多個(gè)過濾器,可以通過注解@Order來(lái)設(shè)置過濾器的執(zhí)行順序,序號(hào)越小,越早被執(zhí)行。

package com.example.demo.Filter; import lombok.Builder;import javax.servlet.*; import javax.servlet.annotation.WebFilter; import java.io.IOException;@Order(1) //URL過濾設(shè)置 @WebFilter(filtername="FilterDemo01",urlPatterns = "/*")public class FilterDemo01 implements Filter{@Overridepublic void init(FilterConfig filterConfig)throws ServletException{//init邏輯,該init將在服務(wù)器啟動(dòng)時(shí)被調(diào)用}@Overridepublic void doFilter(ServletRequest servletRequest,ServletResponse servletResponse,FilterChain filterChain)throws IOException,ServletException{//請(qǐng)求(request)處理邏輯//請(qǐng)求(request)封裝邏輯//chain重新寫回request和response}@Overridepublic void destroy(){//重寫destroy邏輯,該邏輯將在服務(wù)器關(guān)閉時(shí)被調(diào)用} }

實(shí)現(xiàn)過濾器

實(shí)驗(yàn)結(jié)果
因?yàn)橥ㄟ^注解@WebFilter(urlPatterns="/*")定義了urlPatterns的變量值為 “ * ”,代表所有路徑。所以用戶在訪問本項(xiàng)目下的任何路徑的頁(yè)面時(shí),此過濾器都會(huì)在控制臺(tái)輸出一下信息

攔截器

不需要添加依賴
1.新建攔截器類
FilterDemo01.java

package com.example.demo.Filter; import lombok.Builder;import javax.servlet.*; import javax.servlet.annotation.WebFilter; import java.io.IOException;//作用范圍 @WebFilter(urlPatterns = "/*")public class FilterDemo01 implements Filter{@Overridepublic void init(FilterConfig filterConfig)throws ServletException{}@Overridepublic void doFilter(ServletRequest servletRequest,ServletResponse servletResponse,FilterChain filterChain)throws IOException,ServletException{System.out.println("攔截器");filterChain.doFilter(servletRequest,servletResponse);}@Overridepublic void destroy(){} }

2.在入口類中開啟Servlet支持
直接在入口類加入@ServletComponentScan即可。
ServletApplication.java

package com.example.demo;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan;@SpringBootApplication @ServletComponentScan public class ServletApplication {public static void main(String[] args) {SpringApplication.run(ServletApplication.class, args);}}

認(rèn)識(shí)監(jiān)聽器

監(jiān)聽器(Listener)用于監(jiān)聽Web應(yīng)用程序中某些對(duì)象或信息的創(chuàng)建、銷毀、增加、修改、刪除等動(dòng)作,然后做出相應(yīng)的響應(yīng)處理。當(dāng)對(duì)象的狀態(tài)發(fā)生變化時(shí),服務(wù)器自動(dòng)調(diào)用監(jiān)聽器的方法,監(jiān)聽器常用于統(tǒng)計(jì)在線人數(shù)、在線用戶、系統(tǒng)加載時(shí)的信息初始化等。

監(jiān)聽器有如下三種類型
1.監(jiān)聽ServletContext,Request,Session作用域的創(chuàng)建和銷毀

  • ServletContextListener:監(jiān)聽ServletContext
  • HttpSessionListener:監(jiān)聽新的Session創(chuàng)建事件
  • ServletRequestListener:監(jiān)聽ServletRequest的初始化和銷毀

2.監(jiān)聽ServletContext,Request,Session作用域中屬性的變化(增加、修改、刪除)

  • ServletContextAttributeListener:監(jiān)聽Servlet上下文參數(shù)的變化
  • HttpSessionAttributeListener:監(jiān)聽HttpSession參數(shù)的變化
  • ServletRequestAttributeListener:監(jiān)聽ServletRequest參數(shù)的變化

3.監(jiān)聽HttpSession中對(duì)象狀態(tài)的改變(被綁定、解除綁定、鈍化、活化)

  • HttpSessionBindingListener:監(jiān)聽HttpSession,并綁定及解除綁定
  • HttpSessionActivationListener:監(jiān)聽鈍化和活動(dòng)的HttpSession狀態(tài)改變

實(shí)現(xiàn)監(jiān)聽器

實(shí)驗(yàn)結(jié)果
啟動(dòng)項(xiàng)目后,在控制臺(tái)會(huì)輸出如下信息

ServletContex 初始化 Apache Tomcat/9.0.36

1.創(chuàng)建監(jiān)聽類
在上面過濾器項(xiàng)目的基礎(chǔ)上,創(chuàng)建文件Listener,并創(chuàng)建如下java文件

FilterDemo02.java

package com.example.demo.listener;import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.annotation.WebListener; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; @WebListener public class FilterDemo02 implements ServletContextListener {@Overridepublic void contextInitialized(ServletContextEvent servletContextEvent){System.out.println("ServletContex 初始化");System.out.println(servletContextEvent.getServletContext().getServerInfo());}@Overridepublic void contextDestroyed(ServletContextEvent servletContextEvent){System.out.println("ServletContex 銷毀");} }

2.開啟監(jiān)聽類Bean掃描
在入口類中,添加注解@ServletComponentScan

《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀

總結(jié)

以上是生活随笔為你收集整理的SpringBoot中过滤器和监听器的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。