Oauth2.0知識學習(一)

官網直達

  • 四種角色

    • 資源擁有者(Resource Owner): 通常是自己個人
    • 資源服務器(Resource Server): 數據信息在該服務器上保存, 使用API接口攜帶token請求該服務器
    • 應用(Client): 指的第三方應用
    • 授權服務器(Authorization Server): 返回令牌token的服務器, 一般和資源服務器是相同的
  • grant_type分爲幾種:

  1. Resource owner password-based --密碼式
    1. 高度信任應用(高危險)
    2. POST方法請求
    3. 參數需要:賬戶(username)和密碼(password)
    4. 請求token返回json值中包含token
擁有者第三方應用授權服務器資源服務器客戶憑證(用戶賬號/密碼)請求token返回access_token[refresh_token]攜帶Token調用接口返回響應數據擁有者第三方應用授權服務器資源服務器
  1. Authorization code – 授權碼(QQ第三方好像用的是這個;鏈接)
    1. 四種中安全性最高
    2. GET請求
    3. 參數需要
      3.1 client_id: app應用憑證
      3.2 scope: 可選參數; 對此授權權限
      3.3 response_type: code [固定值]
      3.4 redirect_uri: 即將跳轉的網址
    4. 返回code: 在redirect_uri後邊加上?code=xxx
    5. 後端根據code去獲取token
第三方應用授權服務器擁有者資源服務器認證請求登錄認證返回code使用code請求Token返回對應的access_token攜帶Token請求接口返回響應數據第三方應用授權服務器擁有者資源服務器
  1. Implicit – 隱藏式
    1. 不安全(token暴露在前端)
    2. GET請求
    3. 參數需要
      3.1 client_id: app應用憑證
      3.2 scope: 可選參數; 對此授權權限
      3.3 response_type: token [固定值]
      3.4 redirect_uri: 即將跳轉的網址
    4. 返回url後攜帶: access_token等信息
第三方應用授權服務器擁有者資源服務器請求Token登錄認證返回access_token返回對應的access_token攜帶Token請求接口返回響應數據第三方應用授權服務器擁有者資源服務器
  1. Client credentials – 客戶端憑證
    1. 不安全
    2. GET請求
    3. 參數
      3.1. client_id: app的id
      3.2. client_secret: app的祕鑰
      3.3. grant_type: client_credentials[固定值]
      3.4. scope: 權限(可選參數)
第三方應用(擁有者)授權服務器資源服務器請求Token返回access_token攜帶Token請求接口返回響應數據第三方應用(擁有者)授權服務器資源服務器

存在的問題

主要說一下curl API時候的存在的問題 !

#官方描述
curl -X POST -d "grant_type=password&username=<user_name>&password=<password>" -u"<client_id>:<client_secret>" http://localhost:8000/o/token/
  1. username --是註冊的郵箱賬號,不是姓名
    否則會出現–
    {"error_description": "Invalid credentials given.", "error": "invalid_grant"}
    
  2. curl -H "Authorization: Bearer access_token" http://localhost:8000/users/
    
    
    I. 當grant_type設置爲"Resource owner password-based",則在curl的時候,grant_type=password,而且"Client type"需要設置爲"public"
    否則,報錯–
    {"error": "invalid_client"}
    
    II. 當grant_type設置爲"Client credentials",
    curl -X POST -d "grant_type=client_credentials" -u"<client_id>:<client_secret>" http://localhost:8000/o/token/
    
    
  3. client_id --這個是創建時候自己定義的。
    #另外一種寫法
    curl -X POST http://localhost:8000/o/token/ -d "[email protected]&password=admin123&grant_type=password&client_id=<client_id>"
    
    #備註:基本所有的參數都能通過 ‘&變量=’ 添加上,相當於傳遞表單
    

下一篇: oauth2.0的結合Django示例

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章