如何使用单页应用程序和 API 后端进行身份验证? [英] How to do authentication with a single page app and API backend?

查看:27
本文介绍了如何使用单页应用程序和 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 进行身份验证.为此,据我所知,客户端(= 单页应用程序)向 API 发送一个 client_id 和一个 user_id,然后 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/new),
  3. 服务器为应用创建令牌并将其发回,
  4. 客户端应用将令牌以及登录名、密码和请求签名发送到 API(例如 POST/api/v1/mobile_authenticate),
  5. API 验证凭据,
  6. 如果一切正常,应用将使用令牌代表用户进一步操作.

有什么想法吗?如何简化或改进?

Any thoughts? How can this be simplified or improved?

推荐答案

我在服务器 API 中使用 api 密钥进行权限检查.api key 的工作流程如下.

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/users/validate).
  3. 客户端应用程序使用 api 密钥访问 API.(如果是http请求,可以在http头或查询字符串中包含api键.)

希望有帮助.

这篇关于如何使用单页应用程序和 API 后端进行身份验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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