且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

REST API登录模式

更新时间:2022-02-11 21:29:41

现代Web的原则性设计建筑由Roy Fielding的T.和Richard N.泰勒,所有REST术语是从哪里来的作品即序列,包含客户端 - 服务器交互的定义:

Principled Design of the Modern Web Architecture by Roy T. Fielding and Richard N. Taylor, i.e. sequence of works from all REST terminology came from, contains definition of client-server interaction:

所有REST相互作用的 无国籍 的。也就是说,每个 请求包含
  所有的必需的信息的连接器,了解
  要求,独立,可能有$ P $任何请求pceded它
的。

All REST interactions are stateless. That is, each request contains all of the information necessary for a connector to understand the request, independent of any requests that may have preceded it.

这限制完成四大功能,第一和第三重要的是在这种特殊情况下:

This restriction accomplishes four functions, 1st and 3rd is important in this particular case:


  • 1 的:它的删除任何需要的连接器保持应用程序状态
       请求之间
    ,物理资源从而降低消耗
       并提高可扩展性;

  • 3 的:它可以让​​中介来查看和了解隔离的要求
       这可能是必要的,当服务被动态地重新排列;

  • 1st: it removes any need for the connectors to retain application state between requests, thus reducing consumption of physical resources and improving scalability;
  • 3rd: it allows an intermediary to view and understand a request in isolation, which may be necessary when services are dynamically rearranged;

现在让我们回到您的安全情况。每一个请求应该包含所有必需的信息,并授权/认证也不例外。如何实现这一目标?从字面上把所有需要的信息通过电线与每个请求。

And now lets go back to your security case. Every single request should contains all required information, and authorization/authentication is not an exception. How to achieve this? Literally send all required information over wires with every request.

一个例子如何既成功又流行,这是 基于哈希的消息验证code HMAC 。实际上,这意味着将当前消息的哈希值code每一个要求。通过的加密散列函数的联合计算哈希值code进行的秘密密钥的。的加密散列函数的要么是predefined或 code点播的一部分的REST概念(例如JavaScript的)。的秘密密钥的应该由服务器提供给客户端的资源,客户端使用它来计算哈希值code为每个请求。

One of examples how to archeive this is hash-based message authentication code or HMAC. In practice this means adding a hash code of current message to every request. Hash code calculated by cryptographic hash function in combination with a secret cryptographic key. Cryptographic hash function is either predefined or part of code-on-demand REST conception (for example JavaScript). Secret cryptographic key should be provided by server to client as resource, and client uses it to calculate hash code for every request.

有很多的 HMAC 的实施例子,但我希望你要注意以下三种:

There are a lot of examples of HMAC implementations, but I'd like you to pay attention to the following three:

  • Authenticating REST Requests for Amazon Simple Storage Service (Amazon S3)
  • Answer by Mauriceless on quiestion: "How to implement HMAC Authentication in a RESTful WCF API"
  • crypto-js: JavaScript implementations of standard and secure cryptographic algorithms

如果客户知道密钥,那么它的准备与资源工作。否则,他将暂时重定向(状态code 307临时重定向)授权并获得密钥,然后重定向回原始资源。在这种情况下,存在的无需事先知道(即硬code某处)什么的URL来授权客户端的,并有可能调整该模式随时间

If client knows the secret key, then it's ready to operate with resources. Otherwise he will be temporarily redirected (status code 307 Temporary Redirect) to authorize and to get secret key, and then redirected back to the original resource. In this case there is no need to know beforehand (i.e. hardcode somewhere) what the URL to authorize the client is, and it possible to adjust this schema with time.

希望这将帮助您找到妥善的解决办法!

Hope this will helps you to find the proper solution!