文章目錄
authentication 認證訪問者是誰
authorization 訪問權限
一. Basic access authentication
使用Basic access authentication時,每一個request都包含了一個header,格式爲:
Authorization: Basic <credentials>
其中,credentials時使用base64對id與password進行編碼的,id與password之間用 :
隔開。
缺點:
- 每個瀏覽器要存credentials,每個瀏覽器的存儲策略到不同。比如ie中只存15m。
- 由於credential是使用base64編碼的,常常需要何https配合使用。
二. OAuth
允許用戶讓第三方應用訪問該用戶在某一網站上存儲的私密的資源(如照片,視頻,聯繫人列表等),而無需將用戶名和密碼提供給第三方應用。授權模式總共有5中,常見的有三種。
2.1 授權碼模式(authorization code grant)
參考 What is the OAuth 2.0 Authorization Code Grant Type?
。
流程:
- 第三方應用打開瀏覽器
- 用戶看到授權提示並授權
- 攜帶授權碼(
authorization code
)重定向到第三方應用 - 第三方應用使用授權碼獲取
access token
2.2 簡化模式(implicit grant)
參考 What is the OAuth 2.0 Implicit Grant Type?
這種方式的適用環境通常是一些javascript應用沒有比較好的安全方式來存儲 authorization code。
流程:
- 第三方應用打開瀏覽器
- 用戶看到授權提示並授權
- 用戶攜帶着
access token
返回第三方應用
2.3 密碼模式 (password grant)
參考 What is the OAuth 2.0 Password Grant Type?
適用與你自己的應用(受信app),不適合給第三方應用使用。
流程:
- 應用收集用戶的賬號密碼(直接輸入)
- 受信應用發起一次post請求,使用password獲取acess token。
2.4 oauth2.0 授權方式的選擇
OAuth site
Spring Security Architecture
三. Cookie/Session 認證機制
需要在客戶端的瀏覽器端創建了一個Cookie對象,萬一Cookie被刪了呢
四. jwt
json web token。cookie/session 認證機制中服務器端都要存當前用戶信息,而在json web token認證中,用戶信息存在客戶端。
五.參考文獻
4種認證(authentication)或授權(authorization)方式
Web開發中常見的認證機制
Basic access authentication
OAuth 2.0
理解OAuth 2.0