首页 > 解决方案 > application.properties 中的 web.cors.allowed-origins 不允许特定 URL 的 CORS

问题描述

我正在尝试在整个应用程序中为“ http://localhost/4200 ”启用 CORS。由于我使用的是 Springboot 2 和一致。在它的文档中,我只需要在 application.properties 文件中添加这个属性:

management.endpoints.web.cors.allowed-origins=http://localhost:4200

但这不起作用。其次,由于我通过 restController 发出请求,出于测试目的,我尝试添加内联:

CrossOrigin(origins = "http://localhost:4401")

这也允许数据流通过 4401 端口。

标签: spring-bootgetcorsspring-boot-2

解决方案


无法从 application.properties 文件设置 CORS 设置。属性 management.endpoints.web.cors.allowed-origins 与 Spring Actuator 相关。

如果您想使用特定端点的设置,您可以使用您在问题中提到的 @CrossOrigin 注释。

如果要为应用程序设置全局策略,可以添加配置类/bean 来设置 CORS 设置。请参阅:https ://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-cors

相关问题: Spring Boot 通过 application.properties 启用 CORS


推荐阅读