WebMvcConfiguration.java 2.9 KB

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