springcloud的gateway处理跨域问题

gateway处理跨域问题,有两种方式,一种是配置文件,另外一种是代码

配置文件

spring:
  cloud:
    gateway:
      globalcors:
        cors-configurations:
          '[/**]':
            allowCredentials: true
            allowedMethods: "*"
            allowedHeaders: "*"

代码

@Configuration
public class CornConfig {

    @Bean
    public CorsWebFilter corsWebFilter(){
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedHeader("*");
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
        source.registerCorsConfiguration("/**",corsConfiguration);

        return new CorsWebFilter(source);
    }
}

 

以上内容纯学习使用!

 

posted @ 2024-09-27 17:51  多多指教~  阅读(359)  评论(0)    收藏  举报