spring-boot添加自定义拦截器

时间:2022-12-31 08:27:41

spring-boot中的WebMvcConfigurerAdapter类提供了很多自定义操作的方法,先贴出来大家看看

 package org.springframework.web.servlet.config.annotation;

 import java.util.List;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.validation.MessageCodesResolver;
import org.springframework.validation.Validator;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.HandlerExceptionResolver; public abstract class WebMvcConfigurerAdapter implements WebMvcConfigurer {
public WebMvcConfigurerAdapter() {
} public void configurePathMatch(PathMatchConfigurer configurer) {
} public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
} public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
} public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
} public void addFormatters(FormatterRegistry registry) {
} public void addInterceptors(InterceptorRegistry registry) {
} public void addResourceHandlers(ResourceHandlerRegistry registry) {
} public void addCorsMappings(CorsRegistry registry) {
} public void addViewControllers(ViewControllerRegistry registry) {
} public void configureViewResolvers(ViewResolverRegistry registry) {
} public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
} public void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers) {
} public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
} public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
} public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
} public void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
} public Validator getValidator() {
return null;
} public MessageCodesResolver getMessageCodesResolver() {
return null;
}
}

如上代码的实现都是空的,也就是说我们想要完成自定义一些操作,只需要重写这个类中对应的方法,就可以实现

比如昨晚写了篇自定义springboot的消息转换器,其中的一种实现就是通过重写这个类的方法

下边贴一下重写addInterceptors从而实现自定义拦截器的demo

 package com.itcast.springboot;

 import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* @Author : Hellxz
* @Description:自定义拦截器
* @Date : 2018/4/8 12:36
*/
@Configuration
public class MyInterceptor extends WebMvcConfigurerAdapter {
/**
* @Author: Hellxz
* @Description: 添加自定义拦截器
* @Date: 2018/4/8 12:39
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
/**
* 自定义handlerInterceptor,匿名内部类,也可以单独提出来一个类
*/
HandlerInterceptor handlerInterceptor = new HandlerInterceptor() {
@Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
System.out.println("—————进入拦截器");
return true;
} @Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
} @Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
}
};
//将这个handler添加到签名认证拦截器中
registry.addInterceptor(handlerInterceptor);
// 添加拦截uri
// registry.addInterceptor(handlerInterceptor).addPathPatterns("/user/**");
super.addInterceptors(registry);
} }