springboot中過(guò)濾器和攔截器如何實(shí)現(xiàn)

小編給大家分享一下springboot中過(guò)濾器和攔截器如何實(shí)現(xiàn),相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

我們提供的服務(wù)有:成都做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)、成都外貿(mào)網(wǎng)站建設(shè)、微信公眾號(hào)開(kāi)發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、鎮(zhèn)巴ssl等。為近1000家企事業(yè)單位解決了網(wǎng)站和推廣的問(wèn)題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的鎮(zhèn)巴網(wǎng)站制作公司

過(guò)濾器和攔截器二者都是AOP編程思想的提現(xiàn),都能實(shí)現(xiàn)諸如權(quán)限檢查、日志記錄等。二者有一定的相似之處,不同的地方在于:

  • Filter是servlet規(guī)范,只能用在Web程序中,而攔截器是Spring規(guī)范,可以用在Web程序中,也可以用在Application程序中。

  • Filter是servlet中定義的,依賴(lài)servlet容器。而攔截器在Spring中定義,依賴(lài)Spring容器。

  • 攔截器是一個(gè)Spring組件,歸Spring管理,配置在Spring的配置文件中,因此它可使用Spring的任何資源。比如Service、數(shù)據(jù)源等,通過(guò)IOC容器注入到攔截器即可,而Filter則不行。

  • Filter只在servlet前后起作用,而攔截器則能深入到方法前后,異常拋出前后。使用深度更大一些。

Spring中實(shí)現(xiàn)過(guò)濾器Filter

方法1:使用springboot提供的 FilterRegistrationBean注冊(cè)自定義過(guò)濾器
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 {
        //站點(diǎn)圖標(biāo)/favicon.ico  filter會(huì)執(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中注冊(cè)Filter

@Configuration
public class FilterConfig {

    @Bean
    public FilterRegistrationBean registrationBean(){
        FilterRegistrationBean myfilter=new FilterRegistrationBean(new MyFilter());
        myfilter.addUrlPatterns("/*");

        return myfilter;
    }

到這里運(yùn)行demo時(shí)會(huì)發(fā)現(xiàn)do filter執(zhí)行了2次,debug發(fā)現(xiàn)這是因?yàn)闉g覽器請(qǐng)求時(shí)站點(diǎn)圖標(biāo)管理,通過(guò)uri能發(fā)現(xiàn)??梢愿鶕?jù)自己的需求用正則表達(dá)式適當(dāng)控制。

方法2:servlet注解定義Filter
@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í)行時(shí)只有一次請(qǐng)求。和使用spring配置filter這里不同。

Spring中實(shí)現(xiàn)攔截器

   攔截器主要使用自定義類(lèi)集成HandlerInterceptor。preHandle返回true時(shí)程序才會(huì)繼續(xù)向下執(zhí)行,返回false則中斷請(qǐng)求。

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("/*");
    }
}

運(yùn)行結(jié)果

springboot中過(guò)濾器和攔截器如何實(shí)現(xiàn)

以上是springboot中過(guò)濾器和攔截器如何實(shí)現(xiàn)的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

網(wǎng)站欄目:springboot中過(guò)濾器和攔截器如何實(shí)現(xiàn)
本文路徑:http://www.muchs.cn/article48/geeihp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈域名注冊(cè)、搜索引擎優(yōu)化小程序開(kāi)發(fā)、品牌網(wǎng)站建設(shè)、虛擬主機(jī)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)