13693261870
6 天以前 7d03fd7d726469278b5bd1b379f0303fa55b7c3e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package com.terra.system.config;
 
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.alibaba.fastjson2.JSON;
import com.terra.system.interceptor.AuthInterceptor;
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
public class WebConfig extends WebMvcConfigurationSupport {
    @Resource
    private SysService sysService;
 
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        // 设置访问路径为 “/” 跳转到指定页面
        registry.addViewController("/").setViewName("redirect:/sign/toIndex");
        // 设置为最高优先级
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
    }
 
    /**
     * 获取拦截器对象
     *
     * @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("/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);
    }
 
    /**
     * 忽略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/**")
                //.excludePathPatterns("/v3/api-docs")
                .addPathPatterns("/**");
        super.addInterceptors(registry);
    }
 
    /**
     * 处理json格式,值null的转换为""
     */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        //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,
                SerializerFeature.WriteMapNullValue,
                //SerializerFeature.WriteNullStringAsEmpty,
                SerializerFeature.WriteNullNumberAsZero,
                SerializerFeature.WriteNullBooleanAsFalse);
        // 结果是否格式化,默认为false
        //SerializerFeature.PrettyFormat);
 
        converter.setFastJsonConfig(config);
        converter.setDefaultCharset(StandardCharsets.UTF_8);
        converter.setSupportedMediaTypes(Collections.singletonList(
                MediaType.APPLICATION_JSON
        ));
        converters.add(converter);
    }
}