SealClassConfiguration.java 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package com.ym;
  2. import com.alibaba.fastjson.serializer.SerializerFeature;
  3. import com.alibaba.fastjson.support.config.FastJsonConfig;
  4. import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
  5. import com.ym.filter.GlobalExceptionHandlerAdvice;
  6. import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
  7. import org.springframework.boot.web.servlet.FilterRegistrationBean;
  8. import org.springframework.context.annotation.Bean;
  9. import org.springframework.context.annotation.Configuration;
  10. import org.springframework.core.Ordered;
  11. import org.springframework.data.redis.connection.RedisConnectionFactory;
  12. import org.springframework.data.redis.core.RedisTemplate;
  13. import org.springframework.http.MediaType;
  14. import org.springframework.http.converter.HttpMessageConverter;
  15. import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
  16. import org.springframework.web.cors.CorsConfiguration;
  17. import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
  18. import org.springframework.web.filter.CorsFilter;
  19. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  20. import java.util.ArrayList;
  21. import java.util.Iterator;
  22. import java.util.List;
  23. /**
  24. * Created by weiqinxiao on 2019/2/25.
  25. */
  26. @Configuration
  27. public class SealClassConfiguration {
  28. /*@Bean
  29. public <K, V> RedisTemplate<K, V> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
  30. RedisTemplate<K, V> redisTemplate = new RedisTemplate<>();
  31. redisTemplate.setConnectionFactory(redisConnectionFactory);
  32. return redisTemplate;
  33. }*/
  34. @Bean
  35. public GlobalExceptionHandlerAdvice globalExceptionAdvice() {
  36. return new GlobalExceptionHandlerAdvice();
  37. }
  38. @Bean
  39. public WebMvcConfigurer getWebMvcConfigurerAdapter() {
  40. return new WebMvcConfigurer() {
  41. @Override
  42. public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
  43. Iterator<HttpMessageConverter<?>> iterator = converters.iterator();
  44. while (iterator.hasNext()) {
  45. HttpMessageConverter<?> c = iterator.next();
  46. if (c instanceof MappingJackson2HttpMessageConverter) {
  47. iterator.remove();
  48. }
  49. }
  50. FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
  51. List<MediaType> supportedMediaTypes = new ArrayList<>();
  52. supportedMediaTypes.add(MediaType.APPLICATION_JSON);
  53. supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
  54. supportedMediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED);
  55. supportedMediaTypes.add(MediaType.MULTIPART_FORM_DATA);
  56. converter.setSupportedMediaTypes(supportedMediaTypes);
  57. FastJsonConfig fastJsonConfig = new FastJsonConfig();
  58. fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteMapNullValue, SerializerFeature.PrettyFormat);
  59. converter.setFastJsonConfig(fastJsonConfig);
  60. converters.add(converter);
  61. }
  62. };
  63. }
  64. @Bean
  65. @ConditionalOnProperty(prefix = "cn.rongcloud.web", value = "enableCors", havingValue = "true")
  66. public FilterRegistrationBean corsFilter() {
  67. UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
  68. CorsConfiguration config = new CorsConfiguration();
  69. config.setAllowCredentials(true);
  70. config.addAllowedOrigin("*");
  71. config.addAllowedHeader("*");
  72. config.addAllowedMethod("*");
  73. source.registerCorsConfiguration("/**", config);
  74. FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
  75. bean.setOrder(Ordered.HIGHEST_PRECEDENCE);
  76. return bean;
  77. }
  78. }