在路由器外部访问时出现 Spring React Chrome CORS 错误

Posted

技术标签:

【中文标题】在路由器外部访问时出现 Spring React Chrome CORS 错误【英文标题】:Spring React Chrome CORS error when accessed outside router 【发布时间】:2022-01-01 23:52:50 【问题描述】:

我使用 Chrome 作为我的默认浏览器。我在 Spring 后端和 React 前端中配置了跨源。

春天:@CrossOrigin(origins = "http://localhost:3000") 反应: const USER_API_BASE_URL = "http://localhost:8080/api/v1/users";

当我在我的电脑上工作时,一切都很好。我配置了 NOIP 并将我的路由器配置为将传入路由到我的开发 PC。当我使用外部地址启动会话时,出现 CORS 错误: ...

Access to XMLHttpRequest at 'http://localhost:8080/api/v1/users' from origin 'http://TESTURL.ddns.net:3000' has been blocked by CORS policy: The request client is not a secure context and the resource is in more-private address space `local`.
xhr.js:187 GET http://localhost:8080/api/v1/users net::ERR_FAILED
dispatchXhrRequest @ xhr.js:187
xhrAdapter @ xhr.js:13
dispatchRequest @ dispatchRequest.js:53
request @ Axios.js:108
Axios.<computed> @ Axios.js:129
wrap @ bind.js:9
getUsers @ UserService.js:8
componentDidMount @ LoginComponent.jsx:19
commitLifeCycles @ react-dom.development.js:19816
commitLayoutEffects @ react-dom.development.js:22805
callCallback @ react-dom.development.js:188
invokeGuardedCallbackDev @ react-dom.development.js:237
invokeGuardedCallback @ react-dom.development.js:292
commitRootImpl @ react-dom.development.js:22543
unstable_runWithPriority @ scheduler.development.js:653
runWithPriority$1 @ react-dom.development.js:11039
commitRoot @ react-dom.development.js:22383
finishSyncRender @ react-dom.development.js:21809
performSyncWorkOnRoot @ react-dom.development.js:21795
scheduleUpdateOnFiber @ react-dom.development.js:21190
updateContainer @ react-dom.development.js:24375
(anonymous) @ react-dom.development.js:24760
unbatchedUpdates @ react-dom.development.js:21905
legacyRenderSubtreeIntoContainer @ react-dom.development.js:24759
render @ react-dom.development.js:24842
(anonymous) @ index.js:8
./src/index.js @ index.js:19
__webpack_require__ @ bootstrap:856
fn @ bootstrap:150
1 @ reportWebVitals.js:14
__webpack_require__ @ bootstrap:856
checkDeferredModules @ bootstrap:45
webpackJsonpCallback @ bootstrap:32
(anonymous) @ main.chunk.js:1
Show 4 more frames
createError.js:16 Uncaught (in promise) Error: Network Error
    at createError (createError.js:16)
    at XMLHttpRequest.handleError (xhr.js:99)

...

有没有办法解决这个问题?

【问题讨论】:

你能提供飞行前的请求吗?您是否尝试使用https://*.host-url.com 为TESTURL.ddns.net:3000 添加远程设置? 这能回答你的问题吗? Chrome CORS error on request to localhost dev server from remote site 什么是“飞行前”请求?我在哪里添加您提到的“远程设置”? 【参考方案1】:

解决方案是 在 React 前端变化中

const USER_API_BASE_URL = "http://localhost:8080/api/v1/documenttype";
to
const USER_API_BASE_URL = "http://XXsystem.ddns.net:8080/api/v1/documenttype";

在 Spring 后端发生变化

@CrossOrigin("http://localhost:3000")
to
@CrossOrigin(origins = "http://XXsystem.ddns.net:3000")

有趣的是,'localhost 测试仍然有效! 两全其美。

【讨论】:

以上是关于在路由器外部访问时出现 Spring React Chrome CORS 错误的主要内容,如果未能解决你的问题,请参考以下文章

尝试输入反应应用程序路由时出现 404

将本地 React 前端连接到本地 Spring Boot 中间件应用程序时出现 CORS 错误

从 v3 迁移到 v4 react-router 时出现错误

仅在访问媒体文件时出现 Cors Header 错误,应用程序的其余部分工作正常(DRF、pythonanywhere、React)

尝试通过 Axios (React Native) 访问 IBM Watson Assistant 消息时出现错误 500

2021-8-17springboot引入网关路由时出现spring-cloud-starter-gateway出错