|
@@ -1,13 +1,18 @@
|
|
|
package com.xlht.xlhtproject;
|
|
|
|
|
|
+import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
-import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
|
|
+import org.springframework.web.cors.CorsConfiguration;
|
|
|
+import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
|
|
|
+import org.springframework.web.filter.CorsFilter;
|
|
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
|
|
|
|
|
+import java.util.Collections;
|
|
|
+
|
|
|
@Configuration
|
|
|
public class CorsConfig implements WebMvcConfigurer {
|
|
|
|
|
|
- @Override
|
|
|
+ /*@Override
|
|
|
public void addCorsMappings(CorsRegistry registry) {
|
|
|
// 添加映射路径,"/**"表示对所有的路径都进行跨域处理
|
|
|
registry.addMapping("/**")
|
|
@@ -18,5 +23,22 @@ public class CorsConfig implements WebMvcConfigurer {
|
|
|
.allowedHeaders("*") // 允许所有的头部
|
|
|
.allowCredentials(true) // 允许发送Cookie
|
|
|
.maxAge(3600); // 预检请求的缓存时间,单位为秒
|
|
|
+ }*/
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public CorsFilter corsFilter() {
|
|
|
+ CorsConfiguration corsConfiguration = new CorsConfiguration();
|
|
|
+ //1.允许任何来源
|
|
|
+ corsConfiguration.setAllowedOriginPatterns(Collections.singletonList("*"));
|
|
|
+ //2.允许任何请求头
|
|
|
+ corsConfiguration.addAllowedHeader(CorsConfiguration.ALL);
|
|
|
+ //3.允许任何方法
|
|
|
+ corsConfiguration.addAllowedMethod(CorsConfiguration.ALL);
|
|
|
+ //4.允许凭证
|
|
|
+ corsConfiguration.setAllowCredentials(true);
|
|
|
+
|
|
|
+ UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
|
|
|
+ source.registerCorsConfiguration("/**", corsConfiguration);
|
|
|
+ return new CorsFilter(source);
|
|
|
}
|
|
|
}
|