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

修改跨域访问

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