当前位置: 首页 > 知识库问答 >
问题:

Spring云网关如何禁用CORS?

祁远
2023-03-14

我想从前端应用程序调用POST请求,但是在控制台中我看到:

CORS策略阻止从来源“http://localhost:9090/authenticate”访问位于“http://localhost:3000”的XMLHttpRequest:对飞行前请求的响应未通过访问控制检查:请求的资源上没有“access-control-allog-origin”标头。

spring:
  application:
    name: gateway-service
  cloud:
    gateway:
      globalcors:
        corsConfigurations:
          '[/**]':
            allowedOrigins: "localhost:3000"
            allowedMethods:
              - POST

共有1个答案

阎声
2023-03-14

好的,我找到了解决方案,application.yml中的globalcors部分可能会被删除。您必须实现的是:

@Configuration
public class CORSConfiguration implements WebFluxConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowCredentials(true)
                .allowedOrigins("*")
                .allowedHeaders("*")
                .allowedMethods("*")
                .exposedHeaders(HttpHeaders.SET_COOKIE);
    }

    @Bean
    public CorsWebFilter corsWebFilter() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.setAllowCredentials(true);
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addExposedHeader(HttpHeaders.SET_COOKIE);
        UrlBasedCorsConfigurationSource corsConfigurationSource = new UrlBasedCorsConfigurationSource();
        corsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsWebFilter(corsConfigurationSource);
    }
}
 类似资料:
  • 我有一个带有一些endpoint的anexo API,比如: 如何将Spring Cloud Gateway与这些endpoint一起使用?

  • 几天来,他一直试图用Keycloak连接s-c-gateway和s-c-security。我希望位于网关后面的模块不要有keycloak配置。Spring云网关不支持Spring云安全吗?

  • 配置文件如下: java keytools生成的密钥库。项目可以正常启动,当我通过网关请求时,错误。消息如下: io。内蒂。处理程序。ssl。NotSslRecordException:不是SSL/TLS记录:474554202f55414d532f75616d737465737420485454502f312e310d0a486f73743a203139322e3136382e302e313a3

  • 我为所有传入请求创建了全局路由,并使用AbstracTerrorWebExceptionHandler。 我的application.yml 运行应用程序时的日志: 谢谢你的帮助。

  • 我使用的是堆栈下 SpringCloudGatewayHoxton发行版,Java1.8,Linux操作系统 我看到下面的异常在重启后消失,但在一段时间后再次出现。我没有为直接内存做任何显式设置。下面是使用的JVM参数- 有人能告诉我错误的原因吗?

  • 我一直在读关于spring cloud gateway在我的微服务架构中实现API网关的文章。我需要阻止某些URL我一直在使用一些内部操作。但是,我已经像在Zuul中一样在gateway中使用了IgnoredServices和IgnoredPatterns,但是在Spring cloud gateway链接中没有这样的东西。我的内部API以/internal/{something}开头。 同样,我