springBoot 2.X-自定義攔截器


package com.cx.springboot.myInter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 * 
 * @作者 陳先生
 * @創建時間 2018年7月13日
 * @功能描述 自定義攔截器
 */
@Configuration
public class MyInterceptor implements HandlerInterceptor {

    /**
     *  在請求處理之前進行調用(Controller方法調用之前)
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        // 進行邏輯判斷,如果ok就返回true,不行就返回false,返回false就不會處理改請求
        
        String name = request.getParameter("name");
        System.err.println(name +"-攔截器");
        return true;
    }

    /**
     * 請求處理之后進行調用,但是在視圖被渲染之前(Controller方法調用之后)
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
        //
    }
     /**
     * 在整個請求結束之后被調用,也就是在DispatcherServlet 渲染了對應的視圖之后執行(主要是用於進行資源清理工作)
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        
    }

}

1) 創建類 並實現 HandlerInterceptor 接口, 重寫接口三個方法,具體方法執行時機見代碼注釋

 

 

 

 

 

 

package com.cx.springboot.myInter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

/**
 * 
 * @作者 陳先生
 * @創建時間 2018年7月13日
 * @功能描述 攔截器配置
 */
@Configuration
public class MvcConfig extends WebMvcConfigurationSupport {
    // 以下WebMvcConfigurerAdapter 比較常用的重寫接口
    // /** 解決跨域問題 **/
    // public void addCorsMappings(CorsRegistry registry) ;
    // /** 添加攔截器 **/
    // void addInterceptors(InterceptorRegistry registry);
    // /** 這里配置視圖解析器 **/
    // void configureViewResolvers(ViewResolverRegistry registry);
    // /** 配置內容裁決的一些選項 **/
    // void configureContentNegotiation(ContentNegotiationConfigurer
    // configurer);
    // /** 視圖跳轉控制器 **/
    // void addViewControllers(ViewControllerRegistry registry);
    // /** 靜態資源處理 **/
    // void addResourceHandlers(ResourceHandlerRegistry registry);
    // /** 默認靜態資源處理器 **/
    // void configureDefaultServletHandling(DefaultServletHandlerConfigurer
    // configurer);

    @Autowired
    private MyInterceptor myInterceptor;

    /**
     * 表示這些配置的表示靜態文件所處路徑, 不用攔截
     */
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/templates/**")
                .addResourceLocations("classpath:/templates/");
        super.addResourceHandlers(registry);
    }

    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(myInterceptor)
                // addPathPatterns 用於添加攔截規則 , 先把所有路徑都加入攔截, 再一個個排除
                .addPathPatterns("/**")
                // excludePathPatterns 表示改路徑不用攔截
                .excludePathPatterns("/");
        super.addInterceptors(registry);
    }
}

2)配置攔截器 , 一般具體配置參數使用配置文件的方式配置

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM