




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
第SpringBoot過濾器與攔截器深入分析實(shí)現(xiàn)方法目錄過濾器編寫過濾器注冊過濾器基于FilterRegistrationBean基于@WebFilter攔截器
過濾器
實(shí)現(xiàn)過濾器需要實(shí)現(xiàn)javax.servlet.Filter接口。重寫三個(gè)方法。其中init()方法在服務(wù)啟動(dòng)時(shí)執(zhí)行,destroy()在服務(wù)停止之前執(zhí)行。
可用兩種方式注冊過濾器:
使用FilterRegistrationBean來注入。可使用setOrder(0)設(shè)置過濾器的優(yōu)先級,越小優(yōu)先級越高。使用@WebFilter(filterName=myFilter2,urlPatterns=/*)配合@ServletComponentScan()實(shí)現(xiàn)注入。(@Order注解無效)
編寫過濾器
packagecom.example.recorddemo.filters;
importjavax.servlet.*;
importjavax.servlet.http.HttpServletRequest;
importjava.io.IOException;
publicclassMyFilter1implementsFilter{
@Override
publicvoidinit(FilterConfigfilterConfig)throwsServletException{
System.out.println("初始化過濾器:"+filterConfig.getFilterName());
@Override
publicvoiddoFilter(ServletRequestservletRequest,ServletResponseservletResponse,FilterChainfilterChain)throwsIOException,ServletException{
System.out.println("在請求之前做處理");
if(servletRequestinstanceofHttpServletRequest){
System.out.println("URL:"+((HttpServletRequest)servletRequest).getRequestURL());
//調(diào)用filter鏈中的下一個(gè)filter
filterChain.doFilter(servletRequest,servletResponse);
System.out.println("在請求之后做處理");
@Override
publicvoiddestroy(){
System.out.println("銷毀:MyFilter1");
}
注冊過濾器
基于FilterRegistrationBean
在配置類中注冊一個(gè)FilterRegistrationBean類型的Bean。
如果沒有設(shè)置UrlPatterns,那么會(huì)自動(dòng)關(guān)聯(lián)到/*上。如果沒有設(shè)置過濾器的名字,那么會(huì)自動(dòng)推理出一個(gè)過濾器名稱(bean的名字)
WhennoURLpatternorservletsarespecifiedthefilterwillbeassociatedto/*.Thefilternamewillbededucedifnotspecified.
fileter默認(rèn)是enable的,將其設(shè)置為false表示關(guān)閉當(dāng)前過濾器??赏ㄟ^setOrder(0)方法設(shè)置過濾器的優(yōu)先級,如果優(yōu)先級相同,則先定義的優(yōu)先級更高。
@Configuration
publicclassFilterConfiguration{
@Bean
publicFilterRegistrationBeanmyFilter1(){
MyFilter1filter=newMyFilter1();
FilterRegistrationBeanfilterRegistrationBean=newFilterRegistrationBean(filter);
//filterRegistrationBean.addUrlPatterns("/*");
//filterRegistrationBean.setEnabled(true);
returnfilterRegistrationBean;
基于@WebFilter
使用@WebFilter修飾filter。在任意configuration類中添加@ServletComponentScan(com.example.recorddemo.filters),包名可以不填。
importjavax.servlet.*;
importjavax.servlet.annotation.WebFilter;
importjavax.servlet.http.HttpServletRequest;
importjava.io.IOException;
@WebFilter(filterName="myFilter2",urlPatterns="/*")
publicclassMyFilter2implementsFilter{
@Override
publicvoidinit(FilterConfigfilterConfig)throwsServletException{}
@Override
publicvoiddoFilter(ServletRequestservletRequest,ServletResponseservletResponse,FilterChainfilterChain)throwsIOException,ServletException{
//調(diào)用filter鏈中的下一個(gè)filter
filterChain.doFilter(servletRequest,servletResponse);
@Override
publicvoiddestroy(){}
}
攔截器
攔截器會(huì)在處理指定請求之前和之后進(jìn)行相關(guān)操作,配置攔截器需要兩步
編寫攔截器類(實(shí)現(xiàn)HandlerInterceptor接口)添加已實(shí)現(xiàn)的攔截器(實(shí)現(xiàn)WebMvcConfigurer接口,并重寫addInterceptors()方法)添加addPathPatterns()規(guī)定攔截哪些請求。(/*表示只攔截/下的所有目錄,但是不包括子目錄,/**表示攔截/下的所有目錄,及其子目錄)
攔截器類:
packageerceptor;
importorg.springframework.stereotype.Component;
importorg.springframework.web.servlet.HandlerInterceptor;
importorg.springframework.web.servlet.ModelAndView;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
*@authorwangchao
@Component
publicclassMyInterceptorimplementsHandlerInterceptor{
@Override
publicbooleanpreHandle(HttpServletRequestrequest,HttpServletResponseresponse,Objecthandler)throwsException{
//handle可拿到執(zhí)行方法的反射對象。
System.out.println("preHandle:MyInterceptor");
returntrue;
@Override
publicvoidpostHandle(HttpServletRequestrequest,HttpServletResponseresponse,Objecthandler,ModelAndViewmodelAndView)throwsException{
//對于RESTful接口用處不大
@Override
publicvoidafterCompletion(HttpServletRequestrequest,HttpServletResponseresponse,Objecthandler,Exceptionex)throwsException{
//可捕捉異常,但是springboot已經(jīng)有了全局異常捕捉
}
配置攔截器:
packagecom.example.recorddemo.configuration;
importerceptor.MyInterceptor;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.context.annotation.Configuration;
importorg.springframework.web.servlet.HandlerInterceptor;
importorg.springframework.web.servlet.config.annotation.InterceptorRegistration;
importorg.springframework.web.servlet.config.annotation.InterceptorRegistry;
importorg.springframework.web.servlet.config.annotation.WebMvcConfigurer;
importjavax.annotation.Resource;
@Configuration
publicclassInterceptorConfigurationimplementsWebMvcConfigurer{
@Resource
MyInterceptormyInterceptor;
*添加攔截器
*@paramregistry
@Override
publicvoidaddInterceptors(InterceptorRegistryregistry){
registry.addInterceptor(myInterceptor).addPathPatterns("/**");
}
registry.addInterceptor()方法會(huì)返回當(dāng)前的interceptor,因此可直接執(zhí)行addPathPatterns()方法
publicInterceptorRegistrationaddInterceptor(HandlerInterceptorinterceptor){
InterceptorRe
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 婚后財(cái)產(chǎn)補(bǔ)充協(xié)議
- 沈陽轉(zhuǎn)讓協(xié)議書
- 精密機(jī)械零部件加工與質(zhì)量保證合同
- 軟件售后維保合同協(xié)議
- 股權(quán)轉(zhuǎn)讓及合作協(xié)議
- 幕墻石材鋼架施工合同
- 產(chǎn)品采購供應(yīng)合同附加條款
- 贈(zèng)送貼紙定制合同協(xié)議
- 車撞人協(xié)議書范本
- 跟員工提前合同解除協(xié)議
- 陶藝店管理制度
- 2025-2030中國儲能電站行業(yè)市場深度分析及前景趨勢與投資研究報(bào)告
- 2025年標(biāo)準(zhǔn)租房合同范本
- 三元空間下個(gè)人化IP綜藝《燦爛的花園》敘事與價(jià)值研究
- 2025屆安徽省池州市普通高中高三教學(xué)質(zhì)量統(tǒng)一監(jiān)測政治試卷含、答案
- 《汽車博覽會(huì)》名師課件2
- 學(xué)校食堂“三同三公開”制度實(shí)施方案
- 海南2025年海南熱帶海洋學(xué)院招聘113人筆試歷年參考題庫附帶答案詳解
- 2024-2025學(xué)年人教版(2024)七年級英語下冊Unit 6 rain or shine Section A 2a-2e 教案
- 比較文學(xué)形象學(xué)-狄澤林克
- 商業(yè)地產(chǎn)運(yùn)營管理規(guī)章制度
評論
0/150
提交評論