一个 RESTful API 需要是无状态的,所以我们不能在服务器端保存上下文数据。但是,出于安全原因,我不想在每个请求上发送登录名/密码。所以,我想知道下面描述的方式是否尊重REST的文章«5.1.3 Stateless»。
5.1.3 Stateless [...] 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. [...]
首先,客户端发出认证请求,返回用户的ID和保存在服务器端的随机授权令牌。
/auth/ { login, password } => { userID, authorizationToken }
然后,对于下一个需要认证的请求,例如更新用户配置文件,客户端发送所有必要的信息,包括参数中的用户 ID,并在请求的头部放入授权令牌。
在服务器端,我们检查标头中的授权令牌是否正确、未过期以及是否与请求参数中设置的用户 ID 相关联。
谢谢你的帮助 !
PS:我知道还有另一种解决方案,例如 JSON Web Token,但这不是我的问题 :)