简体   繁体   English

重定向被 CORS 策略阻止(React + SpringBoot)

[英]Redirect Blocked by CORS Policy (React + SpringBoot)

I have a basic react-app running on localhost:3000 .我有一个基本的 react-app 在localhost:3000上运行。

That react-app makes an axios call to a SpringBoot app on localhost:9093 .该 react-app 对localhost:9093上的 SpringBoot 应用程序进行 axios 调用。 The app on 9093 uses Spring MVC to return a redirect to another SpringBoot app on localhost:8183 . 9093上的应用程序使用 Spring MVC 将重定向返回到localhost:8183上的另一个 SpringBoot 应用程序。 Finally, the app on 8183 returns a redirect back to the react-app on localhost:3000 .最后, 8183上的应用程序将重定向返回到localhost:3000上的 react-app。

When I try this, I get the following message in my chrome dev tools:当我尝试这个时,我在我的 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.

If the eventual destination as well as the origin are both on localhost:3000 , why am I getting issues?如果最终目的地和原点都在localhost:3000 ,为什么我会遇到问题?

Please let me know if more info is needed!如果需要更多信息,请告诉我!

You may need @CrossOrigin annotation on your controller.您可能需要在控制器上添加 @CrossOrigin 注释。

@RestController
@RequestMapping("/users")
@CrossOrigin
public class UserController {
    // doSomething();
}

The Access-Control-Allow-Origin error comes from an ajax call that is revoked by the browser whose name is OPTIONS call. Access-Control-Allow-Origin错误来自一个 ajax 调用,该调用被名为OPTIONS调用的浏览器撤销。

The browser when seeing the code wanna call an endpoint with a different URL of current origin try to call an OPTIONS call to know is it permitted to call it.浏览器在看到代码想要调用具有当前来源的不同 URL 的端点时尝试调用OPTIONS调用以了解是否允许调用它。 so when the OPTIONS call won't successful the browser prevents from running whole the current code.所以当OPTIONS调用不成功时,浏览器会阻止运行整个当前代码。

For the solution, you should try some ways to pass the OPTIONS call.对于解决方案,您应该尝试一些方法来传递OPTIONS调用。 one of them is inserting proxy inside your Webpack config and pass all OPTIONS call 200.其中之一是在您的 Webpack 配置中插入代理并传递所有OPTIONS调用 200。

Another way is passing 200 answers for every OPTIONS calls on your backend project.另一种方法是为后端项目中的每个OPTIONS调用传递 200 个答案。

Also, you can switch-off your browser security just for the development area.此外,您可以仅为开发区域关闭浏览器安全性。

You can use this code in the application class.您可以在应用程序类中使用此代码。 It will allow all the ports you will use in React JS.它将允许您将在 React JS 中使用的所有端口。 This has to be done in the Spring Boot Application class.这必须在 Spring Boot Application 类中完成。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM