如何做认证通过一个单页的应用程序和API后台? [英] How to do authentication with a single page app and API backend?

查看:309
本文介绍了如何做认证通过一个单页的应用程序和API后台?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我自己都在api.example.com一个API的后端,并在example.com一个前端单页的应用程序。该API基本上是一个数据库后端的包装。

I own both, an API backend at api.example.com and a frontend single page app at example.com . The API is basically a wrapper for a database backend.

现在,我想有单页的应用程序(=客户端应用程序)的用户使用API​​进行身份验证。对于这一点,据我了解,在客户端(=单页的应用程序)一起发送CLIENT_ID用USER_ID的API,然后API给出了一个的accessToken。

Now, I want to have a user of the single page app (= client app) to authenticate with the API. For this, as far as I understand, the client (= single page app) sends a client_id together with a user_id to the API, and the API then gives out an AccessToken.

不过,在我单页的应用程序,我不知道在哪里/如何存储的访问令牌。我要寻找一个简单的参考,还是一个很好的概念,在用户访问记录的API应用程序。

However, in my single page app, I am not sure where/how to store the access token. I am looking for a simple reference, or a good concept to have a logged in user access the API application.

一个朋友建议我这个流量:

A friend suggested me this flow:


  1. 客户端显示登录输入(无论是电子邮件或用户名)和密码,

  2. 的客户端应用程序,使你的API,以获取非授权令牌(例如POST / API / V1 / auth /中新)

  3. 服务器创建应用程序的令牌,并发送回来,

  4. 的客户端应用程序发送令牌的登录名,密码并要求签名的API(例如POST / API / V1 / mobile_authenticate)
  5. 沿
  6. 的API验证并验证凭据,

  7. 如果一切正常的应用程序使用该令牌来进一步使代表用户的。

有什么想法?这怎么能简化或改进?

Any thoughts? How can this be simplified or improved?

推荐答案

我使用的服务器API的权限检查API密钥。 API密钥是如何工作的工作流程如下图所示。

I'm using the api key for the permission check in the server API. The workflow how the api key works is as below.


  1. 的客户端应用程序显示登录输入(无论是电子邮件或用户名)和
    密码。

  2. 客户端应用程序,使得对API的请求,以获得
    API密钥(例如POST / API / V1 /用户/验证)。

  3. 的客户端应用程序访问的API密钥的API。 (如果是HTTP请求时,API密钥可以被包含在HTTP报头或查询字符串。)

希望它帮助。

这篇关于如何做认证通过一个单页的应用程序和API后台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆