




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
第SpringBoot過(guò)濾器與攔截器深入分析實(shí)現(xiàn)方法目錄過(guò)濾器編寫(xiě)過(guò)濾器注冊(cè)過(guò)濾器基于FilterRegistrationBean基于@WebFilter攔截器
過(guò)濾器
實(shí)現(xiàn)過(guò)濾器需要實(shí)現(xiàn)javax.servlet.Filter接口。重寫(xiě)三個(gè)方法。其中init()方法在服務(wù)啟動(dòng)時(shí)執(zhí)行,destroy()在服務(wù)停止之前執(zhí)行。
可用兩種方式注冊(cè)過(guò)濾器:
使用FilterRegistrationBean來(lái)注入??墒褂胹etOrder(0)設(shè)置過(guò)濾器的優(yōu)先級(jí),越小優(yōu)先級(jí)越高。使用@WebFilter(filterName=myFilter2,urlPatterns=/*)配合@ServletComponentScan()實(shí)現(xiàn)注入。(@Order注解無(wú)效)
編寫(xiě)過(guò)濾器
packagecom.example.recorddemo.filters;
importjavax.servlet.*;
importjavax.servlet.http.HttpServletRequest;
importjava.io.IOException;
publicclassMyFilter1implementsFilter{
@Override
publicvoidinit(FilterConfigfilterConfig)throwsServletException{
System.out.println("初始化過(guò)濾器:"+filterConfig.getFilterName());
@Override
publicvoiddoFilter(ServletRequestservletRequest,ServletResponseservletResponse,FilterChainfilterChain)throwsIOException,ServletException{
System.out.println("在請(qǐng)求之前做處理");
if(servletRequestinstanceofHttpServletRequest){
System.out.println("URL:"+((HttpServletRequest)servletRequest).getRequestURL());
//調(diào)用filter鏈中的下一個(gè)filter
filterChain.doFilter(servletRequest,servletResponse);
System.out.println("在請(qǐng)求之后做處理");
@Override
publicvoiddestroy(){
System.out.println("銷(xiāo)毀:MyFilter1");
}
注冊(cè)過(guò)濾器
基于FilterRegistrationBean
在配置類中注冊(cè)一個(gè)FilterRegistrationBean類型的Bean。
如果沒(méi)有設(shè)置UrlPatterns,那么會(huì)自動(dòng)關(guān)聯(lián)到/*上。如果沒(méi)有設(shè)置過(guò)濾器的名字,那么會(huì)自動(dòng)推理出一個(gè)過(guò)濾器名稱(bean的名字)
WhennoURLpatternorservletsarespecifiedthefilterwillbeassociatedto/*.Thefilternamewillbededucedifnotspecified.
fileter默認(rèn)是enable的,將其設(shè)置為false表示關(guān)閉當(dāng)前過(guò)濾器??赏ㄟ^(guò)setOrder(0)方法設(shè)置過(guò)濾器的優(yōu)先級(jí),如果優(yōu)先級(jí)相同,則先定義的優(yōu)先級(jí)更高。
@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ì)在處理指定請(qǐng)求之前和之后進(jìn)行相關(guān)操作,配置攔截器需要兩步
編寫(xiě)攔截器類(實(shí)現(xiàn)HandlerInterceptor接口)添加已實(shí)現(xiàn)的攔截器(實(shí)現(xiàn)WebMvcConfigurer接口,并重寫(xiě)addInterceptors()方法)添加addPathPatterns()規(guī)定攔截哪些請(qǐng)求。(/*表示只攔截/下的所有目錄,但是不包括子目錄,/**表示攔截/下的所有目錄,及其子目錄)
攔截器類:
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í)行方法的反射對(duì)象。
System.out.println("preHandle:MyInterceptor");
returntrue;
@Override
publicvoidpostHandle(HttpServletRequestrequest,HttpServletResponseresponse,Objecthandler,ModelAndViewmodelAndView)throwsException{
//對(duì)于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. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 學(xué)校規(guī)定資產(chǎn)管理制度
- 城鄉(xiāng)建設(shè)公司管理制度
- 醫(yī)藥公司藥妝管理制度
- DB62T 4330-2021 油菜品種 隴油16號(hào)
- DB62T 4415-2021 當(dāng)歸栽培技術(shù)規(guī)程
- 聚會(huì)席位安排方案(3篇)
- 小區(qū)樓梯休整方案(3篇)
- 廠區(qū)供熱規(guī)劃方案(3篇)
- 城郊小院改造方案(3篇)
- 冷鏈試劑交貨方案(3篇)
- 水工隧洞施工技術(shù)規(guī)范
- 執(zhí)行立案申請(qǐng)書(shū)模版
- 軟件采購(gòu)意向協(xié)議書(shū)范本
- 安全總監(jiān)轉(zhuǎn)正述職報(bào)告
- 《大棚蔬菜種植技術(shù)》課件
- 《電工電子技術(shù)(II)》試題A卷 及答案
- 夏縣縣城污水處理提質(zhì)增效-一廠一策-系統(tǒng)化整治方案
- 2024年檔案知識(shí)競(jìng)賽試題及答案
- 跨境電商知識(shí)競(jìng)賽考試題庫(kù)(500題)
- 2024年注冊(cè)計(jì)量師-一級(jí)注冊(cè)計(jì)量師考試近5年真題集錦(頻考類試題)帶答案
- GB/T 44567-2024光學(xué)晶體紫外級(jí)氟化鈣晶體
評(píng)論
0/150
提交評(píng)論