SpringBoot使用Interceptor攔截器的實(shí)例
在springboot中使用攔截器也比較簡(jiǎn)單,實(shí)現(xiàn)HandlerInterceptor或者AsyncHandlerInterceptor接口,再從配置里添加一下攔截器就完成了;
AsyncHandlerInterceptor接口繼承了HandlerInterceptor,多了一個(gè)afterConcurrentHandlingStarted方法:
接口里的方法:
- preHandle:在Controller之前執(zhí)行,可以判斷參數(shù),執(zhí)行的controller方法等,返回值為boolean,返回true繼續(xù)往下運(yùn)行(下面的攔截器和controller),否則開始返回操作(執(zhí)行之前的攔截器返回等操作);
- postHandle:在Controller之后,視圖返回前執(zhí)行,可對(duì)ModelAndView進(jìn)行處理再返回;
- afterCompletion:請(qǐng)求完成后執(zhí)行;
- afterConcurrentHandlingStarted:controller返回值是java.util.concurrent.Callable時(shí)才會(huì)調(diào)用該方法并使用新線程運(yùn)行;
方法執(zhí)行順序有兩種:
- preHandle -> 執(zhí)行Controller -> postHandle -> afterCompletion;
- preHandle -> 執(zhí)行Controller -> afterConcurrentHandlingStarted -> callable線程執(zhí)行call()方法 -> 新線程開始preHandle -> postHandle -> afterCompletion;(controller方法返回Callable對(duì)象時(shí))
配置攔截器:
實(shí)現(xiàn)WebMvcConfigurer接口里的addInterceptors方法,使用參數(shù)InterceptorRegistry對(duì)象添加自己的攔截器,可以添加指定攔截路徑或者去掉某些過濾路徑,還可以設(shè)置攔截器的優(yōu)先級(jí)order,優(yōu)先級(jí)由小到大,默認(rèn)0;
多個(gè)攔截器的執(zhí)行順序:
preHandle方法按照order由小到大順序,執(zhí)行完controller后,其他方法則反向順序,跟過濾器Filter類似;
測(cè)試啟動(dòng)類,默認(rèn)配置:
/** * 2023年3月16日下午4:56:23 */ package testspringboot.test9interceptor; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @author XWF * */ @SpringBootApplication public class Test9Main { /** * @param args */ public static void main(String[] args) { SpringApplication.run(Test9Main.class, args); } }
controller類:
/** * 2023年3月16日下午4:58:02 */ package testspringboot.test9interceptor; import java.util.concurrent.Callable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @author XWF * */ @RestController @RequestMapping("/interceptor") public class Test9Controller { @RequestMapping("/a") public String a(String s) { System.out.println(">>>a():" + s); return "OK"; } @RequestMapping("/b") public Callable<String> b() { Callable<String> callable = new Callable<String>() { @Override public String call() throws Exception { Thread.sleep(2000); System.out.println("call() thread id=" + Thread.currentThread().getId()); Thread.sleep(2000); return "abcdefg"; } }; System.out.println(">>>b()"); return callable; } }
兩個(gè)自定義攔截器1和2:
/** * 2023年3月16日下午5:14:14 */ package testspringboot.test9interceptor; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; /** * @author XWF * */ public class MyInterceptor1 implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("preHandle 1, handler=" + handler); return request.getQueryString().length() < 10 ? true : false; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("postHandle 1"); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("afterCompletion 1"); } }
/** * 2023年3月16日下午5:15:28 */ package testspringboot.test9interceptor; import java.util.Date; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Component; import org.springframework.web.servlet.AsyncHandlerInterceptor; import org.springframework.web.servlet.ModelAndView; /** * @author XWF * */ @Component public class MyInterceptor2 implements AsyncHandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("preHandle 2 " + new Date() + " ThreadId=" + Thread.currentThread().getId()); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("postHandle 2"); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("afterCompletion 2"); } @Override public void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("afterConcurrentHandlingStarted 2 " + new Date()); } }
配置攔截器:
/** * 2023年3月16日下午5:20:31 */ package testspringboot.test9interceptor; import javax.annotation.Resource; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; /** * @author XWF * */ @Configuration public class MyInterceptorConfig implements WebMvcConfigurer { @Resource MyInterceptor2 myinterceptor2; @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new MyInterceptor1()) .addPathPatterns("/interceptor/a") //添加攔截路徑,兩種參數(shù)List<String>和String ... .excludePathPatterns("/interceptor/b") //排除路徑,兩種參數(shù)List<String>和String ... .order(1); //設(shè)置攔截器順序,由小到大,默認(rèn)0 registry.addInterceptor(myinterceptor2); //也可以使用spring管理的對(duì)象 } }
發(fā)送一個(gè)post測(cè)試請(qǐng)求:http://192.168.1.30:8080/interceptor/a?s=hello,攔截器2的order默認(rèn)0,攔截器1的order為1,preHandle先執(zhí)行2的,controller執(zhí)行之后,剩下的Handle都是先執(zhí)行1再執(zhí)行2的;
發(fā)送preHandle返回false的請(qǐng)求:http://192.168.1.30:8080/interceptor/a?s=hello123456789,攔截器1的preHandle返回false后,直接執(zhí)行2的afterCompletion;
發(fā)送測(cè)試callable的請(qǐng)求:http://192.168.1.30:8080/interceptor/b?s=hello,攔截路徑配置跳過攔截器1只執(zhí)行攔截器2,通過threadid可以看到前后使用的是兩個(gè)線程;
到此這篇關(guān)于SpringBoot使用Interceptor攔截器的文章就介紹到這了,更多相關(guān)SpringBoot使用Interceptor攔截器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
FutureTask為何單個(gè)任務(wù)僅執(zhí)行一次原理解析
這篇文章主要為大家介紹了FutureTask為何單個(gè)任務(wù)僅執(zhí)行一次原理解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11Java關(guān)于桶排序的知識(shí)點(diǎn)總結(jié)
這篇文章給大家總結(jié)了關(guān)于JAVA中J桶排序的相關(guān)知識(shí)點(diǎn)和用法分享,有興趣的讀者跟著學(xué)習(xí)下。2018-04-04spring中jdbcTemplate.batchUpdate的幾種使用情況
本文主要介紹了spring中jdbcTemplate.batchUpdate的幾種使用情況,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04Java循環(huán)結(jié)構(gòu)之多重循環(huán)及continue?break
這篇文章主要介紹了Java循環(huán)結(jié)構(gòu)之多重循環(huán)及continue?break,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-09-09SpringBoot集成nacos動(dòng)態(tài)刷新數(shù)據(jù)源的實(shí)現(xiàn)示例
這篇文章主要介紹了SpringBoot集成nacos動(dòng)態(tài)刷新數(shù)據(jù)源的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12Java解決線程的不安全問題之volatile關(guān)鍵字詳解
這篇文章主要介紹了Java解決線程的不安全問題之volatile關(guān)鍵字詳解,可見性指一個(gè)線程對(duì)共享變量值的修改,能夠及時(shí)地被其他線程看到,而 volatile 關(guān)鍵字就保證內(nèi)存的可見性,需要的朋友可以參考下2023-08-08