|
@@ -0,0 +1,184 @@
|
|
|
|
|
+package com.zhentao.common.config;
|
|
|
|
|
+
|
|
|
|
|
+import com.fasterxml.jackson.annotation.JsonInclude;
|
|
|
|
|
+import com.fasterxml.jackson.core.JsonGenerator;
|
|
|
|
|
+import com.fasterxml.jackson.core.JsonParser;
|
|
|
|
|
+import com.fasterxml.jackson.databind.DeserializationFeature;
|
|
|
|
|
+import com.fasterxml.jackson.databind.JsonSerializer;
|
|
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
+import com.fasterxml.jackson.databind.SerializerProvider;
|
|
|
|
|
+import com.zhentao.common.intercepter.AuthIntercepter;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
|
|
|
|
+import org.springframework.context.annotation.Bean;
|
|
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
|
|
+import org.springframework.context.annotation.Primary;
|
|
|
|
|
+import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
|
|
|
|
|
+import org.springframework.web.filter.HttpPutFormContentFilter;
|
|
|
|
|
+import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
|
|
|
|
+import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
|
|
|
|
+import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
|
|
|
|
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
|
|
|
|
|
+import java.io.IOException;
|
|
|
|
|
+
|
|
|
|
|
+@Configuration
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+//@EnableWebMvc
|
|
|
|
|
+public class IntercepterConfig extends WebMvcConfigurationSupport {
|
|
|
|
|
+
|
|
|
|
|
+// @Override
|
|
|
|
|
+// public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
|
|
|
|
|
+// log.info("IntercepterConfig--------------------");
|
|
|
|
|
+// resolvers.add(new CurrentUserHandler());
|
|
|
|
|
+// }
|
|
|
|
|
+// @Autowired
|
|
|
|
|
+// AuthIntercepter authIntercepter;
|
|
|
|
|
+// @Autowired
|
|
|
|
|
+// protected RedisTemplate redisTemplate;
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void addInterceptors(InterceptorRegistry registry) {
|
|
|
|
|
+ // 拦截所有请求,通过判断是否有 @LoginRequired 注解 决定是否需要登录
|
|
|
|
|
+ registry.addInterceptor(authenticationInterceptor()).excludePathPatterns("**.html").excludePathPatterns("**.txt")
|
|
|
|
|
+ .excludePathPatterns("/webjars/**")
|
|
|
|
|
+ .excludePathPatterns("/dashboard/list")
|
|
|
|
|
+ .excludePathPatterns("/swagger-ui.html")
|
|
|
|
|
+ .excludePathPatterns("/swagger-resources/**").excludePathPatterns("/error").addPathPatterns("/**");
|
|
|
|
|
+ super.addInterceptors(registry);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
|
|
|
|
+ super.addResourceHandlers(registry);
|
|
|
|
|
+ registry.addResourceHandler("/**")
|
|
|
|
|
+ .addResourceLocations("classpath:/static/")
|
|
|
|
|
+ .addResourceLocations("classpath:/templates/")
|
|
|
|
|
+ .addResourceLocations("classpath:/resources/");
|
|
|
|
|
+ registry.addResourceHandler("swagger-ui.html")
|
|
|
|
|
+ .addResourceLocations("classpath:/META-INF/resources/");
|
|
|
|
|
+ registry.addResourceHandler("/webjars/**")
|
|
|
|
|
+ .addResourceLocations("classpath:/META-INF/resources/webjars/");
|
|
|
|
|
+ registry.addResourceHandler("SFLMPwcUTv.txt")
|
|
|
|
|
+ .addResourceLocations("classpath:/resources/");
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void addCorsMappings(CorsRegistry registry) {
|
|
|
|
|
+ super.addCorsMappings(registry);
|
|
|
|
|
+ registry
|
|
|
|
|
+ .addMapping("/**")
|
|
|
|
|
+ .allowedHeaders("*")
|
|
|
|
|
+ .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
|
|
|
|
|
+ .allowedOrigins("*");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Bean
|
|
|
|
|
+ public AuthIntercepter authenticationInterceptor() {
|
|
|
|
|
+ return new AuthIntercepter();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+// @Bean
|
|
|
|
|
+// public CurrentUserMethodArgumentResolver currentUserMethodArgumentResolver() {
|
|
|
|
|
+// return new CurrentUserMethodArgumentResolver();
|
|
|
|
|
+// }
|
|
|
|
|
+// @Override
|
|
|
|
|
+//// public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
|
|
|
|
|
+//// argumentResolvers.add(currentUserMethodArgumentResolver());
|
|
|
|
|
+//// super.addArgumentResolvers(argumentResolvers);
|
|
|
|
|
+//// }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * @Description 解决使用put请求 服务器接收不到参数
|
|
|
|
|
+ * @author DengKaiTao
|
|
|
|
|
+ * @date 2018/12/4 16:35
|
|
|
|
|
+ * @return org.springframework.web.filter.HttpPutFormContentFilter
|
|
|
|
|
+ * @version v1.0
|
|
|
|
|
+ **/
|
|
|
|
|
+ @Bean
|
|
|
|
|
+ public HttpPutFormContentFilter httpPutFormContentFilter() {
|
|
|
|
|
+ return new HttpPutFormContentFilter();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+// @Override
|
|
|
|
|
+// public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
|
|
|
|
|
+//
|
|
|
|
|
+//// FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
|
|
|
|
|
+//// FastJsonConfig config = new FastJsonConfig();
|
|
|
|
|
+//// config.setSerializerFeatures(
|
|
|
|
|
+//// //保留map空的字段
|
|
|
|
|
+//// SerializerFeature.WriteMapNullValue,
|
|
|
|
|
+//// // 将String类型的NULL转化为""
|
|
|
|
|
+//// SerializerFeature.WriteNullStringAsEmpty,
|
|
|
|
|
+//// // 将Number类型的NULL转化为0
|
|
|
|
|
+//// SerializerFeature.WriteNullNumberAsZero,
|
|
|
|
|
+//// // 将List类型的NULL转成[]
|
|
|
|
|
+//// SerializerFeature.WriteNullListAsEmpty,
|
|
|
|
|
+//// // 将Boolean类型的NULL转化为false
|
|
|
|
|
+//// SerializerFeature.WriteNullBooleanAsFalse,
|
|
|
|
|
+//// SerializerFeature.PrettyFormat,
|
|
|
|
|
+//// // 避免循环引用
|
|
|
|
|
+//// SerializerFeature.DisableCircularReferenceDetect);
|
|
|
|
|
+////
|
|
|
|
|
+//// converter.setFastJsonConfig(config);
|
|
|
|
|
+//// converter.setDefaultCharset(Charset.forName("UTF-8"));
|
|
|
|
|
+////
|
|
|
|
|
+//// List<MediaType> mediaTypeList = new ArrayList<>();
|
|
|
|
|
+//// // 解决中文乱码问题,相当于在Controller上的@RequestMapping中加了个属性produces = "application/json"
|
|
|
|
|
+//// mediaTypeList.add(MediaType.APPLICATION_JSON);
|
|
|
|
|
+//// converter.setSupportedMediaTypes(mediaTypeList);
|
|
|
|
|
+//// converters.add(converter);
|
|
|
|
|
+// //定义Json转换器
|
|
|
|
|
+// MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
|
|
|
|
|
+// ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
|
+// //定义对象模型
|
|
|
|
|
+// SimpleModule simpleModule = new SimpleModule();
|
|
|
|
|
+// //添加对长整型的转换关系
|
|
|
|
|
+// simpleModule.addSerializer(BigInteger.class, ToStringSerializer.instance);
|
|
|
|
|
+//// simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
|
|
|
|
|
+//// simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
|
|
|
|
|
+//// simpleModule.addSerializer(Date.class,ToStringSerializer.instance);
|
|
|
|
|
+// //将对象模型添加至对象映射器
|
|
|
|
|
+// objectMapper.registerModule(simpleModule);
|
|
|
|
|
+// jackson2HttpMessageConverter.setObjectMapper(objectMapper);
|
|
|
|
|
+// //在转换器列表中添加自定义的Json转换器
|
|
|
|
|
+// converters.add(jackson2HttpMessageConverter);
|
|
|
|
|
+// //添加utf-8的默认String转换器
|
|
|
|
|
+// converters.add(new StringHttpMessageConverter(Charset.forName("UTF-8")));
|
|
|
|
|
+// }
|
|
|
|
|
+
|
|
|
|
|
+ @Bean
|
|
|
|
|
+ @Primary
|
|
|
|
|
+ @ConditionalOnMissingBean(ObjectMapper.class)
|
|
|
|
|
+ public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder)
|
|
|
|
|
+ {
|
|
|
|
|
+ ObjectMapper objectMapper = builder.createXmlMapper(false).build();
|
|
|
|
|
+
|
|
|
|
|
+ // 通过该方法对mapper对象进行设置,所有序列化的对象都将按改规则进行系列化
|
|
|
|
|
+ // Include.Include.ALWAYS 默认
|
|
|
|
|
+ // Include.NON_DEFAULT 属性为默认值不序列化
|
|
|
|
|
+ // Include.NON_EMPTY 属性为 空("") 或者为 NULL 都不序列化,则返回的json是没有这个字段的。这样对移动端会更省流量
|
|
|
|
|
+ // Include.NON_NULL 属性为NULL 不序列化
|
|
|
|
|
+ objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
|
|
|
|
|
+ objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
|
|
|
|
+ // 允许出现特殊字符和转义符
|
|
|
|
|
+ objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
|
|
|
|
|
+ // 允许出现单引号
|
|
|
|
|
+ objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
|
|
|
|
|
+ // 字段保留,将null值转为""
|
|
|
|
|
+ objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>()
|
|
|
|
|
+ {
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void serialize(Object o, JsonGenerator jsonGenerator,
|
|
|
|
|
+ SerializerProvider serializerProvider)
|
|
|
|
|
+ throws IOException
|
|
|
|
|
+ {
|
|
|
|
|
+ jsonGenerator.writeString("");
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ return objectMapper;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+}
|