javascript
SpringBoot 过滤器、拦截器、监听器对比及使用场景!
來源 |?blog.csdn.net/qq_38020915/article/details/116431612
作者 |?dingwen_blog
一、關(guān)系圖理解
二、區(qū)別
1.過濾器
過濾器是在web應(yīng)用啟動的時候初始化一次, 在web應(yīng)用停止的時候銷毀
可以對請求的URL進(jìn)行過濾, 對敏感詞過濾
擋在攔截器的外層
實(shí)現(xiàn)的是 javax.servlet.Filter 接口 ,是 Servlet 規(guī)范的一部分
在請求進(jìn)入容器后,但在進(jìn)入servlet之前進(jìn)行預(yù)處理,請求結(jié)束是在servlet處理完以后
依賴Web容器
會多次執(zhí)行
1.1HttpServletRequestWrapper
“在請求到達(dá)之前對 request 進(jìn)行修改
package?com.dingwen.lir.filter;import?lombok.extern.slf4j.Slf4j;import?javax.servlet.http.HttpServletRequest; import?javax.servlet.http.HttpServletRequestWrapper; import?java.util.Arrays;/***??在請求到達(dá)之前對?request?進(jìn)行修改*/ @Slf4j public?class?RequestWrapper?extends?HttpServletRequestWrapper?{public?RequestWrapper(HttpServletRequest?request)?{super(request);log.info("RequestWrapper");}@Overridepublic?String?getParameter(String?name)?{//?可以對請求參數(shù)進(jìn)行過濾return?super.getParameter(name);}@Overridepublic?String[]?getParameterValues(String?name)?{//?對請求參數(shù)值進(jìn)行過濾 //????????String[]?values?=super.getRequest().getParameterValues(name); //????????return?super.getParameterValues(name);return?"t?e?s?t".split("?");}}1.2 OncePerRequestFilter
“OncePerRequestFilter,顧名思義,它能夠確保在一次請求中只通過一次filter
package?com.dingwen.lir.filter;import?lombok.extern.slf4j.Slf4j; import?org.springframework.web.filter.OncePerRequestFilter;import?javax.servlet.FilterChain; import?javax.servlet.ServletException; import?javax.servlet.http.HttpServletRequest; import?javax.servlet.http.HttpServletResponse; import?java.io.IOException; import?java.io.PrintWriter; import?java.util.Arrays;/***?請求過濾器*?OncePerRequestFilter:*?OncePerRequestFilter,顧名思義,它能夠確保在一次請求中只通過一次filter.*?大家常識上都認(rèn)為,一次請求本來就只filter一次,為什么還要由此特別限定呢,往往我們的常識和實(shí)際的實(shí)現(xiàn)并不真的一樣,經(jīng)過一番資料的查閱,此方法是為了兼容不同的web?container,*?也就是說并不是所有的container都入我們期望的只過濾一次,servlet版本不同,執(zhí)行過程也不同,*?因此,為了兼容各種不同運(yùn)行環(huán)境和版本,默認(rèn)filter繼承OncePerRequestFilter是一個比較穩(wěn)妥的選擇。**/ @Slf4j public?class?RequestFilter?extends?OncePerRequestFilter?{@Overridepublic?void?destroy()?{super.destroy();log.info("RequestFilter?destroy");}/*OncePerRequestFilter.doFilter方法中通過request.getAttribute判斷當(dāng)前過濾器是否已執(zhí)行若未執(zhí)行過,則調(diào)用doFilterInternal方法,交由其子類實(shí)現(xiàn)*/@Overrideprotected?void?doFilterInternal(HttpServletRequest?httpServletRequest,?HttpServletResponse?httpServletResponse,?FilterChain?filterChain)?throws?ServletException,?IOException?{try?{RequestWrapper?requestWrapper?=?new?RequestWrapper(httpServletRequest);filterChain.doFilter(requestWrapper,?httpServletResponse);log.info("RequestFilter");log.info(Arrays.toString(requestWrapper.getParameterValues("name")));}?catch?(Exception?exception)?{httpServletResponse.setCharacterEncoding("utf-8");httpServletResponse.setContentType("application/json;?charset=utf-8");PrintWriter?writer?=?httpServletResponse.getWriter();writer.write(exception.toString());}} }1.3 配置
package?com.dingwen.lir.configuration;import?com.dingwen.lir.filter.RequestFilter; import?com.dingwen.lir.filter.RequestWrapper; import?org.springframework.boot.web.servlet.FilterRegistrationBean; import?org.springframework.context.annotation.Bean; import?org.springframework.context.annotation.Configuration;import?javax.servlet.Filter;/***?過濾器配置類**/ @Configuration public?class?FilterConfig?{@Beanpublic?RequestFilter?requestFilter(){return?new?RequestFilter();}@Beanpublic?FilterRegistrationBean<RequestFilter>?registrationBean()?{FilterRegistrationBean<RequestFilter>?registrationBean?=?new?FilterRegistrationBean<>();registrationBean.setFilter(requestFilter());registrationBean.addUrlPatterns("/filter/*");registrationBean.setName("RequestFilter");//過濾器的級別,值越小級別越高越先執(zhí)行registrationBean.setOrder(1);return?registrationBean;} }2.攔截器
實(shí)現(xiàn) org.springframework.web.servlet.HandlerInterceptor 接口,動態(tài)代理
攔截器應(yīng)用場景, 性能分析, 權(quán)限檢查, 日志記錄
是一個Spring組件,并由Spring容器管理,并不
依賴Tomcat等容器,是可以單獨(dú)使用的。不僅能應(yīng)用在web程序中,也可以用于Application、Swing等程序中
是在請求進(jìn)入servlet后,在進(jìn)入Controller之前進(jìn)行預(yù)處理的,Controller 中渲染了對應(yīng)的視圖之后請求結(jié)束
2.1登錄攔截
package?com.dingwen.lir.interceptor;import?com.dingwen.lir.entity.User; import?org.springframework.stereotype.Component; import?org.springframework.util.ObjectUtils; import?org.springframework.web.servlet.HandlerInterceptor;import?javax.servlet.http.HttpServletRequest; import?javax.servlet.http.HttpServletResponse; import?java.io.IOException;/***?登錄攔截**/ @Component public?class?PageInterceptor?implements?HandlerInterceptor?{@Overridepublic?boolean?preHandle(HttpServletRequest?request,?HttpServletResponse?response,?Object?handler)?throws?Exception?{User?user?=?(User)request.getSession().getAttribute("user");if?(!ObjectUtils.isEmpty(user))?{return?true;}?else?{//?不管是轉(zhuǎn)發(fā)還是重定向,必須返回false。否則出現(xiàn)多次提交響應(yīng)的錯誤redirect(request,?response);return?false;}}/**?對于請求是ajax請求重定向問題的處理方法*?@param?request*?@param?response**/public?void?redirect(HttpServletRequest?request,?HttpServletResponse?response)?throws?IOException?{if("XMLHttpRequest".equals(request.getHeader("X-Requested-With"))){//?ajax//獲取當(dāng)前請求的路徑response.setHeader("Access-Control-Expose-Headers",?"REDIRECT,CONTENT_PATH");//告訴ajax我是重定向response.setHeader("REDIRECT",?"REDIRECT");//告訴ajax我重定向的路徑StringBuffer?url?=?request.getRequestURL();String?contextPath?=?request.getContextPath();response.setHeader("CONTENT_PATH",?url.replace(url.indexOf(contextPath)?+?contextPath.length(),?url.length(),?"/").toString());}else{//?httpresponse.sendRedirect(?"/page/login");}response.getWriter().write(403);response.setStatus(HttpServletResponse.SC_FORBIDDEN);}}2.2配置
package?com.dingwen.lir.configuration;import?com.dingwen.lir.interceptor.PageInterceptor; import?org.springframework.context.annotation.Configuration; import?org.springframework.web.servlet.config.annotation.InterceptorRegistry; import?org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import?org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import?org.springframework.web.servlet.config.annotation.WebMvcConfigurer;/***?mvc?控制器配置*?MyWebMvcConfigurer:?Springboot2.x以后版本使用**/ @Configuration public?class?MyWebMvcConfigurer?implements?WebMvcConfigurer?{/**?攔截器依賴于Spring容器,此處攔截了所有,需要對靜態(tài)資源進(jìn)行放行*/@Overridepublic?void?addInterceptors(InterceptorRegistry?registry)?{//?攔截器默認(rèn)的執(zhí)行順序,就是它的注冊順序,也可以通過Order手動設(shè)置控制,值越小越先執(zhí)行。 //????????registry.addInterceptor(new?PageInterceptor()).addPathPatterns("/**").order()registry.addInterceptor(new?PageInterceptor()).addPathPatterns("/**").excludePathPatterns("/page/login",?"/user/login","/page/ajax","/static/**");}/**?不要要寫控制器即可完成頁面跳轉(zhuǎn)訪問*?@param?registry*/@Overridepublic?void?addViewControllers(ViewControllerRegistry?registry)?{registry.addViewController("/page/ajax").setViewName("ajax");}/**?自定義靜態(tài)資源映射Spring Boot 默認(rèn)為我們提供了靜態(tài)資源映射:classpath:/META-INF/resourcesclasspath:/resourcesclasspath:/staticclasspath:/public優(yōu)先級:META-INF/resources > resources > static > public*?@param?registry**/ //????@Override //????public?void?addResourceHandlers(ResourceHandlerRegistry?registry)?{registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");registry.addResourceHandler("/static/**").addResourceLocations("file:E:/static/"); //????} }3.監(jiān)聽器
實(shí)現(xiàn) javax.servlet.ServletRequestListener, javax.servlet.http.HttpSessionListener, javax.servlet.ServletContextListener 等等接口
主要用來監(jiān)聽對象的創(chuàng)建與銷毀的發(fā)生, 比如 session 的創(chuàng)建銷毀, request 的創(chuàng)建銷毀, ServletContext 創(chuàng)建銷毀
三、注意
1.靜態(tài)資源問題
“SpringBoot2.x以后版本攔截器也會攔截靜態(tài)資源,在配置攔截器是需要將姿態(tài)資源放行。
????/**?攔截器依賴于Spring容器,此處攔截了所有,需要對靜態(tài)資源進(jìn)行放行*/@Overridepublic?void?addInterceptors(InterceptorRegistry?registry)?{registry.addInterceptor(new?PageInterceptor()).addPathPatterns("/**").excludePathPatterns("/page/login",?"/user/login","/page/ajax","/static/**");}“SpringBoot2.x 自定義靜態(tài)資源映射
spring:mvc:static-path-pattern:?/static/**“默認(rèn)目錄 classpath:/META-INF/resources classpath:/resources classpath:/static classpath:/public 優(yōu)先級:META-INF/resources > resources > static > public
2.登錄攔截ajax重定向
“由于ajax是異步的,還在當(dāng)前頁面進(jìn)行的局部請求。當(dāng)攔截到登錄請求時,即使重定向也無法生效。需采用服務(wù)端給地址由前端進(jìn)行跳轉(zhuǎn)。詳細(xì)見登錄攔截器代碼。
//?前端處理 <!DOCTYPE?html> <html?lang="en"> <head><meta?charset="UTF-8"><title>AJAX</title><script?src="https://code.jquery.com/jquery-3.0.0.min.js"></script> </head> <body><button>USER</button> </body> </html><script>$.ajaxSetup({complete:function(xhr,status){//攔截器實(shí)現(xiàn)超時跳轉(zhuǎn)到登錄頁面let?win?=?window;//?通過xhr取得響應(yīng)頭let?REDIRECT?=?xhr.getResponseHeader("REDIRECT");//如果響應(yīng)頭中包含?REDIRECT?則說明是攔截器返回的需要重定向的請求if?(REDIRECT?===?"REDIRECT"){while?(win?!==?win.top){win?=?win.top;}win.location.href?=?xhr.getResponseHeader("CONTEXTPATH");}}});$("button").click(function(){$.get("/page/user",?function(result){$("div").html(result);});}); </script>四、測試
“代碼地址:https://gitee.com/dingwen-gitee/filter-interceptor-study.git
1.攔截器測試
1.1啟動項目訪問首頁
http://localhost:8080/page/index “由于沒有登錄,直接重定向到了登錄頁
1.2輸入用戶名密碼完成登錄,調(diào)轉(zhuǎn)到用戶頁
“此時在訪問首頁
1.2 退出登錄
“成功退出后,訪問為授權(quán)的頁面也相對會被重定向到登錄頁
1.3 ajax未授權(quán)訪問測試
“點(diǎn)擊訪問user ,由于未登錄,沒有全權(quán)訪問。在前端進(jìn)行了頁面跳轉(zhuǎn),轉(zhuǎn)到了登錄頁。
2.過濾器測試
“可以看到過濾器進(jìn)行了相對應(yīng)的處理,重寫的getParameterValues()也生效了。配合使用HttpServletRequestWrapper & OncePerRequestFilter 實(shí)現(xiàn)了對request的修改。
往期推薦Spring為什么建議構(gòu)造器注入?
11個小技巧,玩轉(zhuǎn)Spring!
超級詳細(xì)的Spring Boot 注解總結(jié)
總結(jié)
以上是生活随笔為你收集整理的SpringBoot 过滤器、拦截器、监听器对比及使用场景!的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 枚举迭代_Java中的枚举和迭
- 下一篇: 块元素、行内块和内联元素_如何删除内联块