WebMvcConfiguration.java 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package org.jeecg.config;
  2. import cn.com.ctop.common.module.utils.StringUtils;
  3. import org.jeecg.config.interceptors.BrowseLogHandelInterceptor;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.beans.factory.annotation.Value;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.context.annotation.Configuration;
  8. import org.springframework.web.cors.CorsConfiguration;
  9. import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
  10. import org.springframework.web.filter.CorsFilter;
  11. import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
  12. import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
  13. import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
  14. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  15. /**
  16. * Spring Boot 2.0 解决跨域问题
  17. *
  18. * @Author qinfeng
  19. *
  20. */
  21. @Configuration
  22. public class WebMvcConfiguration implements WebMvcConfigurer {
  23. @Autowired
  24. BrowseLogHandelInterceptor browseLogHandelInterceptor;
  25. @Value("${jeecg.path.upload}")
  26. private String upLoadPath;
  27. @Value("${jeecg.path.webapp}")
  28. private String webAppPath;
  29. @Value("${spring.resource.static-locations}")
  30. private String staticLocations;
  31. @Override
  32. public void addInterceptors(InterceptorRegistry registry) {
  33. registry.addInterceptor(browseLogHandelInterceptor);
  34. }
  35. @Bean
  36. public CorsFilter corsFilter() {
  37. final UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
  38. final CorsConfiguration corsConfiguration = new CorsConfiguration();
  39. /* 是否允许请求带有验证信息 */
  40. corsConfiguration.setAllowCredentials(true);
  41. /* 允许访问的客户端域名 */
  42. corsConfiguration.addAllowedOrigin("*");
  43. /* 允许服务端访问的客户端请求头 */
  44. corsConfiguration.addAllowedHeader("*");
  45. /* 允许访问的方法名,GET POST等 */
  46. corsConfiguration.addAllowedMethod("*");
  47. urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);
  48. return new CorsFilter(urlBasedCorsConfigurationSource);
  49. }
  50. /**
  51. * 静态资源的配置 - 使得可以从磁盘中读取 Html、图片、视频、音频等
  52. */
  53. @Override
  54. public void addResourceHandlers(ResourceHandlerRegistry registry) {
  55. registry.addResourceHandler("/**")
  56. .addResourceLocations("file:" + upLoadPath + "//", "file:" + webAppPath + "//")
  57. .addResourceLocations(staticLocations.split(StringUtils.COMMA));
  58. }
  59. /**
  60. * 访问根路径默认跳转 index.html页面 (简化部署方案: 可以把前端打包直接放到项目的 webapp,上面的配置)
  61. */
  62. @Override
  63. public void addViewControllers(ViewControllerRegistry registry) {
  64. registry.addViewController("/").setViewName("index.html");
  65. }
  66. }