日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

SpringBoot 过滤器、拦截器、监听器对比及使用场景!

發布時間:2025/3/11 javascript 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringBoot 过滤器、拦截器、监听器对比及使用场景! 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

來源 |?blog.csdn.net/qq_38020915/article/details/116431612

作者 |?dingwen_blog

一、關系圖理解

二、區別

1.過濾器

  • 過濾器是在web應用啟動的時候初始化一次, 在web應用停止的時候銷毀

  • 可以對請求的URL進行過濾, 對敏感詞過濾

  • 擋在攔截器的外層

  • 實現的是 javax.servlet.Filter 接口 ,是 Servlet 規范的一部分

  • 在請求進入容器后,但在進入servlet之前進行預處理,請求結束是在servlet處理完以后

  • 依賴Web容器

  • 會多次執行

1.1HttpServletRequestWrapper

在請求到達之前對 request 進行修改

package?com.dingwen.lir.filter;import?lombok.extern.slf4j.Slf4j;import?javax.servlet.http.HttpServletRequest; import?javax.servlet.http.HttpServletRequestWrapper; import?java.util.Arrays;/***??在請求到達之前對?request?進行修改*/ @Slf4j public?class?RequestWrapper?extends?HttpServletRequestWrapper?{public?RequestWrapper(HttpServletRequest?request)?{super(request);log.info("RequestWrapper");}@Overridepublic?String?getParameter(String?name)?{//?可以對請求參數進行過濾return?super.getParameter(name);}@Overridepublic?String[]?getParameterValues(String?name)?{//?對請求參數值進行過濾 //????????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.*?大家常識上都認為,一次請求本來就只filter一次,為什么還要由此特別限定呢,往往我們的常識和實際的實現并不真的一樣,經過一番資料的查閱,此方法是為了兼容不同的web?container,*?也就是說并不是所有的container都入我們期望的只過濾一次,servlet版本不同,執行過程也不同,*?因此,為了兼容各種不同運行環境和版本,默認filter繼承OncePerRequestFilter是一個比較穩妥的選擇。**/ @Slf4j public?class?RequestFilter?extends?OncePerRequestFilter?{@Overridepublic?void?destroy()?{super.destroy();log.info("RequestFilter?destroy");}/*OncePerRequestFilter.doFilter方法中通過request.getAttribute判斷當前過濾器是否已執行若未執行過,則調用doFilterInternal方法,交由其子類實現*/@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");//過濾器的級別,值越小級別越高越先執行registrationBean.setOrder(1);return?registrationBean;} }

2.攔截器

  • 實現 org.springframework.web.servlet.HandlerInterceptor 接口,動態代理

  • 攔截器應用場景, 性能分析, 權限檢查, 日志記錄

  • 是一個Spring組件,并由Spring容器管理,并不

  • 依賴Tomcat等容器,是可以單獨使用的。不僅能應用在web程序中,也可以用于Application、Swing等程序中

  • 是在請求進入servlet后,在進入Controller之前進行預處理的,Controller 中渲染了對應的視圖之后請求結束

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?{//?不管是轉發還是重定向,必須返回false。否則出現多次提交響應的錯誤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//獲取當前請求的路徑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容器,此處攔截了所有,需要對靜態資源進行放行*/@Overridepublic?void?addInterceptors(InterceptorRegistry?registry)?{//?攔截器默認的執行順序,就是它的注冊順序,也可以通過Order手動設置控制,值越小越先執行。 //????????registry.addInterceptor(new?PageInterceptor()).addPathPatterns("/**").order()registry.addInterceptor(new?PageInterceptor()).addPathPatterns("/**").excludePathPatterns("/page/login",?"/user/login","/page/ajax","/static/**");}/**?不要要寫控制器即可完成頁面跳轉訪問*?@param?registry*/@Overridepublic?void?addViewControllers(ViewControllerRegistry?registry)?{registry.addViewController("/page/ajax").setViewName("ajax");}/**?自定義靜態資源映射Spring Boot 默認為我們提供了靜態資源映射:classpath:/META-INF/resourcesclasspath:/resourcesclasspath:/staticclasspath:/public優先級: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.監聽器

  • 實現 javax.servlet.ServletRequestListener, javax.servlet.http.HttpSessionListener, javax.servlet.ServletContextListener 等等接口

  • 主要用來監聽對象的創建與銷毀的發生, 比如 session 的創建銷毀, request 的創建銷毀, ServletContext 創建銷毀

三、注意

1.靜態資源問題

SpringBoot2.x以后版本攔截器也會攔截靜態資源,在配置攔截器是需要將姿態資源放行。

????/**?攔截器依賴于Spring容器,此處攔截了所有,需要對靜態資源進行放行*/@Overridepublic?void?addInterceptors(InterceptorRegistry?registry)?{registry.addInterceptor(new?PageInterceptor()).addPathPatterns("/**").excludePathPatterns("/page/login",?"/user/login","/page/ajax","/static/**");}“

SpringBoot2.x 自定義靜態資源映射

spring:mvc:static-path-pattern:?/static/**“

默認目錄 classpath:/META-INF/resources classpath:/resources classpath:/static classpath:/public 優先級:META-INF/resources > resources > static > public

2.登錄攔截ajax重定向

由于ajax是異步的,還在當前頁面進行的局部請求。當攔截到登錄請求時,即使重定向也無法生效。需采用服務端給地址由前端進行跳轉。詳細見登錄攔截器代碼。

//?前端處理 <!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){//攔截器實現超時跳轉到登錄頁面let?win?=?window;//?通過xhr取得響應頭let?REDIRECT?=?xhr.getResponseHeader("REDIRECT");//如果響應頭中包含?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輸入用戶名密碼完成登錄,調轉到用戶頁

此時在訪問首頁

1.2 退出登錄

成功退出后,訪問為授權的頁面也相對會被重定向到登錄頁

1.3 ajax未授權訪問測試

點擊訪問user ,由于未登錄,沒有全權訪問。在前端進行了頁面跳轉,轉到了登錄頁。

2.過濾器測試

可以看到過濾器進行了相對應的處理,重寫的getParameterValues()也生效了。配合使用HttpServletRequestWrapper & OncePerRequestFilter 實現了對request的修改。

往期推薦

Spring為什么建議構造器注入?


11個小技巧,玩轉Spring!


超級詳細的Spring Boot 注解總結



總結

以上是生活随笔為你收集整理的SpringBoot 过滤器、拦截器、监听器对比及使用场景!的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。