From ab849f796bdc17236a95ea5fe5c166fb8f24a75c Mon Sep 17 00:00:00 2001 From: sws <15810472099@163.com> Date: 星期六, 26 十一月 2022 16:12:02 +0800 Subject: [PATCH] 1 --- src/main/java/com/lf/server/config/WebConfig.java | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 101 insertions(+), 0 deletions(-) diff --git a/src/main/java/com/lf/server/config/WebConfig.java b/src/main/java/com/lf/server/config/WebConfig.java new file mode 100644 index 0000000..529efac --- /dev/null +++ b/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鏍煎紡锛屽�糿ull鐨勮浆鎹负"" + */ + @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); + // 缁撴灉鏄惁鏍煎紡鍖�,榛樿涓篺alse + //SerializerFeature.PrettyFormat); + + converter.setFastJsonConfig(config); + converter.setDefaultCharset(Charset.forName("UTF-8")); + converters.add(converter); + } +} -- Gitblit v1.9.3