Commit 400371b2 by yuquan.zhu Committed by 284718418@qq.com

修改跨域访问

parent 236ab785
......@@ -10,6 +10,7 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
......@@ -35,67 +36,34 @@ public class WebSecurityConfig implements WebMvcConfigurer {
//.excludePathPatterns("/")
registry.addInterceptor(getSessionInterceptor())
.addPathPatterns("/**")
.excludePathPatterns("/callback/**")
.excludePathPatterns("/actuator/*")
.excludePathPatterns("/doc*")
.excludePathPatterns("/v2/**")
.excludePathPatterns("/**/*.js")
.excludePathPatterns("/8timer/**")
.excludePathPatterns("/kqhz/OnClickAtt/**")
.excludePathPatterns("/kqdk/punchclock/**")
.excludePathPatterns("/kqz/punchclock/**")
.excludePathPatterns("/kqz/sauserregdata/**")
.excludePathPatterns("/login/**")
.excludePathPatterns("/login/*")
.excludePathPatterns("/swagger*/**")
.excludePathPatterns("/v2/api-docs")
.excludePathPatterns("/druid/login*")
.excludePathPatterns("/app/**")
.excludePathPatterns("/webjars/**");
// registry.addInterceptor(getSessionInterceptor()).addPathPatterns("/**").excludePathPatterns("/swagger-ui*");
}
/**
* 配置消息转换器--这里我用的是alibaba 开源的 fastjson
* @param converters
*/
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
// Iterator<HttpMessageConverter<?>> iterator = converters.iterator();
// while(iterator.hasNext()){
// HttpMessageConverter<?> converter = iterator.next();
// if(converter instanceof MappingJackson2HttpMessageConverter){
// iterator.remove();
// }
// }
// 1.需要先定义一个convert 转换消息的对象
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
// 2.添加fastJson的配置信息,比如,是否需要格式化返回的json数据
FastJsonConfig fastJsonConfig = new FastJsonConfig();
// 时间格式化
fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
// fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
// fastJsonConfig.setDateFormat("yyyy-MM-dd");
// 空值特别处理
// WriteNullListAsEmpty 将Collection类型字段的字段空值输出为[]
// WriteNullStringAsEmpty 将字符串类型字段的空值输出为空字符串 ""
// WriteNullNumberAsZero 将数值类型字段的空值输出为0
// WriteNullBooleanAsFalse 将Boolean类型字段的空值输出为false
fastJsonConfig.setSerializerFeatures(
SerializerFeature.PrettyFormat
// SerializerFeature.WriteEnumUsingToString,
// SerializerFeature.WriteNullStringAsEmpty,
// SerializerFeature.WriteMapNullValue,
// SerializerFeature.WriteDateUseDateFormat
);
// fastJsonConfig.setSerializeFilters((ValueFilter) (o, s, source) -> {
// if (source == null) {
// return "";//此处是关键,如果返回对象的变量为null,则自动变成""
// }
// if (source instanceof Date) {
// return ((Date) source).getTime();
// }
// return source;
// });
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat, SerializerFeature.WriteNullListAsEmpty,
SerializerFeature.WriteNullStringAsEmpty);
// 处理中文乱码问题
List<MediaType> fastMediaTypes = new ArrayList<>();
fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
......@@ -103,7 +71,7 @@ public class WebSecurityConfig implements WebMvcConfigurer {
// 3.在convert中添加配置信息
fastConverter.setFastJsonConfig(fastJsonConfig);
// 4.将convert添加到converters当中
converters.add(0,fastConverter);
converters.add(fastConverter);
}
......@@ -114,16 +82,11 @@ public class WebSecurityConfig implements WebMvcConfigurer {
// @Override
// public void addCorsMappings(CorsRegistry registry) {
//// System.out.println("我是MyWebConfig跨域");
// //设置允许跨域的路径
// registry.addMapping("/**")
// //设置允许跨域请求的域名
// .allowedOrigins("*")
// //是否允许证书 不再默认开启
// .allowCredentials(true)
// //设置允许的方法
// .allowedMethods("*")
// //跨域允许时间
// .allowedMethods("GET", "POST", "DELETE", "PUT","PATCH")
// .maxAge(3600);
// }
}
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