您好,登錄后才能下訂單哦!
前言
在編寫過(guò)濾器、監(jiān)聽(tīng)器、攔截器之前我們需要在spring-boot啟動(dòng)的類上加上注解@ServletComponentScan:
@SpringBootApplication @ServletComponentScan public class MySpringbootApplication { public static void main(String[] args) { SpringApplication.run(MySpringbootApplication.class, args); } }
Servlet
spring-boot編寫過(guò)濾器和spring中差不多,直接看代碼:
@WebServlet(urlPatterns = "/serv") public class MyServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) { System.out.println("------------doget-------------"); doPost(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) { System.out.println("------------dopost-------------"); } }
其實(shí)也就是注解的不同而已:
@WebServlet(urlPatterns = "/serv")
過(guò)濾器(Filter)
在spring-boot里編寫過(guò)濾器我們只需要實(shí)現(xiàn)javax.servlet.Filter
@WebFilter(filterName = "myFilter", urlPatterns = "/*") public class MyFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { System.out.println("初始化過(guò)濾器"); } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("執(zhí)行過(guò)濾器"); filterChain.doFilter(servletRequest, servletResponse); } @Override public void destroy() { System.out.println("銷毀過(guò)濾器!"); } }
然后添加一個(gè)注解:
@WebFilter(filterName = "myFilter", urlPatterns = "/*")
監(jiān)聽(tīng)器 (Listener)
在上面,看了下過(guò)濾器的使用。其實(shí)監(jiān)聽(tīng)器和攔截器就差不多了,直接上代碼:
@WebListener public class MyHttpSessionListener implements HttpSessionListener { @Override public void sessionCreated(HttpSessionEvent httpSessionEvent) { System.out.println("session 被創(chuàng)建"); } @Override public void sessionDestroyed(HttpSessionEvent httpSessionEvent) { System.out.println("session 被摧毀"); } }
我們發(fā)現(xiàn)只是注解發(fā)生了變化:
@WebListener
攔截器(Interceptor)
攔截器大致和上面差不多,不過(guò)有一點(diǎn)點(diǎn)不同。我們知道在web開(kāi)發(fā)中,可以使用過(guò)濾器和攔截器來(lái)過(guò)濾外部的web請(qǐng)求。但是攔截器提供了更加細(xì)致的控制功能。主要有:請(qǐng)求之前、請(qǐng)求之后渲染之前、渲染之后、請(qǐng)求全部結(jié)束之后這四個(gè)步驟的攔截。
這里面使用攔截器主要有三個(gè)步驟
自定義攔截器,實(shí)現(xiàn)org.springframework.web.servlet.HandlerInterceptor
自定義WebAppConfigurer,繼承WebMvcConfigurerAdapter
在自定義的WebAppConfigurer覆蓋父類方法addInterceptors(InterceptorRegistry registry),并在方法中添加自己定義的攔截器
public class MyInterceptor implements HandlerInterceptor{ @Override public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception { System.out.println(MyInterceptor.class.getName()+" : 在請(qǐng)求之前調(diào)用"); return true; } @Override public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception { System.out.println(MyInterceptor.class.getName()+" :請(qǐng)求處理之后視圖渲染之前使用"); } @Override public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception { System.out.println(MyInterceptor.class.getName()+" :請(qǐng)視圖渲染之后使用"); } } @Configuration public class MyWebAppConfigurer extends WebMvcConfigurerAdapter { @Override public void addInterceptors(InterceptorRegistry registry) { // 多個(gè)攔截器組成一個(gè)攔截器鏈 // addPathPatterns 用于添加攔截規(guī)則 // excludePathPatterns 用戶排除攔截 registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**"); registry.addInterceptor(new MyInterceptor2()).addPathPatterns("/**"); super.addInterceptors(registry); } }
以上就是關(guān)于在spring-boot中如何定義過(guò)濾器、監(jiān)聽(tīng)器和攔截器。關(guān)于他們的原理以及一些細(xì)節(jié)問(wèn)題(如攔截器的攔截順序),就不詳述。有興趣的可以去網(wǎng)上搜索。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。