87cf1fd4fee979b1ce2fd2188b89bf03acc325f2.svn-base 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package org.jeecg.config;
  2. import com.fasterxml.jackson.databind.DeserializationFeature;
  3. import com.fasterxml.jackson.databind.ObjectMapper;
  4. import com.fasterxml.jackson.databind.module.SimpleModule;
  5. import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
  6. import org.springframework.beans.factory.annotation.Value;
  7. import org.springframework.boot.actuate.trace.http.InMemoryHttpTraceRepository;
  8. import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
  9. import org.springframework.context.annotation.Bean;
  10. import org.springframework.context.annotation.Conditional;
  11. import org.springframework.context.annotation.Configuration;
  12. import org.springframework.context.annotation.Primary;
  13. import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
  14. import org.springframework.web.cors.CorsConfiguration;
  15. import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
  16. import org.springframework.web.filter.CorsFilter;
  17. import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
  18. import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
  19. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  20. /**
  21. * Spring Boot 2.0 解决跨域问题
  22. *
  23. * @Author qinfeng
  24. *
  25. */
  26. @Configuration
  27. public class WebMvcConfiguration implements WebMvcConfigurer {
  28. @Value("${jeecg.path.upload}")
  29. private String upLoadPath;
  30. @Value("${jeecg.path.webapp}")
  31. private String webAppPath;
  32. @Value("${spring.resource.static-locations}")
  33. private String staticLocations;
  34. /**
  35. * 静态资源的配置 - 使得可以从磁盘中读取 Html、图片、视频、音频等
  36. */
  37. @Override
  38. public void addResourceHandlers(ResourceHandlerRegistry registry) {
  39. registry.addResourceHandler("/**")
  40. .addResourceLocations("file:" + upLoadPath + "//", "file:" + webAppPath + "//")
  41. .addResourceLocations(staticLocations.split(","));
  42. }
  43. /**
  44. * 方案一: 默认访问根路径跳转 doc.html页面 (swagger文档页面)
  45. * 方案二: 访问根路径改成跳转 index.html页面 (简化部署方案: 可以把前端打包直接放到项目的 webapp,上面的配置)
  46. */
  47. @Override
  48. public void addViewControllers(ViewControllerRegistry registry) {
  49. registry.addViewController("/").setViewName("doc.html");
  50. }
  51. @Bean
  52. @Conditional(CorsFilterCondition.class)
  53. public CorsFilter corsFilter() {
  54. final UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
  55. final CorsConfiguration corsConfiguration = new CorsConfiguration();
  56. //是否允许请求带有验证信息
  57. corsConfiguration.setAllowCredentials(true);
  58. // 允许访问的客户端域名
  59. corsConfiguration.addAllowedOrigin("*");
  60. // 允许服务端访问的客户端请求头
  61. corsConfiguration.addAllowedHeader("*");
  62. // 允许访问的方法名,GET POST等
  63. corsConfiguration.addAllowedMethod("*");
  64. urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);
  65. return new CorsFilter(urlBasedCorsConfigurationSource);
  66. }
  67. /**
  68. * 序列换成json时,将所有的long变成string
  69. * js中long过长精度丢失
  70. */
  71. @Bean
  72. @Primary
  73. @ConditionalOnMissingBean(ObjectMapper.class)
  74. public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
  75. ObjectMapper objectMapper = new ObjectMapper();
  76. SimpleModule simpleModule = new SimpleModule();
  77. //忽略在json字符串中存在,在java类中不存在字段,防止错误。
  78. objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
  79. objectMapper.configure(DeserializationFeature.READ_ENUMS_USING_TO_STRING, true);
  80. simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
  81. simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
  82. objectMapper.registerModule(simpleModule);
  83. return objectMapper;
  84. }
  85. /**
  86. * SpringBootAdmin的Httptrace不见了
  87. * https://blog.csdn.net/u013810234/article/details/110097201
  88. */
  89. @Bean
  90. public InMemoryHttpTraceRepository getInMemoryHttpTrace(){
  91. return new InMemoryHttpTraceRepository();
  92. }
  93. }