SpringBoot中Filter和Interceptor如何使用-創(chuàng)新互聯(lián)

SpringBoot中Filter和Interceptor如何使用,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

成都創(chuàng)新互聯(lián)公司于2013年創(chuàng)立,先為南譙等服務(wù)建站,南譙等地企業(yè),進行企業(yè)商務(wù)咨詢服務(wù)。為南譙企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。

一、Filter(過濾器)

Filter接口定義在javax.servlet包中,是Servlet規(guī)范定義的,作用于Request/Response前后,被Servlet容器調(diào)用,當(dāng)Filter被Sring管理后可以使用Spring容器資源。

實現(xiàn)一個Filter

自定義的過濾器需要實現(xiàn)javax.servlet.Filter,F(xiàn)ilter接口中有三個方法:

init(FilterConfig filterConfig):過濾器初始化的被調(diào)用。  doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain):在doFilter()方法中,chain.doFilter()前的一般是對request執(zhí)行的過濾操作,chain.doFilter后面的代碼一般是對response執(zhí)行的操作,chain.doFiter()執(zhí)行下一個過濾器或者業(yè)務(wù)處理器。  destory():過濾器銷毀的時候被調(diào)用。

在Spring容器中使用過濾器

通過FilterRegistrationBean

@Configuration public class WebConfig{  @Bean  public FilterRegistrationBean xxxFilter() {    FilterRegistrationBean registrationBean = new FilterRegistrationBean();    registrationBean.setFilter(new xxxFilter());    registrationBean.setUrlPatterns(Arrays.asList("/*"));    registrationBean.setOrder(1); // 過濾器的優(yōu)先級    return registrationBean;  }}

通過@WebFilter和@ServletComponentScan

通過@WebFilter的方式定義Filter,默認使用Filter的類名設(shè)置優(yōu)先級。使用FilterRegistrationBean可以指定優(yōu)先級。Filter使用白名單過濾Url的方式,配置需要攔截的Url,如果想設(shè)置不過濾某些Url需要在doFilter方法中指定。

二、Interceptor(攔截器)

定義一個Interceptor需要實現(xiàn)org.springframework.web.servlet.HandlerInterceptor接口,Interceptor是Spring容器定義的,它可以使用Spring容器的任何資源,只要通過IoC注入到Interceptor即可,Interceptor可以深入到業(yè)務(wù)處理方法的執(zhí)行前后和拋出異常的時候,而Filerter無法做到這一點,所以Interceptor相比Filter具有更大的彈性。

實現(xiàn)一個Interceptor

實現(xiàn)HandlerInterceptor或者繼承HandlerInterceptorAdapter

public interface HandlerInterceptor {  default boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {    return true;  }  default void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {  }  default void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {  }}

preHandle:在業(yè)務(wù)處理器處理請求之前被調(diào)用。  postHandle:在業(yè)務(wù)處理器處理請求后并生成視圖前被調(diào)用,此時有機會修改ModelAndView。  afterCompletion:業(yè)務(wù)處理處理器處理完請求后(已經(jīng)渲染視圖)被執(zhí)行,并可以處理業(yè)務(wù)方法發(fā)生異常的場景。

在Spring容器中使用攔截器

@Configurationpublic class WebConfig implements WebMvcConfigurer {  @Override  public void addInterceptors(InterceptorRegistry registry) {    InterceptorRegistration registration = registry.addInterceptor(new TimeInterceptor());    registration.excludePathPatterns("/user");    registration.excludePathPatterns("/*");  }}

Interceptor既可以指定要過濾的Url也可以指定不攔截的Url,缺省情況攔截所有Url。

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,的支持。

網(wǎng)站標題:SpringBoot中Filter和Interceptor如何使用-創(chuàng)新互聯(lián)
瀏覽路徑:http://bm7419.com/article46/cdgehg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計、軟件開發(fā)、網(wǎng)站設(shè)計ChatGPT、動態(tài)網(wǎng)站、網(wǎng)站策劃

廣告

聲明:本網(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)

網(wǎng)站優(yōu)化排名