javascript - 重定向被 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
,为什么我会遇到问题?
如果需要更多信息,请告诉我!
解决方案
您可以在应用程序类中使用此代码。它将允许您在 React JS 中使用的所有端口。这必须在 Spring Boot Application 类中完成。
/*****Cross Origin Implementation*****/
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedMethods("*")
.allowedOrigins("*")
.allowedHeaders("*")
.allowCredentials(true);
}
推荐阅读
- python-3.x - 如何在python中对小数点后的数字进行四舍五入
- android - 在 ListView 中,为什么我不断获取第一行的值而不是所选行的值?
- c# - “索引”和“范围”运算符 - 它们是什么?
- r - 将字符串拆分为数字和字符串(有缺失)
- bash - Bash:一种尺寸适合所有转义用于密码参数的特殊字符的解决方案
- c# - Angular 6 - 单个表单组中的多个文件输入
- nlp - fasttext skipgram 和 word2vec skipgram 有什么区别?
- docker - 为什么我一直在另一台机器上访问 docker 守护进程,但它总是失败
- javascript - 如何使用 Node.js、ws 和 es6 类包装 WebSocket 服务器类?
- node.js - 如何在快递应用中设置多选路线?