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);
|
}
|
|
}
|