未使用 CORS 的春季 Yml 配置



我已经在Spring yml配置中配置了CORS允许的源,如下所示。

endpoints:
  cors:
    allowed-origins: http://client.local
    allow-credentials: true

但是直到我添加了一个 Java 配置,它才被应用,如下所示

@Configuration
@EnableWebMvc
public class WebConfiguration extends WebMvcConfigurerAdapter {
    @Value("${endpoints.cors.allowed-origins}")
    private String allowedOrigins;
    @Value("${endpoints.cors.allow-credentials}")
    private boolean allowCredentials;
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/api/**")
                .allowedOrigins(allowedOrigins)
                .allowCredentials(allowCredentials);
    }
}

我想保留 yml 配置并丢弃 Java 配置,为什么不应用 yml 配置?

我知道

,可能为时已晚,但我会尝试...是否已将spring-boot-starter-actuator添加到依赖项中?

不使用自定义属性,请使用 Spring Boot 提供的端点。

前任:

management.endpoints.web.cors.allowed-origins=http://domain:port

Spring 引导应用程序属性配置

最新更新