spring-boot添加自定义拦截器
spring-boot中的WebMvcConfigurerAdapter类提供了很多自定义操作的方法,先贴出来大家看看
1 package org.springframework.web.servlet.config.annotation; 2 3 import java.util.List; 4 import org.springframework.format.FormatterRegistry; 5 import org.springframework.http.converter.HttpMessageConverter; 6 import org.springframework.validation.MessageCodesResolver; 7 import org.springframework.validation.Validator; 8 import org.springframework.web.method.support.HandlerMethodArgumentResolver; 9 import org.springframework.web.method.support.HandlerMethodReturnValueHandler; 10 import org.springframework.web.servlet.HandlerExceptionResolver; 11 12 public abstract class WebMvcConfigurerAdapter implements WebMvcConfigurer { 13 public WebMvcConfigurerAdapter() { 14 } 15 16 public void configurePathMatch(PathMatchConfigurer configurer) { 17 } 18 19 public void configureContentNegotiation(ContentNegotiationConfigurer configurer) { 20 } 21 22 public void configureAsyncSupport(AsyncSupportConfigurer configurer) { 23 } 24 25 public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { 26 } 27 28 public void addFormatters(FormatterRegistry registry) { 29 } 30 31 public void addInterceptors(InterceptorRegistry registry) { 32 } 33 34 public void addResourceHandlers(ResourceHandlerRegistry registry) { 35 } 36 37 public void addCorsMappings(CorsRegistry registry) { 38 } 39 40 public void addViewControllers(ViewControllerRegistry registry) { 41 } 42 43 public void configureViewResolvers(ViewResolverRegistry registry) { 44 } 45 46 public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) { 47 } 48 49 public void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers) { 50 } 51 52 public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { 53 } 54 55 public void extendMessageConverters(List<HttpMessageConverter<?>> converters) { 56 } 57 58 public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) { 59 } 60 61 public void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) { 62 } 63 64 public Validator getValidator() { 65 return null; 66 } 67 68 public MessageCodesResolver getMessageCodesResolver() { 69 return null; 70 } 71 }
如上代码的实现都是空的,也就是说我们想要完成自定义一些操作,只需要重写这个类中对应的方法,就可以实现
比如昨晚写了篇自定义springboot的消息转换器 ,其中的一种实现就是通过重写这个类的方法
下边贴一下重写addInterceptors从而实现自定义拦截器的demo
1 package com.itcast.springboot; 2 3 import org.springframework.context.annotation.Configuration; 4 import org.springframework.web.servlet.HandlerInterceptor; 5 import org.springframework.web.servlet.ModelAndView; 6 import org.springframework.web.servlet.config.annotation.InterceptorRegistry; 7 import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 8 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 12 /** 13 * @Author : Hellxz 14 * @Description:自定义拦截器 15 * @Date : 2018/4/8 12:36 16 */ 17 @Configuration 18 public class MyInterceptor extends WebMvcConfigurerAdapter { 19 /** 20 * @Author: Hellxz 21 * @Description: 添加自定义拦截器 22 * @Date: 2018/4/8 12:39 23 */ 24 @Override 25 public void addInterceptors(InterceptorRegistry registry) { 26 /** 27 * 自定义handlerInterceptor,匿名内部类,也可以单独提出来一个类 28 */ 29 HandlerInterceptor handlerInterceptor = new HandlerInterceptor() { 30 @Override 31 public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception { 32 System.out.println("—————进入拦截器"); 33 return true; 34 } 35 36 @Override 37 public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception { 38 } 39 40 @Override 41 public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception { 42 } 43 }; 44 //将这个handler添加到签名认证拦截器中 45 registry.addInterceptor(handlerInterceptor); 46 // 添加拦截uri 47 // registry.addInterceptor(handlerInterceptor).addPathPatterns("/user/**"); 48 super.addInterceptors(registry); 49 } 50 51 }