我想开发一个 REST API。 REST 指南指定状态不得存储在服务器端。 但是我要实现的 REST 方法暗示了用户连接管理。为了尊重无状态,我是否需要在每个 REST 方法请求中提供用户凭据?我发现这样效率很低……难道没有其他更有效的方法吗??
最佳答案
无状态是 REST 架构的主要限制之一,可以在原文中阅读 publication :
5.1.3 Stateless
We next add a constraint to the client-server interaction: communication must be stateless in nature, as in the client-stateless-server (CSS) style of Section 3.4.3 (Figure 5-3), such that each request from client to server must contain all of the information necessary to understand the request, and cannot take advantage of any stored context on the server. Session state is therefore kept entirely on the client.
因此,对于您提到的凭据,您应该在每次调用中分别提供它们(即基本身份验证 + SSL)。当然,这是在“真实世界”出现的情况下,应用程序开始有所不同。您也可以使用 OAuth、固定 token 等,但请记住,您正在削弱 API 的“RESTfulness”。
关于web-services - Web 服务中的 REST 无状态和用户 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4634473/