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