我需要一些澄清。我一直在阅读关于REST和构建基于REST的应用程序的书籍。根据维基百科,REST本身被定义为具象状态传输。因此,我不理解所有这些无国籍的官样文章,每个人都在不停地吐出来。

从维基百科:

在任何特定的时间,客户端可以处于之间的转换 应用程序处于“静止”状态。处于休息状态的客户端可以 与用户交互,但不创建负载,也不消耗每个客户端 服务器集或网络上的存储。

他们只是说不要使用会话/应用程序级数据存储吗?

我知道REST的一个目标是使URI访问一致且可用,例如,不是在帖子中隐藏分页请求,而是使请求的页码成为get URI的一部分。对我来说很有道理。但是,说每个客户端数据(会话数据)都不应该存储在服务器端似乎有点过头了。

如果我有一个消息队列,而我的用户想要读取消息,但在读取消息时,又想在会话期间阻止某些发送者的消息通过,该怎么办?将其存储在服务器端的某个位置,并让服务器只发送未被用户阻止的消息(或消息ID),这难道不合理吗?

每次请求新的消息列表时,我真的必须发送整个消息发送者列表来阻止吗?与我相关的消息列表在一开始就不应该是公开可用的资源。

我只是想理解一下。谁来澄清一下。


更新:

我发现了一个堆栈溢出的问题,它的答案并不能让我一直走到那里: 如何在REST中管理状态 这说明客户端状态很重要,应该在每个请求上都传输....Ugg . .看起来开销很大…这样对吗??


当前回答

您必须在客户端管理客户端会话。这意味着您必须在每个请求中发送身份验证数据,并且您可能(但不一定)在服务器上有一个内存缓存,它将身份验证数据与用户信息(如身份、权限等)配对。

这种REST无状态约束非常重要。如果不应用此约束,您的服务器端应用程序将不能很好地扩展,因为维护每个客户机会话将是它的阿喀琉斯之踵。

其他回答

最基本的解释是:

服务器上没有客户端会话状态。

无状态意味着服务器不在服务器端存储任何关于客户端会话的状态。

客户端会话存储在客户端上。服务器无状态意味着每个服务器可以在任何时间服务任何客户端,不存在会话关联或会话保持。相关的会话信息存储在客户机上,并根据需要传递给服务器。

这并不妨碍与web服务器通信的其他服务维护有关业务对象(如购物车)的状态,而不是有关客户端当前应用程序/会话状态的状态。

客户端的应用程序状态永远不应该存储在服务器上,而是从客户端传递到每个需要它的地方。

这就是REST中的ST(状态传输)的由来。您传递状态,而不是让服务器存储它。这是扩展到数百万并发用户的唯一方法。如果没有别的原因,只是因为数百万次会话就是数百万次会话。

会话管理的负载被分摊到所有客户机上,客户机存储它们的会话状态,服务器可以以无状态的方式为多个数量级或更多的客户机提供服务。

即使对于一个您认为只需要成千上万并发用户的服务,您仍然应该使您的服务无状态。几万还是几万,会有时间和空间成本。

无状态是HTTP协议和web一般设计的操作方式,是一个总体上更简单的实现,您有一个单一的代码路径,而不是一堆服务器端逻辑来维护一堆会话状态。

这里有一些非常基本的实现原则:

这些是原则而不是实现,你如何满足这些原则可能会有所不同。

总而言之,五个主要原则是:

给每件“东西”一个ID 把事物联系起来 使用标准方法 具有多个表示形式的资源 无状态通信

REST论文中没有任何关于身份验证或授权的内容。

因为验证RESTful请求与验证非RESTful请求并没有什么不同。身份验证与RESTful讨论无关。

解释如何为您的特定需求创建无状态应用程序,对于StackOverflow来说太宽泛了。

实现与REST相关的身份验证和授权甚至过于宽泛,一般在互联网上详细解释了各种实现方法。

寻求帮助/信息的评论将/应该被标记为 不再需要。

您完全正确,支持与服务器的完全无状态交互确实给客户机增加了额外的负担。但是,如果考虑扩展应用程序,客户机的计算能力与客户机的数量成正比。因此,扩展到大量的客户是更加可行的。

只要您让服务器负责管理与特定客户端交互相关的一些信息,这个负担就会迅速增长到消耗服务器。

这是一种权衡。

在开发RESTful服务时,为了登录,您需要对用户进行身份验证。一种可能的选择是在每次执行用户操作时发送用户名和密码。在这种情况下,服务器将根本不存储会话数据。

Another option is to generate a session-id on the server and send it to the client, so the client will be able to send session-id to the server and authenticate with that. This is much much safer than sending username and password each time, since if somebody gets their hand on that data, then he/she can impersonate the user until the username and password is changed. You may say that even the session id can be stolen and the user will be impersonated in that case and you are right. However, in this case impersonating the user will only be possible while the session id is valid.

如果RESTful API需要用户名和密码才能更改用户名和密码,那么即使有人使用会话id冒充用户,黑客也无法锁定真正的用户。

会话id可以通过单向锁定(加密)某个标识用户的东西并将时间添加到会话id中来生成,这样就可以定义会话的过期时间。

The server may or may not store session ids. Of course, if the server stores the session id, then it would violate the criteria defined in the question. However, it is only important to make sure that the session id can be validated for the given user, which does not necessitate storing the session id. Imagine a way that you have a one-way-encryption of email, user id and some user-specific private data, like favorite color, this would be the first level and somehow adding the username date to the encrypted string and apply a two-way encryption. As a result when a session id is received, the second level could be decrypted to be able to determine which username the user claims to be and whether the session time is right. If this is valid, then the first level of encryption could be validated by doing that encryption again and checking whether it matches the string. You do not need to store session data in order to achieve that.

Stateless means the state of the service doesn’t persist between subsequent requests and response. Each request carries its own user credentials and is individually authenticated. But in stateful each request is known from any prior request. All stateful requests are session-oriented i.e. each request need to know and retain changes made in previous requests. Banking application is an example of stateful application. Where user first login then make transaction and logs out. If after logout user will try to make the transaction, he will not be able to do so. Yes, http protocol is essentially a stateless protocol but to make it stateful we make us of HTTP cookies. So, is SOAP by default. But it can be make stateful likewise, depends upon framework you are using. HTTP is stateless but still we can maintain session in our java application by using different session tracking mechanism. Yes, We can also maintain session in webservice whether it is REST or SOAP. It can be implemented by using any third party library or you can implement by our own.

摘自http://gopaldas.org/webservices/soap/webservice-is-stateful-or-stateless-rest-soap

无状态和有状态的主要区别在于每次都将数据传递回服务器。在无状态的情况下,客户端必须提供所有的信息,所以很多参数可能需要在每个请求中传递。在有状态的情况下,cliet只传递一次这些参数,它们由服务器维护,直到客户端再次修改。

在我看来,API应该是无状态的,这允许快速扩展。