content: package com.company.project.common.config;
  
  import cn.dev33.satoken.interceptor.SaInterceptor;
  import cn.dev33.satoken.router.SaRouter;
  import cn.dev33.satoken.stp.StpUtil;
  import com.alibaba.fastjson.serializer.SerializeConfig;
  import com.alibaba.fastjson.serializer.SerializerFeature;
  import com.alibaba.fastjson.serializer.ToStringSerializer;
  import com.alibaba.fastjson.serializer.ValueFilter;
  import com.alibaba.fastjson.support.config.FastJsonConfig;
  import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
  import org.springframework.beans.factory.annotation.Value;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.http.MediaType;
  import org.springframework.http.converter.ByteArrayHttpMessageConverter;
  import org.springframework.http.converter.HttpMessageConverter;
  import org.springframework.http.converter.ResourceHttpMessageConverter;
  import org.springframework.http.converter.StringHttpMessageConverter;
  import org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter;
  import org.springframework.web.servlet.config.annotation.*;
  
  import java.io.File;
  import java.math.BigInteger;
  import java.nio.charset.StandardCharsets;
  import java.util.ArrayList;
  import java.util.List;
  
  /**
   * spring mvc 配置
   *
   * @author wenbin
   * @version V1.0
   * @date 2020年3月18日
   */
  @Configuration
  public class WebMvcConfigurer extends WebMvcConfigurationSupport {
  
      @Value("${file.path}")
      private String filePath;
  
      /**
       * 发现如果继承了WebMvcConfigurationSupport，则在yml中配置的相关内容会失效。 需要重新指定静态资源
       */
      @Override
      public void addResourceHandlers(ResourceHandlerRegistry registry) {
          registry.addResourceHandler("/static/**").addResourceLocations(
                  "classpath:/static/");
          registry.addResourceHandler("doc.html").addResourceLocations(
                  "classpath:/META-INF/resources/");
          registry.addResourceHandler("/webjars/**").addResourceLocations(
                  "classpath:/META-INF/resources/webjars/");
          registry.addResourceHandler("/files/**")
                  .addResourceLocations("file:" + filePath + File.separator);
      }
  
  
      //添加拦截器
      //web是从管理后台来的接口，不走app那一套校验逻辑
      @Override
      public void addInterceptors(InterceptorRegistry registry) {
          registry.addInterceptor(new SaInterceptor(handler ->
                  SaRouter.match("/**", r -> StpUtil.checkLogin()))
                  .isAnnotation(true))
                  .excludePathPatterns("/doc.html")
                  .excludePathPatterns("/swagger-resources/**")
                  .excludePathPatterns("/webjars/**")
                  .excludePathPatterns("/error")
                  .excludePathPatterns("/static/**")
                  .excludePathPatterns("/files/**")
                  .excludePathPatterns("/login")
                  .excludePathPatterns("/index/login")
                  .excludePathPatterns("/sys/user/login")
                  .excludePathPatterns("/sys/getVerify")
  
                  .excludePathPatterns("/app/api/**")
                  .addPathPatterns("/**");
      }
  
  
      /**
       * 使用阿里 FastJson 作为JSON MessageConverter
       */
      @Override
      public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
          converters.add(new ByteArrayHttpMessageConverter());
          converters.add(new StringHttpMessageConverter(StandardCharsets.UTF_8));// @ResponseBody 解决乱码
          converters.add(new ResourceHttpMessageConverter());
          converters.add(new AllEncompassingFormHttpMessageConverter());
          converters.add(fastJsonHttpMessageConverter());
      }
  
      @Bean
      public FastJsonHttpMessageConverter fastJsonHttpMessageConverter() {
          FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
          FastJsonConfig config = new FastJsonConfig();
          //Long类型转String类型
          SerializeConfig serializeConfig = SerializeConfig.globalInstance;
          // ToStringSerializer 是这个包 com.alibaba.fastjson.serializer.ToStringSerializer
          serializeConfig.put(BigInteger.class, ToStringSerializer.instance);
          serializeConfig.put(Long.class, ToStringSerializer.instance);
          serializeConfig.put(Long.TYPE, ToStringSerializer.instance);
          config.setSerializeConfig(serializeConfig);
          config.setSerializerFeatures(
                  SerializerFeature.WriteMapNullValue, // 保留map空的字段
                  SerializerFeature.WriteNullStringAsEmpty, // 将String类型的null转成""
                  SerializerFeature.WriteNullNumberAsZero, // 将Number类型的null转成0
                  SerializerFeature.WriteNullListAsEmpty, // 将List类型的null转成[]
                  SerializerFeature.WriteNullBooleanAsFalse, // 将Boolean类型的null转成false
                  SerializerFeature.WriteDateUseDateFormat,  //日期格式转换
                  SerializerFeature.DisableCircularReferenceDetect // 避免循环引用
          );
          config.setSerializeFilters(valueFilter);
          converter.setFastJsonConfig(config);
          converter.setDefaultCharset(StandardCharsets.UTF_8);
          // 解决中文乱码问题，相当于在Controller上的@RequestMapping中加了个属性produces = "application/json"
          List<MediaType> mediaTypeList = new ArrayList<>();
          mediaTypeList.add(MediaType.APPLICATION_JSON);
          converter.setSupportedMediaTypes(mediaTypeList);
          return converter;
      }
  
  
      /**
       * FastJson过滤器将null值转换为字符串
       * obj 是class
       * s 是key值
       * o1 是value值
       */
      public static final ValueFilter valueFilter = (obj, s, v) -> {
          if (v == null) {
              return "";
          }
          return v;
      };
  
      /**
       * 页面跨域访问Controller过滤
       */
      @Override
      public void addCorsMappings(CorsRegistry registry) {
          WebMvcConfigurer.super.addCorsMappings(registry);
          registry.addMapping("/**")
                  .allowedHeaders("*")
                  .allowedMethods("POST", "GET", "PUT", "DELETE")
                  .allowedOrigins("*");
      }
  
  
      /**
       * 配置servlet处理
       */
      @Override
      public void configureDefaultServletHandling(
              DefaultServletHandlerConfigurer configurer) {
          configurer.enable();
      }
  
  
  }
  
  