ConfigurerAdapter.java 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Copyright 2019-2020 Zheng Jie
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package me.zhengjie.config;
  17. import com.alibaba.fastjson.serializer.SerializerFeature;
  18. import com.alibaba.fastjson.support.config.FastJsonConfig;
  19. import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
  20. import org.springframework.context.annotation.Bean;
  21. import org.springframework.context.annotation.Configuration;
  22. import org.springframework.http.MediaType;
  23. import org.springframework.http.converter.HttpMessageConverter;
  24. import org.springframework.web.cors.CorsConfiguration;
  25. import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
  26. import org.springframework.web.filter.CorsFilter;
  27. import org.springframework.web.servlet.config.annotation.EnableWebMvc;
  28. import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
  29. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  30. import java.nio.charset.StandardCharsets;
  31. import java.util.ArrayList;
  32. import java.util.List;
  33. /**
  34. * WebMvcConfigurer
  35. *
  36. * @author Zheng Jie
  37. * @date 2018-11-30
  38. */
  39. @Configuration
  40. @EnableWebMvc
  41. public class ConfigurerAdapter implements WebMvcConfigurer {
  42. /** 文件配置 */
  43. private final FileProperties properties;
  44. public ConfigurerAdapter(FileProperties properties) {
  45. this.properties = properties;
  46. }
  47. @Bean
  48. public CorsFilter corsFilter() {
  49. UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
  50. CorsConfiguration config = new CorsConfiguration();
  51. config.setAllowCredentials(true);
  52. config.addAllowedOriginPattern("*");
  53. config.addAllowedHeader("*");
  54. config.addAllowedMethod("*");
  55. source.registerCorsConfiguration("/**", config);
  56. return new CorsFilter(source);
  57. }
  58. @Override
  59. public void addResourceHandlers(ResourceHandlerRegistry registry) {
  60. FileProperties.ElPath path = properties.getPath();
  61. String avatarUtl = "file:" + path.getAvatar().replace("\\","/");
  62. String pathUtl = "file:" + path.getPath().replace("\\","/");
  63. registry.addResourceHandler("/avatar/**").addResourceLocations(avatarUtl).setCachePeriod(0);
  64. registry.addResourceHandler("/file/**").addResourceLocations(pathUtl).setCachePeriod(0);
  65. registry.addResourceHandler("/**").addResourceLocations("classpath:/META-INF/resources/").setCachePeriod(0);
  66. }
  67. @Override
  68. public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
  69. // 使用 fastjson 序列化,会导致 @JsonIgnore 失效,可以使用 @JSONField(serialize = false) 替换
  70. FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
  71. List<MediaType> supportMediaTypeList = new ArrayList<>();
  72. supportMediaTypeList.add(MediaType.APPLICATION_JSON);
  73. FastJsonConfig config = new FastJsonConfig();
  74. config.setDateFormat("yyyy-MM-dd HH:mm:ss");
  75. config.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect);
  76. converter.setFastJsonConfig(config);
  77. converter.setSupportedMediaTypes(supportMediaTypeList);
  78. converter.setDefaultCharset(StandardCharsets.UTF_8);
  79. converters.add(converter);
  80. }
  81. }