医院废弃物信息化管理平台
“mitao”
2025-12-24 3763145f34d7706923d55edc32f5cfbc8bc08b92
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
package cn.besteam.config;
 
import cn.besteam.modules.shiro.authc.aop.AppLoginValidateInterceptor;
import cn.besteam.modules.shiro.authc.aop.LoginValidateInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
 
import java.nio.charset.Charset;
import java.util.Iterator;
import java.util.List;
 
/**
 * Spring Boot 2.0 解决跨域问题
 *
 * @author qinfeng
 */
@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {
 
    @Autowired
    private LoginValidateInterceptor loginValidateInterceptor;
 
    @Autowired
    private AppLoginValidateInterceptor appLoginValidateInterceptor;
 
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
 
        InterceptorRegistration registration = registry.addInterceptor(loginValidateInterceptor);
        registration.excludePathPatterns("/", "/api/**", "/**/*.html", "/swagger-resources/**", "/webjars/**", "/v2/api-docs", "/**/*.js", "/**/*.css", "/**/*.png", "/**/*.jpg", "/**/*.txt");
        //
        InterceptorRegistration appRegistration = registry.addInterceptor(appLoginValidateInterceptor);
        appRegistration.addPathPatterns("/api/**");
    }
 
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
 
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("*.txt").addResourceLocations("classpath:/static/");
    }
 
    @Bean
    public RequestMappingHandlerAdapter requestMappingHandlerAdapter() {
 
        RequestMappingHandlerAdapter adapter = new RequestMappingHandlerAdapter();
        // @ResponseBody注解靠HttpMessageConverter解析
        List<HttpMessageConverter<?>> converters = adapter.getMessageConverters();
        Iterator<HttpMessageConverter<?>> iterator = converters.iterator();
        while (iterator.hasNext()) {
            HttpMessageConverter<?> converter = iterator.next();
            // 移除默认编码为ISO8859-1的字符串解析器
            if (converter instanceof StringHttpMessageConverter) {
                iterator.remove();
            }
        }
        // 字符串才使用UTF-8解析
        converters.add(new StringHttpMessageConverter(Charset.forName("UTF-8")));
        // 解析json
        converters.add(new MappingJackson2HttpMessageConverter());
        adapter.setMessageConverters(converters);
        return adapter;
    }
 
    @Bean
    public static PropertySourcesPlaceholderConfigurer propertyConfigIn() {
 
        return new PropertySourcesPlaceholderConfigurer();
    }
 
    @Bean
    public CorsFilter corsFilter() {
 
        final UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
        final CorsConfiguration corsConfiguration = new CorsConfiguration();
        /*是否允许请求带有验证信息*/
        corsConfiguration.setAllowCredentials(true);
        /*允许访问的客户端域名*/
        corsConfiguration.addAllowedOrigin("*");
        /*允许服务端访问的客户端请求头*/
        corsConfiguration.addAllowedHeader("*");
        /*允许访问的方法名,GET POST等*/
        corsConfiguration.addAllowedMethod("*");
        urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsFilter(urlBasedCorsConfigurationSource);
    }
 
}