Commit 634e3459 authored by liqin's avatar liqin 💬

bug fixed

parent b158ab0e
package cn.wisenergy.chnmuseum.party.conf;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import java.util.Arrays;
@Configuration
public class GlobalCorsConfig {
@Value("${cors.url.list:#{null}}")
private String corsUrlList;
@Bean
public CorsFilter corsFilter() {
//1.添加CORS配置信息
CorsConfiguration config = new CorsConfiguration();
//1) 允许通过的域,不要写*,否则cookie就无法使用了
if (corsUrlList != null && corsUrlList.length() > 1) {
config.setAllowedOrigins(Arrays.asList(StringUtils.split(corsUrlList, ",")));
} else {
config.addAllowedOrigin("*");
}
//2) 是否发送Cookie信息
config.setAllowCredentials(true);
//3) 允许的请求方式
config.addAllowedMethod("OPTIONS");
config.addAllowedMethod("GET");
config.addAllowedMethod("POST");
config.addAllowedMethod("PUT");
config.addAllowedMethod("DELETE");
// 4)允许的头信息
config.addAllowedHeader("*");
//2.添加映射路径,我们拦截一切请求
UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
configSource.registerCorsConfiguration("/**", config);
//3.返回新的CorsFilter.
return new CorsFilter(configSource);
}
}
//package cn.wisenergy.chnmuseum.party.conf;
//
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.web.cors.CorsConfiguration;
//import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
//import org.springframework.web.filter.CorsFilter;
//
//import java.util.Arrays;
//
//@Configuration
//public class GlobalCorsConfig {
//
// @Value("${cors.url.list:#{null}}")
// private String corsUrlList;
//
// @Bean
// public CorsFilter corsFilter() {
// //1.添加CORS配置信息
// CorsConfiguration config = new CorsConfiguration();
// //1) 允许通过的域,不要写*,否则cookie就无法使用了
// if (corsUrlList != null && corsUrlList.length() > 1) {
// config.setAllowedOrigins(Arrays.asList(StringUtils.split(corsUrlList, ",")));
// } else {
// config.addAllowedOrigin("*");
// }
//
// //2) 是否发送Cookie信息
//
// config.setAllowCredentials(true);
// //3) 允许的请求方式
// config.addAllowedMethod("OPTIONS");
// config.addAllowedMethod("GET");
// config.addAllowedMethod("POST");
// config.addAllowedMethod("PUT");
// config.addAllowedMethod("DELETE");
//
// // 4)允许的头信息
// config.addAllowedHeader("*");
// //2.添加映射路径,我们拦截一切请求
// UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
// configSource.registerCorsConfiguration("/**", config);
// //3.返回新的CorsFilter.
// return new CorsFilter(configSource);
// }
//
//}
package cn.wisenergy.chnmuseum.party.conf;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
/**
* MVC配置
*/
@Configuration
public class MvcConfiguration extends WebMvcConfigurationSupport {
/**
* 后置跨域支持【当出现跨域请求,此处会放在拦截器最后执行,CORS失效】
*
* @param registry
*/
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOriginPatterns("*")
.allowedHeaders("*")
.allowedMethods("*")
.allowCredentials(true)
.maxAge(3600);
}
/**
* 配置消息转换器:Ali开源的fastJson
*
* @param converters
*/
// @Override
// public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
// // 先移除jackson转换器,springBoot1.x可以不排除
// for (int i = converters.size() - 1; i >= 0; i--) {
// if (converters.get(i) instanceof MappingJackson2HttpMessageConverter) {
// converters.remove(i);
// }
// }
// //1.需要定义一个convert转换消息的对象;
// FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
// StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
// ByteArrayHttpMessageConverter byteArrayHttpMessageConverter = new ByteArrayHttpMessageConverter();
//
// //2.添加fastJson的配置信息,比如:是否要格式化返回的json数据;
// FastJsonConfig fastJsonConfig = new FastJsonConfig();
// fastJsonConfig.setSerializerFeatures(
// SerializerFeature.PrettyFormat,
// SerializerFeature.WriteMapNullValue,
// SerializerFeature.WriteNullStringAsEmpty,
// SerializerFeature.DisableCircularReferenceDetect,
// SerializerFeature.WriteNullListAsEmpty,
// SerializerFeature.BrowserCompatible,
// SerializerFeature.WriteDateUseDateFormat);
// // 设置编码
// fastJsonConfig.setCharset(Charset.forName("UTF-8"));
// fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
//
// // 设置数字转化问题
// SerializeConfig serializeConfig = SerializeConfig.globalInstance;
// serializeConfig.put(BigInteger.class, ToStringSerializer.instance);
// serializeConfig.put(Long.class, ToStringSerializer.instance);
// serializeConfig.put(Long.TYPE, ToStringSerializer.instance);
// serializeConfig.setPropertyNamingStrategy( PropertyNamingStrategy.CamelCase);
// fastJsonConfig.setSerializeConfig(serializeConfig);
//
// //3处理中文乱码问题
// List<MediaType> fastMediaTypes = new ArrayList<>();
// fastMediaTypes.add(MediaType.APPLICATION_JSON);
// fastMediaTypes.add(MediaType.TEXT_HTML);
// fastMediaTypes.add(MediaType.MULTIPART_FORM_DATA);
//
// //4.在convert中添加配置信息.
// fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
// fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
//
// //5.将convert添加到converters当中.
// converters.add(fastJsonHttpMessageConverter);
// converters.add(stringHttpMessageConverter);
// converters.add(byteArrayHttpMessageConverter);
// }
/**
* 启用@EnableWebMvc后,properties文件中的静态路径失效,必须覆盖后重新制定
* 配置静态访问资源
*
* @param registry
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
// swagger2配置
registry.addResourceHandler("/swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
// 静态资源拦截
registry.addResourceHandler("/**")
.addResourceLocations("classpath:/META-INF/")
.addResourceLocations("classpath:/META-INF/resources/")
.addResourceLocations("classpath:/resources/")
.addResourceLocations("classpath:/static/")
.addResourceLocations("classpath:/public/")
.addResourceLocations("classpath:/statics/")
.addResourceLocations("classpath:/template/")
.addResourceLocations("classpath:/");
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment