spring boot設(shè)置過(guò)濾器、監(jiān)聽(tīng)器及攔截器的方法
前言
其實(shí)這篇文章算不上是springboot的東西,我們?cè)趕pring普通項(xiàng)目中也是可以直接使用的
設(shè)置過(guò)濾器:
以前在普通項(xiàng)目中我們要在web.xml中進(jìn)行filter的配置,但是只從servlet 3.0后,我們就可以在直接在項(xiàng)目中進(jìn)行filter的設(shè)置,因?yàn)樗峁┝艘粋€(gè)注解@WebFilter(在javax.servlet.annotation包下),使用這個(gè)注解我們就可以進(jìn)行filter的設(shè)置了,同時(shí)也解決了我們使用springboot項(xiàng)目沒(méi)有web.xml的尷尬,使用方法如下所示
@WebFilter(urlPatterns="/*",filterName="corsFilter", asyncSupported = true) public class CorsFilter implements Filter{ @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException { HttpServletResponse response = (HttpServletResponse)servletResponse; HttpServletRequest request = (HttpServletRequest)servletRequest; chain.doFilter(servletRequest, servletResponse); } @Override public void destroy() { } }
其實(shí)在WebFilter注解中有一些屬性我們需要進(jìn)行設(shè)置, 比如value、urlPatterns,這兩個(gè)屬性其實(shí)都是一樣的作用,都是為了設(shè)置攔截路徑,asyncSupported這個(gè)屬性是設(shè)置配置的filter是否支持異步響應(yīng),默認(rèn)是不支持的,如果我們的項(xiàng)目需要進(jìn)行請(qǐng)求的異步響應(yīng),請(qǐng)求經(jīng)過(guò)了filter,那么這個(gè)filter的asyncSupported屬性必須設(shè)置為true不然請(qǐng)求的時(shí)候會(huì)報(bào)異常。
設(shè)置攔截器:
編寫(xiě)一個(gè)配置類(lèi),繼承org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter或者org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport并重寫(xiě)addInterceptors(InterceptorRegistry registry)方法,其實(shí)父類(lèi)的addInterceptors(InterceptorRegistry registry)方法就是個(gè)空方法。使用方法如下:
@Configuration public class MvcConfig extends WebMvcConfigurationSupport { @Override public void addInterceptors(InterceptorRegistry registry) { InterceptorRegistration registration = registry.addInterceptor(new HandlerInterceptor() { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { } }); // 配置攔截路徑 registration.addPathPatterns("/**"); // 配置不進(jìn)行攔截的路徑 registration.excludePathPatterns("/static/**"); } }
配置監(jiān)聽(tīng)器:
一般我們常用的就是request級(jí)別的javax.servlet.ServletRequestListener和session級(jí)別的javax.servlet.http.HttpSessionListener,下面以ServletRequestListener為例,編寫(xiě)一個(gè)類(lèi)實(shí)現(xiàn)ServletRequestListener接口并實(shí)現(xiàn)requestInitialized(ServletRequestEvent event)方法和requestDestroyed(ServletRequestEvent event)方法,在實(shí)現(xiàn)類(lèi)上加上@WebListener(javax.servlet.annotation包下),如下所示
@WebListener public class RequestListener implements ServletRequestListener { @Override public void requestDestroyed(ServletRequestEvent sre) { System.out.println("請(qǐng)求結(jié)束"); } @Override public void requestInitialized(ServletRequestEvent sre) { System.out.println("請(qǐng)求開(kāi)始"); } }
這樣每一個(gè)請(qǐng)求都會(huì)被監(jiān)聽(tīng)到,在請(qǐng)求處理前equestInitialized(ServletRequestEvent event)方法,在請(qǐng)求結(jié)束后調(diào)用requestDestroyed(ServletRequestEvent event)方法,其實(shí)在spring中有一個(gè)非常好的例子,就是org.springframework.web.context.request.RequestContextListener類(lèi)
public class RequestContextListener implements ServletRequestListener { private static final String REQUEST_ATTRIBUTES_ATTRIBUTE = RequestContextListener.class.getName() + ".REQUEST_ATTRIBUTES"; @Override public void requestInitialized(ServletRequestEvent requestEvent) { if (!(requestEvent.getServletRequest() instanceof HttpServletRequest)) { throw new IllegalArgumentException( "Request is not an HttpServletRequest: " + requestEvent.getServletRequest()); } HttpServletRequest request = (HttpServletRequest) requestEvent.getServletRequest(); ServletRequestAttributes attributes = new ServletRequestAttributes(request); request.setAttribute(REQUEST_ATTRIBUTES_ATTRIBUTE, attributes); LocaleContextHolder.setLocale(request.getLocale()); RequestContextHolder.setRequestAttributes(attributes); } @Override public void requestDestroyed(ServletRequestEvent requestEvent) { ServletRequestAttributes attributes = null; Object reqAttr = requestEvent.getServletRequest().getAttribute(REQUEST_ATTRIBUTES_ATTRIBUTE); if (reqAttr instanceof ServletRequestAttributes) { attributes = (ServletRequestAttributes) reqAttr; } RequestAttributes threadAttributes = RequestContextHolder.getRequestAttributes(); if (threadAttributes != null) { // We're assumably within the original request thread... LocaleContextHolder.resetLocaleContext(); RequestContextHolder.resetRequestAttributes(); if (attributes == null && threadAttributes instanceof ServletRequestAttributes) { attributes = (ServletRequestAttributes) threadAttributes; } } if (attributes != null) { attributes.requestCompleted(); } } }
在這個(gè)類(lèi)中,spring將每一個(gè)請(qǐng)求開(kāi)始前都將請(qǐng)求進(jìn)行了一次封裝并設(shè)置了一個(gè)threadLocal,這樣我們?cè)谡?qǐng)求處理的任何地方都可以通過(guò)這個(gè)threadLocal獲取到請(qǐng)求對(duì)象,好處當(dāng)然是有的啦,比如我們?cè)趕ervice層需要用到request的時(shí)候,可以不需要調(diào)用者傳request對(duì)象給我們,我們可以通過(guò)一個(gè)工具類(lèi)就可以獲取,豈不美哉。
擴(kuò)充:在springboot的啟動(dòng)類(lèi)中我們可以添加一些ApplicationListener監(jiān)聽(tīng)器,例如:
@SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication application = new SpringApplication(DemoApplication.class); application.addListeners(new ApplicationListener<ApplicationEvent>() { @Override public void onApplicationEvent(ApplicationEvent event) { System.err.println(event.toString()); } }); application.run(args); } }
ApplicationEvent是一個(gè)抽象類(lèi),她的子類(lèi)有很多比如ServletRequestHandledEvent(發(fā)生請(qǐng)求事件的時(shí)候觸發(fā))、ApplicationStartedEvent(應(yīng)用開(kāi)始前觸發(fā),做一些啟動(dòng)準(zhǔn)備工作)、ContextRefreshedEvent(容器初始化結(jié)束后觸發(fā)),其他還有很多,這里不再多說(shuō),但是這些ApplicationListener只能在springboot項(xiàng)目以main方法啟動(dòng)的時(shí)候才會(huì)生效,也就是說(shuō)項(xiàng)目要打jar包時(shí)才適用,如果打war包,放在Tomcat等web容器中是沒(méi)有效果的。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。
- SpringBoot 過(guò)濾器, 攔截器, 監(jiān)聽(tīng)器的具體使用
- SpringBoot使用過(guò)濾器、攔截器和監(jiān)聽(tīng)器的案例代碼(Springboot搭建java項(xiàng)目)
- SpringBoot 過(guò)濾器、攔截器、監(jiān)聽(tīng)器對(duì)比及使用場(chǎng)景分析
- SpringBoot實(shí)現(xiàn)攔截器、過(guò)濾器、監(jiān)聽(tīng)器過(guò)程解析
- SpringBoot定義過(guò)濾器、監(jiān)聽(tīng)器、攔截器的方法
- Spring?Boot中的過(guò)濾器攔截器監(jiān)聽(tīng)器使用技巧匯總
相關(guān)文章
Java強(qiáng)制保留兩位小數(shù)的四種方法案例詳解
這篇文章主要介紹了Java強(qiáng)制保留兩位小數(shù)的四種方法案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09異常try?catch的常見(jiàn)四類(lèi)方式(案例代碼)
這篇文章主要介紹了異常try?catch的常見(jiàn)四類(lèi)方式,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-05-05SpringBoot啟動(dòng)指定profile的多種方式
這篇文章主要介紹了SpringBoot啟動(dòng)指定profile的多種方式,本文通過(guò)圖文實(shí)例相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09Java中可以實(shí)現(xiàn)負(fù)載均衡的算法詳解
這篇文章主要介紹了Java中可以實(shí)現(xiàn)負(fù)載均衡的算法詳解,在Java中,有多種算法可以實(shí)現(xiàn)負(fù)載均衡,下面是兩個(gè)常見(jiàn)的算法示例,隨機(jī)算法和輪詢(xún)算法,需要的朋友可以參考下2023-08-08Java 8 開(kāi)發(fā)的 Mybatis 注解代碼生成工具
MybatisAnnotationTools 是基于 Java8 開(kāi)發(fā)的一款可以用于自動(dòng)化生成 MyBatis 注解類(lèi)的工具,支持配置數(shù)據(jù)源、類(lèi)路徑,表名去前綴、指定類(lèi)名前后綴等功能.這篇文章主要介紹了Java 8 開(kāi)發(fā)的 Mybatis 注解代碼生成工具 ,需要的朋友可以參考下2019-07-07如何使用Comparator比較接口實(shí)現(xiàn)ArrayList集合排序
這篇文章主要介紹了如何使用Comparator比較接口實(shí)現(xiàn)ArrayList集合排序問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12在Windows系統(tǒng)下安裝Thrift的方法與使用講解
今天小編就為大家分享一篇關(guān)于在Windows系統(tǒng)下安裝Thrift的方法與使用講解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12基于Feign實(shí)現(xiàn)異步調(diào)用
近期,需要對(duì)之前的接口進(jìn)行優(yōu)化,縮短接口的響應(yīng)時(shí)間,但是springcloud中的feign是不支持傳遞異步化的回調(diào)結(jié)果的,因此有了以下的解決方案,記錄一下,需要的朋友可以參考下2021-05-05