spring-gateway CORS报错?

下面是报错信息:

Access to XMLHttpRequest at 'https://xxx.com/account/info/...' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Credentials' header in the response is '' which must be 'true' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

下面是spring-gateway配置:

spring:
  cloud:
    loadbalancer.ribbon.enabled: false
    gateway:
      globalcors:
        add-to-simple-url-handler-mapping: true
        corsConfigurations:
          '[/**]':
            # 为true时allowedOrigins不允许为* 会报错
            allowCredentials: true
            allowedOrigins: "http://localhost:3000"
            allowedMethods: "*"
            allowedHeaders: "*"
      default-filters:
        - DedupeResponseHeader=Access-Control-Allow-Origin Access-Control-Allow-Credentials
      discovery:
        locator:
          #gateway可以通过开启以下配置来打开根据服务的serviceId来匹配路由,默认是false大写
          lower-case-service-id: true
          # 是否可以通过其他服务的serviceId来转发到具体的服务实例。默认为false
          enabled: true
      routes:
      - id: auth-service
        uri: lb:http://auth-service # lb://serviceId
        predicates:
        - Path=/auth/**
        filters:
        - StripPrefix=1
      - id: message-service
        uri: lb:http://message-service # lb://serviceId
        predicates:
        - Path=/message/**
        filters:
        - StripPrefix=1
      - id: space-service
        uri: lb:http://space-service # lb://serviceId
        predicates:
        - Path=/space/**
        filters:
        - StripPrefix=1
      - id: account-service
        uri: lb:http://account-service # lb://serviceId
        predicates:
        - Path=/account/**
        filters:
        - StripPrefix=1
      - id: users-service
        uri: lb:http://users-service # lb://serviceId
        predicates:
        - Path=/users/**
        filters:
        - StripPrefix=1
      - id: notify-service
        uri: lb:http://notify-service # lb://serviceId
        predicates:
        - Path=/notify/**
        filters:
        - StripPrefix=1
      - id: search-service
        uri: lb:http://search-service # lb://serviceId
        predicates:
        - Path=/search/**
        filters:
        - StripPrefix=1
阅读 2.6k
1 个回答

升级了spring-gateway重启就好了

 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.1</version>
    </parent>

    <properties>
        <java.version>1.8</java.version>
        <spring-boot.version>2.7.1</spring-boot.version>
        <spring-cloud.version>2021.0.1</spring-cloud.version>
        <spring-cloud-alibaba.version>2021.0.1.0</spring-cloud-alibaba.version>
    </properties>
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进