Spring Boot 定義過濾器、監聽器、攔截器

1、自定義過濾器java

建立一個過濾器,實現javax.servlet.Filter接口,並重寫其中的init、doFilter、destory方法。web

[java] view plain copy
package com.example.filter;  
  
import java.io.IOException;  
  
import javax.servlet.Filter;  
import javax.servlet.FilterChain;  
import javax.servlet.FilterConfig;  
import javax.servlet.ServletException;  
import javax.servlet.ServletRequest;  
import javax.servlet.ServletResponse;  
import javax.servlet.annotation.WebFilter;  
/** 
 * 實現javax.servlet.Filter,覆蓋其三個方法 
 * @author Administrator 
 * 
 */  
@WebFilter(filterName="customFilter",urlPatterns="/*")  
public class CustomFilter implements Filter{  
  
    @Override  
    public void destroy() {  
        System.out.println("CustomFilter過濾器銷燬");  
    }  
  
    @Override  
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)  
            throws IOException, ServletException {  
        System.out.println("CustomFilter指定過濾器操做......");  
        //執行操做後必須doFilter  
        chain.doFilter(request, response);  
    }  
  
    @Override  
    public void init(FilterConfig arg0) throws ServletException {  
        System.out.println("CustomFilter初始化......");  
    }  
}  
2、自定義監聽器spring

建立一個過濾器,實現ServletContextListener接口session

[java] view plain copy
package com.example.listener;  
  
import javax.servlet.ServletContextEvent;  
import javax.servlet.ServletContextListener;  
import javax.servlet.annotation.WebListener;  
  
/** 
 * 實現ServletContextListener,覆蓋其兩個方法 
 * @author Administrator 
 * 
 */  
@WebListener  
public class CustomListener implements ServletContextListener{  
  
    @Override  
    public void contextDestroyed(ServletContextEvent servletContext) {  
        System.out.println("servletContext銷燬......");  
    }  
  
    @Override  
    public void contextInitialized(ServletContextEvent servletContext) {  
        System.out.println("servletContext初始化......");  
    }  
}  ide

建立一個session監聽器,監聽session的建立和銷燬
[java] view plain copy
package com.example.listener;  
  
import javax.servlet.annotation.WebListener;  
import javax.servlet.http.HttpSessionEvent;  
import javax.servlet.http.HttpSessionListener;  
  
/** 
 * 實現HttpSessionListener接口,監視session的動態 
 * @author Administrator 
 * 
 */  
@WebListener  
public class SessionListener implements HttpSessionListener{  
  
    @Override  
    public void sessionCreated(HttpSessionEvent session) {  
        System.out.println("session建立成功......");  
    }  
  
    @Override  
    public void sessionDestroyed(HttpSessionEvent session) {  
        System.out.println("session銷燬......");  
    }  
}  
3、自定義攔截器post

步驟:url

一、建立本身的攔截器實現HandlerInterceptor接口spa

二、建立本身的攔截器鏈,繼承WebMvcConfigurerAdapter類,重寫addInterceptors方法。.net

三、實例化本身的攔截器,並加入到攔截器鏈中。繼承

攔截器一:

[java] view plain copy
package com.example.Interceptor;  
  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
import org.springframework.web.servlet.HandlerInterceptor;  
import org.springframework.web.servlet.ModelAndView;  
  
public class CustomInterceptor implements HandlerInterceptor {  
  
    @Override  
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object object,  
            Exception exception) throws Exception {  
        //在整個請求結束以後被調用,也就是在DispatcherServlet 渲染了對應的視圖以後執行(主要是用於進行資源清理工做)  
        System.out.println("3. 整個請求結束以後被調用......CustomInterceptor1......");  
    }  
  
    @Override  
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object object, ModelAndView view)  
            throws Exception {  
        // 請求處理以後進行調用,可是在視圖被渲染以前  
        System.out.println("2. 請求處理以後進行調用,可是在視圖被渲染以前......CustomInterceptor1......");  
    }  
  
    @Override  
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception {  
        // 在請求處理以前進行調用  
        System.out.println("1. 在請求處理以前進行調用......CustomInterceptor1......");  
        // 只有返回true纔會繼續向下執行,返回false取消當前請求  
        return true;  
    }  
}  

攔截器二:
[java] view plain copy
package com.example.Interceptor;  
  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
import org.springframework.web.servlet.HandlerInterceptor;  
import org.springframework.web.servlet.ModelAndView;  
  
public class CustomInterceptor2 implements HandlerInterceptor {  
  
    @Override  
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object object,  
            Exception exception) throws Exception {  
        //在整個請求結束以後被調用,也就是在DispatcherServlet 渲染了對應的視圖以後執行(主要是用於進行資源清理工做)  
        System.out.println("3. 整個請求結束以後被調用......CustomInterceptor2......");  
    }  
  
    @Override  
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object object, ModelAndView view)  
            throws Exception {  
        // 請求處理以後進行調用,可是在視圖被渲染以前  
        System.out.println("2. 請求處理以後進行調用,可是在視圖被渲染以前......CustomInterceptor2......");  
    }  
  
    @Override  
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception {  
        // 在請求處理以前進行調用  
        System.out.println("1. 在請求處理以前進行調用......CustomInterceptor2......");  
        // 只有返回true纔會繼續向下執行,返回false取消當前請求  
        return true;  
    }  
}  

自定義攔截器鏈並加入自定義的攔截器 [java] view plain copy package com.example.adapter;      import org.springframework.context.annotation.Configuration;   import org.springframework.web.servlet.config.annotation.InterceptorRegistry;   import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;      import com.example.Interceptor.CustomInterceptor;   import com.example.Interceptor.CustomInterceptor2;      /**   * 繼承WebMvcConfigurerAdapter,複寫addInterceptors方法   * @author Administrator   *   */   @Configuration   public class WebAdapter extends WebMvcConfigurerAdapter{          @Override       public void addInterceptors(InterceptorRegistry registry) {           //衆多的攔截器組成了一個攔截器鏈           /**           * 主要方法說明:           * addPathPatterns 用於添加攔截規則           * excludePathPatterns 用戶排除攔截           */           registry.addInterceptor(new CustomInterceptor()).addPathPatterns("/*");           registry.addInterceptor(new CustomInterceptor2()).addPathPatterns("/*");           super.addInterceptors(registry);       }   }  

相關文章
相關標籤/搜索