前言
專注于為中小企業(yè)提供做網(wǎng)站、成都做網(wǎng)站服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)淇濱免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了千余家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
其實(shí)這篇文章算不上是springboot的東西,我們在spring普通項(xiàng)目中也是可以直接使用的
設(shè)置過濾器:
以前在普通項(xiàng)目中我們要在web.xml中進(jìn)行filter的配置,但是只從servlet 3.0后,我們就可以在直接在項(xiàng)目中進(jìn)行filter的設(shè)置,因?yàn)樗峁┝艘粋€注解@WebFilter(在javax.servlet.annotation包下),使用這個注解我們就可以進(jìn)行filter的設(shè)置了,同時也解決了我們使用springboot項(xiàng)目沒有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,這兩個屬性其實(shí)都是一樣的作用,都是為了設(shè)置攔截路徑,asyncSupported這個屬性是設(shè)置配置的filter是否支持異步響應(yīng),默認(rèn)是不支持的,如果我們的項(xiàng)目需要進(jìn)行請求的異步響應(yīng),請求經(jīng)過了filter,那么這個filter的asyncSupported屬性必須設(shè)置為true不然請求的時候會報異常。
設(shè)置攔截器:
編寫一個配置類,繼承org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter或者org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport并重寫addInterceptors(InterceptorRegistry registry)方法,其實(shí)父類的addInterceptors(InterceptorRegistry registry)方法就是個空方法。使用方法如下:
@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)聽器:
一般我們常用的就是request級別的javax.servlet.ServletRequestListener和session級別的javax.servlet.http.HttpSessionListener,下面以ServletRequestListener為例,編寫一個類實(shí)現(xiàn)ServletRequestListener接口并實(shí)現(xiàn)requestInitialized(ServletRequestEvent event)方法和requestDestroyed(ServletRequestEvent event)方法,在實(shí)現(xiàn)類上加上@WebListener(javax.servlet.annotation包下),如下所示
@WebListener public class RequestListener implements ServletRequestListener { @Override public void requestDestroyed(ServletRequestEvent sre) { System.out.println("請求結(jié)束"); } @Override public void requestInitialized(ServletRequestEvent sre) { System.out.println("請求開始"); } }
這樣每一個請求都會被監(jiān)聽到,在請求處理前equestInitialized(ServletRequestEvent event)方法,在請求結(jié)束后調(diào)用requestDestroyed(ServletRequestEvent event)方法,其實(shí)在spring中有一個非常好的例子,就是org.springframework.web.context.request.RequestContextListener類
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(); } } }
在這個類中,spring將每一個請求開始前都將請求進(jìn)行了一次封裝并設(shè)置了一個threadLocal,這樣我們在請求處理的任何地方都可以通過這個threadLocal獲取到請求對象,好處當(dāng)然是有的啦,比如我們在service層需要用到request的時候,可以不需要調(diào)用者傳request對象給我們,我們可以通過一個工具類就可以獲取,豈不美哉。
擴(kuò)充:在springboot的啟動類中我們可以添加一些ApplicationListener監(jiān)聽器,例如:
@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是一個抽象類,她的子類有很多比如ServletRequestHandledEvent(發(fā)生請求事件的時候觸發(fā))、ApplicationStartedEvent(應(yīng)用開始前觸發(fā),做一些啟動準(zhǔn)備工作)、ContextRefreshedEvent(容器初始化結(jié)束后觸發(fā)),其他還有很多,這里不再多說,但是這些ApplicationListener只能在springboot項(xiàng)目以main方法啟動的時候才會生效,也就是說項(xiàng)目要打jar包時才適用,如果打war包,放在Tomcat等web容器中是沒有效果的。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對創(chuàng)新互聯(lián)的支持。
網(wǎng)站欄目:springboot設(shè)置過濾器、監(jiān)聽器及攔截器的方法
網(wǎng)站URL:http://bm7419.com/article48/jcsdhp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗(yàn)、微信小程序、網(wǎng)站內(nèi)鏈、品牌網(wǎng)站設(shè)計(jì)、App開發(fā)、外貿(mào)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)