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 接口, 重寫接口三個方法,具體方法執行時機見代碼註釋java

 

 

 

 

 

 

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)配置攔截器 , 通常具體配置參數使用配置文件的方式配置web

相關文章
相關標籤/搜索