簡體   English   中英

用於用戶和會話的REST API URI設計?

[英]REST API URI Design for users and sessions?

我是REST設計的新手,所以我想在這里提供一些建議。

這是我到目前為止的內容:

GET    /api/users                 //all users
GET    /api/users/123             //specific
GET    /api/users/me              //get your own profle 
POST   /api/users/me/session      //start new session (login)
DELETE /api/users/me/session      //ends the current session (logout)

我想知道有關會話/登錄/注銷的信息。 我在這里想的是對的還是應該以其他方式設計使其更具REST風格?

另外,關於注冊用戶,應該是:

POST /api/users

即使它也開始新的會話?

我建議避免使用術語“會話”,而使用auth(如身份驗證)。 術語會話給人一種通常與REST相抵觸的服務器端會話的印象。

以下是很好的:

GET    /api/users                 //all users
GET    /api/users/123             //specific
GET    /api/users/me              //get your own profile 

對於身份驗證,您可能需要執行以下操作:

POST   /api/auth                  //username/password required. auth_token is sent back
DELETE /api/auth                  //auth_token is sent in HTTP header

REST是無狀態的,這意味着服務器不需要在連接的應用程序上保持活動狀態。 引用羅伊·菲爾丁

 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 

驗證用戶身份的常見方法是在請求的身份驗證標頭中添加“秘密”或訪問令牌,您可以使用例如OAuth來實現

另外,添加身份驗證標頭時,無需區分:

GET    /api/users/123             //specific
GET    /api/users/me              //get your own profile 

因為您可以簡單地檢查服務器端是否要求的用戶令牌與身份驗證標頭中的令牌匹配,所以您返回“我”配置文件。 POST /api/users確實是一種將用戶添加到系統的方法。 因此:

GET    /api/users     //all
GET    /api/user/{id} //a user
PUT    /api/user/{id} //update
POST   /api/user      //Add new user
DELETE /api/user/{id} //Remove user

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM