|
@@ -1,11 +1,14 @@
|
|
|
package com.ruixuan.framework.config;
|
|
package com.ruixuan.framework.config;
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
+import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
|
|
import org.springframework.context.annotation.Bean;
|
|
import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
|
+import org.springframework.core.Ordered;
|
|
|
import org.springframework.web.cors.CorsConfiguration;
|
|
import org.springframework.web.cors.CorsConfiguration;
|
|
|
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
|
|
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
|
|
|
import org.springframework.web.filter.CorsFilter;
|
|
import org.springframework.web.filter.CorsFilter;
|
|
|
|
|
+import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
|
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
|
|
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
|
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
|
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
|
@@ -13,6 +16,8 @@ import com.ruixuan.common.config.RuoYiConfig;
|
|
|
import com.ruixuan.common.constant.Constants;
|
|
import com.ruixuan.common.constant.Constants;
|
|
|
import com.ruixuan.framework.interceptor.RepeatSubmitInterceptor;
|
|
import com.ruixuan.framework.interceptor.RepeatSubmitInterceptor;
|
|
|
|
|
|
|
|
|
|
+import java.util.Arrays;
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 通用配置
|
|
* 通用配置
|
|
|
*
|
|
*
|
|
@@ -46,7 +51,22 @@ public class ResourcesConfig implements WebMvcConfigurer
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * 跨域配置
|
|
|
|
|
|
|
+ * 跨域配置 - WebMvcConfigurer方式
|
|
|
|
|
+ */
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void addCorsMappings(CorsRegistry registry)
|
|
|
|
|
+ {
|
|
|
|
|
+ registry.addMapping("/**")
|
|
|
|
|
+ .allowedOriginPatterns("*")
|
|
|
|
|
+ .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
|
|
|
|
|
+ .allowedHeaders("*")
|
|
|
|
|
+ .exposedHeaders("*")
|
|
|
|
|
+ .allowCredentials(true)
|
|
|
|
|
+ .maxAge(3600);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 跨域过滤器(最高优先级)
|
|
|
*/
|
|
*/
|
|
|
@Bean
|
|
@Bean
|
|
|
public CorsFilter corsFilter()
|
|
public CorsFilter corsFilter()
|
|
@@ -59,8 +79,8 @@ public class ResourcesConfig implements WebMvcConfigurer
|
|
|
config.addAllowedHeader("*");
|
|
config.addAllowedHeader("*");
|
|
|
// 设置访问源请求方法
|
|
// 设置访问源请求方法
|
|
|
config.addAllowedMethod("*");
|
|
config.addAllowedMethod("*");
|
|
|
- // 暴露响应头
|
|
|
|
|
- config.addExposedHeader("*");
|
|
|
|
|
|
|
+ // 暴露响应头 - 明确列出常用响应头
|
|
|
|
|
+ config.setExposedHeaders(Arrays.asList("Authorization", "Content-Type", "X-Requested-With", "accept", "Origin", "Access-Control-Request-Method", "Access-Control-Request-Headers", "Access-Control-Allow-Origin", "Access-Control-Allow-Credentials"));
|
|
|
// 有效期 1800秒
|
|
// 有效期 1800秒
|
|
|
config.setMaxAge(1800L);
|
|
config.setMaxAge(1800L);
|
|
|
// 添加映射路径,拦截一切请求
|
|
// 添加映射路径,拦截一切请求
|
|
@@ -69,4 +89,16 @@ public class ResourcesConfig implements WebMvcConfigurer
|
|
|
// 返回新的CorsFilter
|
|
// 返回新的CorsFilter
|
|
|
return new CorsFilter(source);
|
|
return new CorsFilter(source);
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 注册CORS过滤器,设置最高优先级
|
|
|
|
|
+ */
|
|
|
|
|
+ @Bean
|
|
|
|
|
+ public FilterRegistrationBean<CorsFilter> corsFilterRegistration()
|
|
|
|
|
+ {
|
|
|
|
|
+ FilterRegistrationBean<CorsFilter> registration = new FilterRegistrationBean<>(corsFilter());
|
|
|
|
|
+ registration.setOrder(Ordered.HIGHEST_PRECEDENCE);
|
|
|
|
|
+ registration.addUrlPatterns("/*");
|
|
|
|
|
+ return registration;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|