Spring Cloud Gateway 跨域 CORS 配置方式實現


網上找了一堆文章全是說這樣寫無效

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

如果對您有幫助,請幫忙點贊分享評論,謝謝


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM