SpringBoot 使用WebMvcConfigurer处理请求

1. 简介

WebMvcConfigurer配置类其实是Spring内部的一种配置方式,采用JavaBean的形式来代替传统的xml配置文件形式进行针对框架个性化定制。

基于java-based方式的spring mvc配置,需要创建一个配置类并实现WebMvcConfigurer 接口,WebMvcConfigurerAdapter 抽象类是对WebMvcConfigurer接口的简单抽象(增加了一些默认实现),但在在SpringBoot2.0及Spring5.0中WebMvcConfigurerAdapter已被废弃 。

官方推荐直接实现WebMvcConfigurer或者直接继承WebMvcConfigurationSupport

  • 方式一:实现WebMvcConfigurer接口(推荐)
  • 方式二:继承WebMvcConfigurationSupport类

WebMvcConfigurer 提供的接口列表如下:

public interface WebMvcConfigurer {
    default void configurePathMatch(PathMatchConfigurer configurer) {
    }

    default void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    }

    default void configureAsyncSupport(AsyncSupportConfigurer configurer) {
    }

    default void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
    }

    default void addFormatters(FormatterRegistry registry) {
    }

    default void addInterceptors(InterceptorRegistry registry) {
    }

    default void addResourceHandlers(ResourceHandlerRegistry registry) {
    }

    default void addCorsMappings(CorsRegistry registry) {
    }

    default void addViewControllers(ViewControllerRegistry registry) {
    }

    default void configureViewResolvers(ViewResolverRegistry registry) {
    }

    default void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
    }

    default void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> handlers) {
    }

    default void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    }

    default void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
    }

    default void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers) {
    }

    default void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers) {
    }

    @Nullable
    default Validator getValidator() {
        return null;
    }

    @Nullable
    default MessageCodesResolver getMessageCodesResolver() {
        return null;
    }
}

2. 接口实现

2.1 addInterceptors

  • 作用: 拦截所有接口请求,常见的JWT校验便是在这里实现
  • 实例:
@Configuration
public class MvcConf implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
    	//拦截所有接口,/test2除外
        registry.addInterceptor(new MyInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/test2","/login.do");
    }
}

写个简易拦截器 MyInterceptor

public class MyInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("token");
        return StrUtil.isNotEmpty(token);
    }
}

2.2 addResourceHandlers

  • 作用: 重写静态资源配置,自定义静态资源路径的映射
  • 示例:
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/img/**").addResourceLocations("classpath:/static/image/");
    }

这段代码,所有从/img路径访问的图片,都会从 /static/image目录去查找

2.3 addCorsMappings

  • 作用: 配置跨域请求的问题
  • 示例:
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**");
    }

2.4 addViewControllers

  • 作用: 页面跳转转换,可以替代controller接口的方法
  • 示例:
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/login2.do").setViewName("login");
    }

你可能感兴趣的:(Spring,Boot,java,spring,boot,mvc,interceptor)