OAuth2的學習小結


OAUTH2核心參數說明

 

grant_type參數說明表格:

grant_type

說明

authorization_code

標准的Server授權模式

password

基於用戶密碼的授權模式

client_credentials

基於APP密鑰的授權模式

refresh_token

刷新accessToken

 

 

response_type參數說明表格:

response_type

說明

code

標准的Server授權模式響應模式

token

腳本的授權響應模式,直接返回token,需要對回調進行校驗

 

OAUTH2各種請求流程

Authorization Code(標准請求流程,必須實現)

標准的的Server授權模式,與目前開放平台的Session機制很像。

 

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"

     }

 

 

 

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

 

 

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"

     }

 

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"

     }

 

 

優先考慮實現的流程

Authorization Code為我們需要優先支持的流程,很多開源的OAUTH實現都是優先實現了該授權流程。ETAO的B2C網站會用這個流程與開放平台交互。

 

 

開源實現

目前OAUTH 2有比較多的開源實現,其中比較好的開源實現是OAuth for Spring Security,大家可以參考http://static.springsource.org/spring-security/oauth/tutorial.html這個網址去具體了解。有興趣的同學可以去這個網址去下載其源代碼看看http://maven.springframework.org/milestone/org/springframework/security/oauth/spring-security-oauth/1.0.0.M2/spring-security-oauth-1.0.0.M2-sources.jar ,容器主要關注下面幾個類:org.springframework.security.oauth2.provider.OAuth2AuthorizationFilter

org.springframework.security.oauth2.provider. DefaultOAuth2GrantManager

org.springframework.security.oauth2.provider.verification.VerificationCodeFilter

第一個和第二個類為參數校驗和參數解析,第三個類為響應生成的類。

TIP主要關注下面的類:

org.springframework.security.oauth2.provider.OAuth2ProtectedResourceFilter

這個類主要實現了對AccessToken的校驗

 

詳細的例子請訪問:http://git.oschina.net/shengzhao/spring-oauth-server


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM