小码问答,有问必答!

2023-07-12 09:23

gateway网关项目启动报错

王姐姐 2023-07-12 09:23 回答了这个问题

JavaEE

Failed to bind properties under 'spring.cloud.gateway.globalcors.cors-configurations.allowedorigins' to org.springframework.web.cors.CorsConfiguration:


    Reason: org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.String] to type [org.springframework.web.cors.CorsConfiguration]


Action:


Update your application's configuration


出现这个错误通常是因为在配置文件中未正确设置 spring.cloud.gateway.globalcors.cors-configurations.allowedorigins 属性。allowedorigins 应该是一个字符串列表,包含允许的来源站点。

确保在配置文件(如 application.propertiesapplication.yml)中正确设置了该属性。例如,在 application.yml 文件中设置允许的来源站点:

spring:
  cloud:
    gateway:
      globalcors:
        cors-configurations:
          "[/**]":
            allowedOrigins:
              - https://example.com
              - https://example2.com

如果你在 Java 配置类中进行配置,可以使用以下方式:

@Configurationpublic class GatewayConfig {    @Bean
    public CorsFilter corsFilter() {        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();        CorsConfiguration config = new CorsConfiguration();
        config.setAllowedOrigins(Arrays.asList("https://example.com", "https://example2.com"));
        source.registerCorsConfiguration("/**", config);        return new CorsFilter(source);
    }
}


0条评论

我要评论