spring boot 注冊攔截器過程詳解
攔截器是動態(tài)攔截Action調(diào)用的對象。它提供了一種機(jī)制可以使開發(fā)者可以定義在一個(gè)action執(zhí)行的前后執(zhí)行的代碼,也可以在一個(gè)action執(zhí)行前阻止其執(zhí)行,同時(shí)也提供了一種可以提取action中可重用部分的方式。在AOP(Aspect-Oriented Programming)中攔截器用于在某個(gè)方法或字段被訪問之前,進(jìn)行攔截然后在之前或之后加入某些操作。
如何在spring boot中添加攔截器?
1.首先自己實(shí)現(xiàn)一個(gè)攔截器
import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class MyInterceptor implements HandlerInterceptor{ @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("preHandler"); return true;//這里一定要返回true要不然后面的不會執(zhí)行 } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("postHandler"); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("afterHandler"); } }
2.實(shí)現(xiàn)WebMvcConfigurer接口并重寫addInterceptors方法
@Configuration public class WebMvcConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(myInterceptor()).addPathPatterns("/**");//將自己的攔截器注冊到spring中并添加攔截的路徑 } @Bean MyInterceptor myInterceptor(){ return new MyInterceptor();//提供自己的攔截器的bean } }
3.創(chuàng)建controller進(jìn)行測試
@RestController public class HelloController { @GetMapping("gethello") public String getHello(){ System.out.println("get hello"); return "get hello"; } }
4.測試結(jié)果
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- SpringBoot實(shí)現(xiàn)登錄注冊常見問題解決方案
- springboot實(shí)現(xiàn)注冊加密與登錄解密功能(demo)
- SpringBoot使用郵箱發(fā)送驗(yàn)證碼實(shí)現(xiàn)注冊功能
- SpringBoot集成RabbitMQ實(shí)現(xiàn)用戶注冊的示例代碼
- springboot快速整合Mybatis組件的方法(推薦)
- SpringCloud微服務(wù)之Hystrix組件實(shí)現(xiàn)服務(wù)熔斷的方法
- Spring Cloud重試機(jī)制與各組件的重試總結(jié)
- Spring容器注冊組件實(shí)現(xiàn)過程解析
相關(guān)文章
詳解SpringBoot中@SessionAttributes的使用
這篇文章主要通過示例為大家詳細(xì)介紹了SpringBoot中@SessionAttributes的使用,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-07-07微服務(wù)領(lǐng)域Spring Boot自動伸縮的實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于微服務(wù)領(lǐng)域Spring Boot自動伸縮的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用spring boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-10-10Spring遠(yuǎn)程調(diào)用HttpClient/RestTemplate的方法
這篇文章主要介紹了Spring遠(yuǎn)程調(diào)用HttpClient/RestTemplate的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03Java 動態(tài)數(shù)組的實(shí)現(xiàn)示例
Java動態(tài)數(shù)組是一種可以任意伸縮數(shù)組長度的對象,本文主要介紹了Java 動態(tài)數(shù)組的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08Java commons-httpclient如果實(shí)現(xiàn)get及post請求
這篇文章主要介紹了Java commons-httpclient如果實(shí)現(xiàn)get及post請求,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09@JsonFormat處理LocalDateTime失效的問題
這篇文章主要介紹了關(guān)于@JsonFormat處理LocalDateTime失效的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08MyBatis學(xué)習(xí)筆記(二)之關(guān)聯(lián)關(guān)系
這篇文章主要介紹了MyBatis學(xué)習(xí)筆記(二)之關(guān)聯(lián)關(guān)系 的相關(guān)資料,需要的朋友可以參考下2016-02-02