首页 > 解决方案 > 重定向被 CORS 策略阻止(React + SpringBoot)

问题描述

我有一个基本的 react-app 运行在localhost:3000.

该 react-app 对localhost:9093. 应用程序9093使用 Spring MVC 将重定向返回到另一个 SpringBoot 应用程序localhost:8183。最后,应用程序 on8183返回一个重定向回 react-app on localhost:3000

当我尝试这个时,我在我的 chrome 开发工具中收到以下消息:

Access to XMLHttpRequest at 'http://localhost:3000/?err_code=inv_otk' (redirected from 'http://localhost:9093/saml?email=a@a.com') from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

如果最终目的地和起点都在localhost:3000,为什么我会遇到问题?

如果需要更多信息,请告诉我!

标签: javascriptreactjsspringspring-bootspring-mvc

解决方案


您可以在应用程序类中使用此代码。它将允许您在 React JS 中使用的所有端口。这必须在 Spring Boot Application 类中完成。

        /*****Cross Origin Implementation*****/
@Override
public void addCorsMappings(CorsRegistry registry) {
    registry.addMapping("/**")
    .allowedMethods("*")
    .allowedOrigins("*")
    .allowedHeaders("*")
    .allowCredentials(true);
}

推荐阅读