oauth-2.0 - 如果我有服务器端代理,我可以将 Oauth2 授权代码流用于 SPA(React 应用程序)吗?

标签 oauth-2.0 single-sign-on microservices openid-connect identityserver4

在观看了大量关于 OAuth2 的教程后,每个人都反复声明了一个最佳实践——如果你有一个 React 应用程序(或 Angular,或 Ember)——你 必须使用隐式流。

我知道将客户端凭据存储在公开可见的 javascript 中是行不通的。但是,我的情况有点不同:

  • 我只将 Oauth2 用于微服务的单点登录和 token 生成。我选择了它而不是简单地生成 token ,因为支持良好的第三方库是围绕 Oauth2 想法构建的。
  • 我的想法是拥有一个 React 应用程序和一个 ASP.NET MVC 应用程序,它为 javascript 提供服务并充当 API 请求的代理。用户为服务器端应用程序进行身份验证(通过使用 Oauth2 授权代码流)。
  • 然后,如果我需要从 API 检索数据,我会从 React 调用我的 ASP.NET MVC 应用程序(通过发送一个简单的 cookie)。 MVC 应用程序持有 token ,而不会将其暴露给用户的浏览器。
  • 显然,当被调用时,我的 MVC 应用程序将请求重定向到必要的 API,提供不记名 token 。

  • 为了更好地理解为什么我会这么想,以下是我收到的一些可能不寻常的要求:
  • 我真的不希望访问 token 被共享 - 即使它的生命周期相对较短。
  • 我还希望能够将每个用户帐户限制为 3 个并发用户 session 。使用 cookie 和服务器端 session 很容易做到。

  • 我无法理解为什么这个想法会那么糟糕。是否有任何技术问题可能会阻止此操作?或者可能存在安全风险?

    最佳答案

    The authorization code flow returns an authorization code (like it says on the tin) that can then be exchanged for an ID token and access token. This requires client authentication using a client id and secret to retrieve the tokens from the back end and has the benefit of not exposing tokens to the User Agent.

    This flow allows for long lived access (through the use of refresh tokens). Clients using this flow must be able to maintain a secret.


    根据您的描述,您有服务到服务的授权流程,并且由于您的服务没有公开客户端 key ,因此完全可以使用代码流程。此外,您应该使用它来允许长期存在的 token 。

    关于oauth-2.0 - 如果我有服务器端代理,我可以将 Oauth2 授权代码流用于 SPA(React 应用程序)吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42788267/

    相关文章:

    authentication - SSO(单点登录)如何工作

    azure - 将自定义声明 onPremisesSamAccountName 添加到 Azure AD 应用程序注册身份验证的 access_token 中

    amazon-web-services - 使用 AWS Cognito 和子域进行单点登录

    php - apache-mod_auth_kerb; kerb_authenticate_user 使用 user (NULL) 和 auth_type Kerberos 输入;客户没有将他们的凭证委托(delegate)给我们

    architecture - Monolith 和 n Layer 有什么区别?

    google-cloud-platform - 针对 Google IAP 使用 Salesforce 命名凭据

    java - OAuth2-SpringBoot - 刷新 token

    spring - JASIG CAS : single sign out not working

    spring-boot - 微服务无法使用发现服务器找到 zipkin 服务

    c# - 单独 git 存储库中的服务结构项目