【发布时间】:2020-05-14 05:59:44
【问题描述】:
我尝试构建一个 Spring Boot 后端并将我的 React 前端与它连接起来。在我的本地部署中,我的 API 请求失败,因为 localhost:3000 不支持 CrossOrigin。 我在 Spring Boot 中插入了“CORSGlobalConfig”:
@Configuration
@EnableWebFlux
public class CorsGlobalConfig implements WebFluxConfigurer {
@Override
public void addCorsMappings(CorsRegistry corsRegistry) {
corsRegistry.addMapping("/**")
.allowedOrigins("http://localhost:3000")
.allowedMethods("PUT","POST","GET");
}
}
在我看来,这应该可以正常工作,但有些地方不对。我需要在某处实现我的配置吗? 错误如下所示:
Access to XMLHttpRequest at 'http://localhost:8080/REQUESTPATH'
from origin 'http://localhost:3000' has been blocked by CORS policy:
Response to preflight request doesn't pass access control check:
No 'Access-Control-Allow-Origin' header is present on the requested resource.
感谢您的帮助!
【问题讨论】:
标签: reactjs spring spring-boot cors spring-webflux