ITPub博客

首页 > 应用开发 > Java > 网关路由 gateway 跨域问题

网关路由 gateway 跨域问题

原创 Java 作者:fhadmin 时间:2021-03-31 11:25:23 0 删除 编辑

1.写个配置类CorsConfig (如果不行,可以不写此类,和springcloud的版本有关)

package net.youqu.micro.service.config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.web.cors.CorsConfiguration;import org.springframework.web.cors.reactive.CorsWebFilter;import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;import org.springframework.web.util.pattern.PathPatternParser;/**
 * description:
 * java项目fhadmin。org
 */@Configurationpublic 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);
    }
}

2.  配置 application.properties 方式

#--------gateway配置--------#跨域配置spring.cloud.gateway.globalcors.corsConfigurations.[/**].allowedOrigins=*spring.cloud.gateway.globalcors.corsConfigurations.[/**].allowedHeaders=*spring.cloud.gateway.globalcors.corsConfigurations.[/**].allowedMethods=*spring.cloud.gateway.globalcors.corsConfigurations.[/**].allowCredentials=true

yaml 方式

spring:  cloud:    gateway:      globalcors:        corsConfigurations:          '[/**]':            allowCredentials: true            allowedHeaders: '*'
            allowedMethods: '*'
            allowedOrigins: '*'

 


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31558068/viewspace-2765862/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2018-10-12

  • 博文量
    310
  • 访问量
    166459