基於Oauth2的api接口開發(一)

1.OAUTH2核心參數說明

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,須要對回調進行校驗

 

2.OAUTH2各類請求流程

2.1.Authorization Code(標準請求流程,必須實現)

標準的的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流程圖

 

2.2.Implicit Grant(直接發放模式)

適用於運行於瀏覽器中的腳本應用,須要校驗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

 

2.3.Resource Owner Password Credentials (基於用戶名與密碼模式)

稱之爲用戶名密碼模式,須要提供終端用戶的用戶名和密碼,適用於好比操做系統或者高權限的應用。

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"

     }

 

2.4.Client Credentials

基於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"

     }

3.oauth2實現

Spring security+oauth2框架與Shiro+oltu相比,後者的入門門檻相對較低, 代碼更加透明, 理解更容易,可擴展性更強, 且模塊化開發。

  • Spring security+oauth2框架
  1. Spring Oauth2,參考http://www.oschina.net/p/spring-oauth-server.
  2. 官方開發指南http://projects.spring.io/spring-security-oauth/docs/oauth2.html 
  3. 中文版http://www.oschina.net/translate/oauth-2-developers-guide

代碼地址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】

 

  • 推薦資料:

http://www.ruanyifeng.com/blog/2014/05/oauth_2_0.html

相關文章
相關標籤/搜索