javascript - OAuth2 重定向 URL 应该指向前端还是后端?

标签 javascript cookies oauth-2.0 csrf create-react-app

我正在使用授权授予流程在我的应用程序中设置 OAuth2。我还使用 create-react-app,这样我就在 localhost:3000 上进行开发,它代理到 localhost:8080 上的应用程序服务器后端。

一切都基本正常,除了我无法让 CSRF token 正常工作。

我意识到这是因为我将 OAuth2 重定向 URL 设置为后端,因此它没有发送私有(private)加密的 csrf_state cookie,因为请求源自 google我的应用程序。

我认为这在生产中不会成为问题,因为不会有代理服务器。相反,后端和前端都将从同一个 mydomain.com

提供服务

那么,我不应该开发这项工作吗?或者我应该将 OAuth2 重定向 URL 设置为我的前端 (localhost:3000),然后自动重定向到后端 (localhost:8080),以便它可以发送私有(private)加密的 CSRF token ?

或者有没有办法让 cookie 源自 google,而不需要多个重定向?或者我应该不理会 CSRF,因为 SameSite 现在在浏览器中拥有如此广泛的支持?

最佳答案

OAuth2.0 授权代码授予包括使用 state 参数的 CSRF 保护。使用它而不是依赖 cookie。

state

RECOMMENDED. An opaque value used by the client to maintain state between the request and callback. The authorization server includes this value when redirecting the user-agent back to the client. The parameter SHOULD be used for preventing cross-site request forgery as described in Section 10.12.

Source: https://www.rfc-editor.org/rfc/rfc6749#section-4.1

关于javascript - OAuth2 重定向 URL 应该指向前端还是后端?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59124914/

相关文章:

cookies - ASPNetCore : OpenId Cookies always show expires=1969-12-31 even with IsPersistent=true and ExpireTimeSpan set

node.js - 使用 googleapis 获取 Google 日历事件列表

java - Spring OAuth2 - 无法获取客户端 token

javascript - jQuery 方法 $.on 返回什么?

javascript - 检测左滚动 JavaScript

javascript - CORS 请求后,AJAX 无法在浏览器上设置 Cookie。 Set-Cookie header 仅存在于 FireFox 中,而不存在于 Chrome 中

node.js - nodejs : sessionID is changing after redirect. 如何保持用户 session 数据持久化?

javascript - 当组的所有 ul>li 不包含过滤词时隐藏 <h3>

javascript - 打开一个新选项卡并写一些东西

javascript - 使用nodejs向微软团队 channel 发送消息