網上找了一堆文章全是說這樣寫無效
globalcors:
cors-configurations:
'[/**]':
allowCredentials: true
allowedOriginPatterns: "*"
allowedMethods: "*"
allowedHeaders: "*"
都說要去代碼里面寫這樣一個CorsWebFilter才能生效
@Configuration
public class CorsConfig {
@Bean
public CorsWebFilter corsFilter() {
CorsConfiguration config = new CorsConfiguration();
config.addAllowedMethod("*");
config.addAllowedOrigin("*");
config.addAllowedHeader("*");
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
source.registerCorsConfiguration("/**", config);
return new CorsWebFilter(source);
}
}
這個確實可以用,但我們能直接配置的話不是方便很多嗎?
后來我去官方文檔里面找到了這樣一段話

所以重點來了
spring:
cloud:
gateway:
globalcors:
cors-configurations:
'[/**]':
allowCredentials: true
allowedOriginPatterns: "*"
allowedMethods: "*"
allowedHeaders: "*"
add-to-simple-url-handler-mapping: true
這樣配置就可以生效了,簡單嗎?
我的版本是spring cloud gateway 3.0.0
如果對您有幫助,請幫忙點贊分享評論,謝謝
