管道基础大数据平台系统开发-【后端】-Server
1
sws
2022-11-26 ab849f796bdc17236a95ea5fe5c166fb8f24a75c
src/main/java/com/lf/server/config/WebConfig.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,101 @@
package com.lf.server.config;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.lf.server.interceptor.AuthInterceptor;
import com.lf.server.service.all.SysService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.config.annotation.*;
import java.nio.charset.Charset;
import java.util.List;
/**
 * Web配置类
 * @author WWW
 */
@Configuration
public class WebConfig extends WebMvcConfigurationSupport {
    @Autowired
    private SysService sysService;
    /**
     * èŽ·å–æ‹¦æˆªå™¨å¯¹è±¡
     *
     * @return
     */
    public AuthInterceptor getAuthBean() {
        return new AuthInterceptor(sysService);
    }
    /**
     * swagger控制
     */
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
        super.addResourceHandlers(registry);
    }
    /**
     * å¿½ç•¥url地址大小写
     */
    @Override
    protected void configurePathMatch(PathMatchConfigurer configurer) {
        AntPathMatcher matcher = new AntPathMatcher();
        matcher.setCaseSensitive(false);
        configurer.setPathMatcher(matcher);
    }
    /**
     * è·¨åŸŸè¯·æ±‚
     */
    @Override
    protected void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowCredentials(true)
                .allowedOrigins("*")
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .allowedHeaders("*")
                .maxAge(3600);
    }
    /**
     * æ·»åŠ ç»Ÿä¸€çš„æ‹¦æˆªå™¨
     */
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(getAuthBean())
                // .excludePathPatterns("/swagger", "/webjars/**", "/v2/**", "/sign/**")
                .addPathPatterns("/**");
        super.addInterceptors(registry);
    }
    /**
     * å¤„理json格式,值null的转换为""
     */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        FastJsonConfig config = new FastJsonConfig();
        config.setSerializerFeatures(
                SerializerFeature.WriteNullListAsEmpty,
                SerializerFeature.WriteMapNullValue,
                //SerializerFeature.WriteNullStringAsEmpty,
                SerializerFeature.WriteNullNumberAsZero,
                SerializerFeature.WriteNullBooleanAsFalse);
        // ç»“果是否格式化,默认为false
        //SerializerFeature.PrettyFormat);
        converter.setFastJsonConfig(config);
        converter.setDefaultCharset(Charset.forName("UTF-8"));
        converters.add(converter);
    }
}