grant_type參數說明表格:html
grant_type前端 |
說明git |
authorization_codegithub |
標準的Server受權模式spring |
passwordjson |
基於用戶密碼的受權模式瀏覽器 |
client_credentialsapp |
基於APP密鑰的受權模式框架 |
refresh_tokenide |
刷新accessToken |
response_type參數說明表格:
response_type |
說明 |
code |
標準的Server受權模式響應模式 |
token |
腳本的受權響應模式,直接返回token,須要對回調進行校驗 |
標準的的Server受權模式,與目前開放平臺的Session機制很像。第一步獲取code,第二步code換token。
第一步:APP首先發送獲取code請求
GET /authorize?response_type=code&client_id=s6BhdRkqt3&
redirect_uri=https%3A%2F%2Fclient%2Eexample%2Ecom%2Fcb HTTP/1.1
Host: server.example.com
容器返回code
HTTP/1.1 302 Found
Location: https://client.example.com/cb?code=i1WsRn1uB1
第二步:APP根據code發送獲取token請求
POST /token HTTP/1.1
Host: server.example.com
Content-Type: application/x-www-form-urlencoded
grant_type=authorization_code&client_id=s6BhdRkqt3&
client_secret=gX1fBat3bV&code=i1WsRn1uB1&
redirect_uri=https%3A%2F%2Fclient%2Eexample%2Ecom%2Fcb
容器直接返回token
HTTP/1.1 200 OK
Content-Type: application/json
Cache-Control: no-store
{
"access_token":"SlAV32hkKG",
"token_type":"example",
"expires_in":3600,
"refresh_token":"8xLOxBtZp8",
"example_parameter":"example-value"
}
標準oauth2流程圖
適用於運行於瀏覽器中的腳本應用,須要校驗callback地址,並且只返回該應用註冊的回調地址
APP直接請求token
GET /authorize?response_type=token&client_id=s6BhdRkqt3&
redirect_uri=https%3A%2F%2Fclient%2Eexample%2Ecom%2Fcb HTTP/1.1
Host: server.example.com
容器經過重定向返回token
HTTP/1.1 302 Found
Location: http://example.com/rd#access_token=FJQbwq9&
token_type=example&expires_in=3600
稱之爲用戶名密碼模式,須要提供終端用戶的用戶名和密碼,適用於好比操做系統或者高權限的應用。
APP直接帶上用戶名和密碼請求
POST /token HTTP/1.1
Host: server.example.com
Content-Type: application/x-www-form-urlencoded
grant_type=password&client_id=s6BhdRkqt3&
client_secret=47HDu8s&username=johndoe&password=A3ddj3w
容器直接返回token
HTTP/1.1 200 OK
Content-Type: application/json
Cache-Control: no-store
{
"access_token":"SlAV32hkKG",
"token_type":"example",
"expires_in":3600,
"refresh_token":"8xLOxBtZp8",
"example_parameter":"example-value"
}
基於APP的密鑰直接進行受權,APP的權限很是大,慎用。這個模式能夠考慮用於目前咱們不須要彈出受權的特殊應用,如淘江湖,前端插件等。
APP直接根據客戶端的密碼來請求
POST /token HTTP/1.1
Host: server.example.com
Content-Type: application/x-www-form-urlencoded
grant_type=client_credentials&client_id=s6BhdRkqt3&
client_secret=47HDu8s
容器直接返回token
HTTP/1.1 200 OK
Content-Type: application/json
Cache-Control: no-store
{
"access_token":"SlAV32hkKG",
"token_type":"example",
"expires_in":3600,
"refresh_token":"8xLOxBtZp8",
"example_parameter":"example-value"
}
代碼地址1:https://github.com/favccxx/FavOAuth2 【oauth2-server】
代碼地址2:http://git.oschina.net/mkk/oauth2-shiro 【oauth2-server】
代碼地址3:http://git.oschina.net/mkk/spring-oauth-client/ 【oauth2-client】