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示例

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