小編給大家分享一下springboot中過濾器和攔截器如何實現(xiàn),相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
我們提供的服務(wù)有:成都做網(wǎng)站、成都網(wǎng)站設(shè)計、成都外貿(mào)網(wǎng)站建設(shè)、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、鎮(zhèn)巴ssl等。為近1000家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的鎮(zhèn)巴網(wǎng)站制作公司
過濾器和攔截器二者都是AOP編程思想的提現(xiàn),都能實現(xiàn)諸如權(quán)限檢查、日志記錄等。二者有一定的相似之處,不同的地方在于:
Filter是servlet規(guī)范,只能用在Web程序中,而攔截器是Spring規(guī)范,可以用在Web程序中,也可以用在Application程序中。
Filter是servlet中定義的,依賴servlet容器。而攔截器在Spring中定義,依賴Spring容器。
攔截器是一個Spring組件,歸Spring管理,配置在Spring的配置文件中,因此它可使用Spring的任何資源。比如Service、數(shù)據(jù)源等,通過IOC容器注入到攔截器即可,而Filter則不行。
Filter只在servlet前后起作用,而攔截器則能深入到方法前后,異常拋出前后。使用深度更大一些。
public class MyFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("MyFilter init...");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//站點圖標(biāo)/favicon.ico filter會執(zhí)行2次
HttpServletRequest request=(HttpServletRequest) servletRequest;
System.out.println(request.getRequestURI());
System.out.println("MyFilter dofilter...");
filterChain.doFilter(servletRequest,servletResponse);
}
@Override
public void destroy() {
}
}在springboot中注冊Filter
@Configuration
public class FilterConfig {
@Bean
public FilterRegistrationBean registrationBean(){
FilterRegistrationBean myfilter=new FilterRegistrationBean(new MyFilter());
myfilter.addUrlPatterns("/*");
return myfilter;
}到這里運行demo時會發(fā)現(xiàn)do filter執(zhí)行了2次,debug發(fā)現(xiàn)這是因為瀏覽器請求時站點圖標(biāo)管理,通過uri能發(fā)現(xiàn)。可以根據(jù)自己的需求用正則表達式適當(dāng)控制。
@Component
@WebFilter(filterName = "myFilter2",urlPatterns = "/*")
public class MyFilter2 implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("myFilter2 init...");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("myFilter2 dofilter ...");
filterChain.doFilter(servletRequest,servletResponse);
}
@Override
public void destroy() {
}
}使用servleta注解聲明的filter,執(zhí)行時只有一次請求。和使用spring配置filter這里不同。
攔截器主要使用自定義類集成HandlerInterceptor。preHandle返回true時程序才會繼續(xù)向下執(zhí)行,返回false則中斷請求。
public class MyInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("/preHandler");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView)
throws Exception {
System.out.println("postHandler");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
System.out.println("afterCompletion");
}
}在程序中配置攔截器并聲明攔截規(guī)則
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new MyInterceptor()).addPathPatterns("/*");
}
}運行結(jié)果

以上是springboot中過濾器和攔截器如何實現(xiàn)的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
網(wǎng)站欄目:springboot中過濾器和攔截器如何實現(xiàn)
本文路徑:http://www.chinadenli.net/article48/geeihp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、域名注冊、搜索引擎優(yōu)化、小程序開發(fā)、品牌網(wǎng)站建設(shè)、虛擬主機
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)