13693261870
3 天以前 80247f1b5e00fab2ce6c9e8474cfeceefb902d95
se-system/src/main/java/com/terra/system/config/WebConfig.java
@@ -3,26 +3,37 @@
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.terra.system.interceptor.AuthInterceptor;
import com.alibaba.fastjson2.JSON;
import com.terra.common.interceptor.AuthInterceptor;
import com.terra.common.service.CommonService;
import com.terra.system.service.all.SysService;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.config.annotation.*;
import reactor.core.Exceptions;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;
/**
 * Web配置类
 * @author WWW
 */
@Configuration
//@Configuration
public class WebConfig extends WebMvcConfigurationSupport {
    @Resource
    private SysService sysService;
    CommonService commonService;
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
@@ -38,7 +49,7 @@
     * @return
     */
    public AuthInterceptor getAuthBean() {
        return new AuthInterceptor(sysService);
        return new AuthInterceptor(commonService);
    }
    /**
@@ -47,7 +58,9 @@
    @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("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/swagger-ui/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("/druid/**").addResourceLocations("classpath:/META-INF/resources/druid/");
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
        super.addResourceHandlers(registry);
    }
@@ -66,7 +79,7 @@
    /**
     * 跨域请求
     */
    @Override
    /*@Override
    protected void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowCredentials(true)
@@ -74,7 +87,7 @@
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .allowedHeaders("*")
                .maxAge(3600);
    }
    }*/
    /**
     * 添加统一的拦截器
@@ -83,6 +96,7 @@
    protected void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(getAuthBean())
                // .excludePathPatterns("/swagger", "/webjars/**", "/v2/**", "/sign/**")
                //.excludePathPatterns("/v3/api-docs")
                .addPathPatterns("/**");
        super.addInterceptors(registry);
    }
@@ -92,7 +106,24 @@
     */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        //FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter() {
            @Override
            protected void writeInternal(Object object, org.springframework.http.HttpOutputMessage outputMessage)
                    throws IOException, org.springframework.http.converter.HttpMessageNotWritableException {
                ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
                if (attributes != null) {
                    String requestURI = attributes.getRequest().getRequestURI();
                    if ("/v3/api-docs".equals(requestURI)) { // 排除 /v3/api-docs 路径
                        // 使用默认的 JSON 序列化(如 Jackson),这里需要手动处理,不能直接调用 super.writeInternal
                        outputMessage.getBody().write(String.valueOf(object).getBytes());
                        return;
                    }
                }
                super.writeInternal(object, outputMessage); // 其他路径使用 Fastjson2 处理
            }
        };
        FastJsonConfig config = new FastJsonConfig();
        config.setSerializerFeatures(
                SerializerFeature.WriteNullListAsEmpty,
@@ -104,7 +135,10 @@
        //SerializerFeature.PrettyFormat);
        converter.setFastJsonConfig(config);
        converter.setDefaultCharset(Charset.forName("UTF-8"));
        converter.setDefaultCharset(StandardCharsets.UTF_8);
        converter.setSupportedMediaTypes(Collections.singletonList(
                MediaType.APPLICATION_JSON
        ));
        converters.add(converter);
    }
}