理解OAuth2.0

OAuth 2.0

简介

OAuth2.0是OAuth协议的延续版本,但不向前兼容OAuth 1.0(即完全废止了OAuth1.0)。 OAuth 2.0关注客户端开发者的简易性。要么通过组织在资源拥有者和HTTP服务商之间的被批准的交互动作代表用户,要么允许第三方应用代表用户获得访问的权限。同时为Web应用,桌面应用和手机,和起居室设备提供专门的认证流程。2012年10月,OAuth 2.0协议正式发布为RFC 6749。

官方协议:OAuth2地址

业务场景

日常场景介绍

外卖员需要进入一个含有门禁系统的大楼,进入该大楼需要通过密码,而将密码交给外卖员不安全的。

问题:

  • 外卖员不止一位
  • 当我需要禁止外卖员进入小区,只能我更改密码

解决目标:

  • 外卖员自由进入小区
  • 外卖员无需知道小区密码

解决方案

解决以上方法,通过授权机制,来解决。

    1. 小区门禁系统新增“获取授权”操作
    1. 外卖员点击“获取授权”操作,采集外卖员姓名、头像、工号等信息
    1. 门禁系统将采集的信息发送给我,我将采集的信息与外卖软件上的信息对比,确认信息无误,同意授权。
    1. 门禁系统得到我的确认,给外卖员一个令牌(access token),并只有30分钟的有效期。
    1. 外卖员通过令牌,进入小区送餐。

以上流程,就是OAuth授权机制。

互联网场景介绍

  1. QQ、CSDN等大型网站,存储了用户个人账户等信息。
  2. 其他应用需要接入QQ、CSDN等等,实现用户登录
  3. 用户授权其他应用登录QQ、CSDN等等大型网站,获取我的个人头像、昵称等等个人信息。

OAuth授权机制

其实授权所给的令牌与密码作用一致,但令牌具有以下特点:

  • 令牌具有时效性,短期有效
  • 令牌具有scope属性

OAuth2授权机制

OAuth2标准定义的四种授权方式

  • Authorization Code(授权码)
  • Implicit(隐含式)
  • Resource Owner Password Credentials(密码模式)
  • Client Credentials(客户端模式)

这里我只介绍最常用的Authorization Code(授权码)方式

Authorization Code(授权码)

The authorization code is obtained by using an authorization server
as an intermediary between the client and resource owner. Instead of
requesting authorization directly from the resource owner, the client
directs the resource owner to an authorization server (via its
user-agent as defined in [RFC2616]), which in turn directs the
resource owner back to the client with the authorization code.

Before directing the resource owner back to the client with the
authorization code, the authorization server authenticates the
resource owner and obtains authorization. Because the resource owner
only authenticates with the authorization server, the resource
owner’s credentials are never shared with the client.

The authorization code provides a few important security benefits,
such as the ability to authenticate the client, as well as the
transmission of the access token directly to the client without
passing it through the resource owner’s user-agent and potentially
exposing it to others, including the resource owner.

以上是官方解释。


  1. A网站提供链接,跳转到B网站提供的链接,授权B网站中的用户数据给A网站使用。

微信官方提供的链接

//固定地址+参数
String baseUrl = "https://open.weixin.qq.com/connect/qrconnect" +
        "?appid=%s" +
        "&redirect_uri=%s" +
        "&response_type=code" +
        "&scope=snsapi_login" +
        "&state=%s" +
        "#wechat_redirect";

appid:应用唯一标识
redirect_uri:重定向地址,请使用urlEncode对链接进行处理
response_type:要求返回授权码。
scope:授权范围。应用授权作用域,拥有多个作用域用逗号(,)分隔,网页应用目前仅填写snsapi_login
state:用于保持请求和回调的状态,授权请求后原样带回给第三方。该参数可用于防止csrf攻击(跨站请求伪造攻击),建议第三方带上该参数,可设置为简单的随机数加session进行校验

流程图1

  1. B网站要求用户登录,并确认授权给A网站个人信息等资料;用户同意》》B网站跳回指定redirect_uri,并附带一个授权码。
redirect_uri?code=CODE&state=STATE

流程图2

  1. A网站获取授权码,通过授权码,向B网站请求令牌(access_token)。

https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code

  • appid:应用唯一标识
  • secret:应用密钥
  • code:上一步获取的授权码
  • grant_type:微信官方默认填authorization_code。标识采用的授权方式为Authorization Code(授权码)方式

流程图3

  1. B网站接收请求,颁发令牌(access_token),向A网站回复一段JSON数据
{ 
"access_token":"ACCESS_TOKEN", 
"expires_in":7200, 
"refresh_token":"REFRESH_TOKEN",
"openid":"OPENID", 
"scope":"SCOPE",
"unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL"
}

流程图4

  1. A网站可以凭借获取的access_token向B网站获取信息
http请求方式: GET
https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID

流程图5

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值