首页
导航
博客
电子书
算法
众创
代码
随贴
关于我们
您好,欢迎来到码863代码分享网! 请
[登录]
/
[注册]
搜 索
标题:
*
140
字
TAG标签:
(用空格隔开)
30
字
恢复历史版本:
请选择分类
html
python
javascript
php
sql
c
c++
c#
java
plain
所有人可见
仅自己可见
编辑器:UEditor
编辑器:TinyMCE
编辑器:Editor.md
HTML转MD
HTML转MD2
<p><a href="http://www.ruanyifeng.com/blog/2014/05/restful_api.html">http://www.ruanyifeng.com/blog/2014/05/restful_api.html</a></p><p><a href="http://en.wikipedia.org/wiki/OAuth" target="_blank" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(17, 34, 51);">OAuth</a>是一个关于授权(authorization)的开放网络标准,在全世界得到广泛应用,目前的版本是2.0版。</p><p>本文对OAuth 2.0的设计思路和运行流程,做一个简明通俗的解释,主要参考材料为<a href="http://www.rfcreader.com/#rfc6749" target="_blank" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(17, 34, 51);">RFC 6749</a>。</p><p><img src="/ueditor/php/upload/image/20200609/1591647713154699.png" alt="OAuth Logo" title="" style="margin: 0px; padding: 0px; list-style-type: none; border: 0.3em solid rgb(224, 223, 204); border-radius: 1em;"/></p><blockquote style="margin: 2em; padding: 1em; list-style-type: none; border: 0.3em solid rgb(224, 223, 204); color: rgb(17, 17, 17); background-color: rgb(245, 242, 240); border-radius: 1em; font-family: Consolas, Monaco, "Andale Mono", monospace; text-shadow: white 0px 1px; overflow: auto; font-size: 10px; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px;"><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.8px;">更新:我后来又写了一组三篇的<a href="http://www.ruanyifeng.com/blog/2019/04/oauth_design.html" target="_blank" style="margin: 0px; padding: 0px; list-style-type: none; border: none; color: rgb(17, 34, 51);"> 《OAuth 2.0 教程》</a>,更加通俗,并带有代码实例,欢迎阅读。</p></blockquote><h2 style="margin: 1.5em 0px 0.2em; padding: 0px 0px 0.2em; list-style-type: none; font-weight: 500; border-top: none; border-right: none; border-bottom: 1px solid rgb(211, 211, 211); border-left: none; border-image: initial; font-size: 2.88em; line-height: 1.35em; letter-spacing: -0.03em; font-family: Georgia, serif; white-space: normal; word-spacing: 2.4px; background-color: rgb(245, 245, 213);">一、应用场景</h2><p>为了理解OAuth的适用场合,让我举一个假设的例子。</p><p>有一个"云冲印"的网站,可以将用户储存在Google的照片,冲印出来。用户为了使用该服务,必须让"云冲印"读取自己储存在Google上的照片。</p><p><img src="/ueditor/php/upload/image/20200609/1591647715754566.png" alt="云冲印" title="" style="margin: 0px; padding: 0px; list-style-type: none; border: 0.3em solid rgb(224, 223, 204); border-radius: 1em;"/></p><p>问题是只有得到用户的授权,Google才会同意"云冲印"读取这些照片。那么,"云冲印"怎样获得用户的授权呢?</p><p>传统方法是,用户将自己的Google用户名和密码,告诉"云冲印",后者就可以读取用户的照片了。这样的做法有以下几个严重的缺点。</p><blockquote style="margin: 2em; padding: 1em; list-style-type: none; border: 0.3em solid rgb(224, 223, 204); color: rgb(17, 17, 17); background-color: rgb(245, 242, 240); border-radius: 1em; font-family: Consolas, Monaco, "Andale Mono", monospace; text-shadow: white 0px 1px; overflow: auto; font-size: 10px; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px;"><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.8px;">(1)"云冲印"为了后续的服务,会保存用户的密码,这样很不安全。</p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.8px;">(2)Google不得不部署密码登录,而我们知道,单纯的密码登录并不安全。</p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.8px;">(3)"云冲印"拥有了获取用户储存在Google所有资料的权力,用户没法限制"云冲印"获得授权的范围和有效期。</p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.8px;">(4)用户只有修改密码,才能收回赋予"云冲印"的权力。但是这样做,会使得其他所有获得用户授权的第三方应用程序全部失效。</p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.8px;">(5)只要有一个第三方应用程序被破解,就会导致用户密码泄漏,以及所有被密码保护的数据泄漏。</p></blockquote><p>OAuth就是为了解决上面这些问题而诞生的。</p><h2 style="margin: 1.5em 0px 0.2em; padding: 0px 0px 0.2em; list-style-type: none; font-weight: 500; border-top: none; border-right: none; border-bottom: 1px solid rgb(211, 211, 211); border-left: none; border-image: initial; font-size: 2.88em; line-height: 1.35em; letter-spacing: -0.03em; font-family: Georgia, serif; white-space: normal; word-spacing: 2.4px; background-color: rgb(245, 245, 213);">二、名词定义</h2><p>在详细讲解OAuth 2.0之前,需要了解几个专用名词。它们对读懂后面的讲解,尤其是几张图,至关重要。</p><blockquote style="margin: 2em; padding: 1em; list-style-type: none; border: 0.3em solid rgb(224, 223, 204); color: rgb(17, 17, 17); background-color: rgb(245, 242, 240); border-radius: 1em; font-family: Consolas, Monaco, "Andale Mono", monospace; text-shadow: white 0px 1px; overflow: auto; font-size: 10px; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px;"><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.8px;">(1) <span style="font-weight: 800;">Third-party application</span>:第三方应用程序,本文中又称"客户端"(client),即上一节例子中的"云冲印"。</p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.8px;">(2)<span style="font-weight: 800;">HTTP service</span>:HTTP服务提供商,本文中简称"服务提供商",即上一节例子中的Google。</p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.8px;">(3)<span style="font-weight: 800;">Resource Owner</span>:资源所有者,本文中又称"用户"(user)。</p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.8px;">(4)<span style="font-weight: 800;">User Agent</span>:用户代理,本文中就是指浏览器。</p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.8px;">(5)<span style="font-weight: 800;">Authorization server</span>:认证服务器,即服务提供商专门用来处理认证的服务器。</p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.8px;">(6)<span style="font-weight: 800;">Resource server</span>:资源服务器,即服务提供商存放用户生成的资源的服务器。它与认证服务器,可以是同一台服务器,也可以是不同的服务器。</p></blockquote><p>知道了上面这些名词,就不难理解,OAuth的作用就是让"客户端"安全可控地获取"用户"的授权,与"服务商提供商"进行互动。</p><h2 style="margin: 1.5em 0px 0.2em; padding: 0px 0px 0.2em; list-style-type: none; font-weight: 500; border-top: none; border-right: none; border-bottom: 1px solid rgb(211, 211, 211); border-left: none; border-image: initial; font-size: 2.88em; line-height: 1.35em; letter-spacing: -0.03em; font-family: Georgia, serif; white-space: normal; word-spacing: 2.4px; background-color: rgb(245, 245, 213);">三、OAuth的思路</h2><p>OAuth在"客户端"与"服务提供商"之间,设置了一个授权层(authorization layer)。"客户端"不能直接登录"服务提供商",只能登录授权层,以此将用户与客户端区分开来。"客户端"登录授权层所用的令牌(token),与用户的密码不同。用户可以在登录的时候,指定授权层令牌的权限范围和有效期。</p><p>"客户端"登录授权层以后,"服务提供商"根据令牌的权限范围和有效期,向"客户端"开放用户储存的资料。</p><h2 style="margin: 1.5em 0px 0.2em; padding: 0px 0px 0.2em; list-style-type: none; font-weight: 500; border-top: none; border-right: none; border-bottom: 1px solid rgb(211, 211, 211); border-left: none; border-image: initial; font-size: 2.88em; line-height: 1.35em; letter-spacing: -0.03em; font-family: Georgia, serif; white-space: normal; word-spacing: 2.4px; background-color: rgb(245, 245, 213);">四、运行流程</h2><p>OAuth 2.0的运行流程如下图,摘自RFC 6749。</p><p><img src="/ueditor/php/upload/image/20200609/1591647716172637.png" alt="OAuth运行流程" title="" style="margin: 0px; padding: 0px; list-style-type: none; border: 0.3em solid rgb(224, 223, 204); border-radius: 1em;"/></p><blockquote style="margin: 2em; padding: 1em; list-style-type: none; border: 0.3em solid rgb(224, 223, 204); color: rgb(17, 17, 17); background-color: rgb(245, 242, 240); border-radius: 1em; font-family: Consolas, Monaco, "Andale Mono", monospace; text-shadow: white 0px 1px; overflow: auto; font-size: 10px; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px;"><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.8px;">(A)用户打开客户端以后,客户端要求用户给予授权。</p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.8px;">(B)用户同意给予客户端授权。</p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.8px;">(C)客户端使用上一步获得的授权,向认证服务器申请令牌。</p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.8px;">(D)认证服务器对客户端进行认证以后,确认无误,同意发放令牌。</p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.8px;">(E)客户端使用令牌,向资源服务器申请获取资源。</p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.8px;">(F)资源服务器确认令牌无误,同意向客户端开放资源。</p></blockquote><p>不难看出来,上面六个步骤之中,B是关键,即用户怎样才能给于客户端授权。有了这个授权以后,客户端就可以获取令牌,进而凭令牌获取资源。</p><p>下面一一讲解客户端获取授权的四种模式。</p><h2 style="margin: 1.5em 0px 0.2em; padding: 0px 0px 0.2em; list-style-type: none; font-weight: 500; border-top: none; border-right: none; border-bottom: 1px solid rgb(211, 211, 211); border-left: none; border-image: initial; font-size: 2.88em; line-height: 1.35em; letter-spacing: -0.03em; font-family: Georgia, serif; white-space: normal; word-spacing: 2.4px; background-color: rgb(245, 245, 213);">五、客户端的授权模式</h2><p>客户端必须得到用户的授权(authorization grant),才能获得令牌(access token)。OAuth 2.0定义了四种授权方式。</p><ul style="list-style-type: none;" class=" list-paddingleft-2"><li><p>授权码模式(authorization code)</p></li><li><p>简化模式(implicit)</p></li><li><p>密码模式(resource owner password credentials)</p></li><li><p>客户端模式(client credentials)</p></li></ul><h2 style="margin: 1.5em 0px 0.2em; padding: 0px 0px 0.2em; list-style-type: none; font-weight: 500; border-top: none; border-right: none; border-bottom: 1px solid rgb(211, 211, 211); border-left: none; border-image: initial; font-size: 2.88em; line-height: 1.35em; letter-spacing: -0.03em; font-family: Georgia, serif; white-space: normal; word-spacing: 2.4px; background-color: rgb(245, 245, 213);">六、授权码模式</h2><p>授权码模式(authorization code)是功能最完整、流程最严密的授权模式。它的特点就是通过客户端的后台服务器,与"服务提供商"的认证服务器进行互动。</p><p><img src="/ueditor/php/upload/image/20200609/1591647716381043.png" alt="授权码模式" title="" style="margin: 0px; padding: 0px; list-style-type: none; border: 0.3em solid rgb(224, 223, 204); border-radius: 1em;"/></p><p>它的步骤如下:</p><blockquote style="margin: 2em; padding: 1em; list-style-type: none; border: 0.3em solid rgb(224, 223, 204); color: rgb(17, 17, 17); background-color: rgb(245, 242, 240); border-radius: 1em; font-family: Consolas, Monaco, "Andale Mono", monospace; text-shadow: white 0px 1px; overflow: auto; font-size: 10px; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px;"><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.8px;">(A)用户访问客户端,后者将前者导向认证服务器。</p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.8px;">(B)用户选择是否给予客户端授权。</p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.8px;">(C)假设用户给予授权,认证服务器将用户导向客户端事先指定的"重定向URI"(redirection URI),同时附上一个授权码。</p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.8px;">(D)客户端收到授权码,附上早先的"重定向URI",向认证服务器申请令牌。这一步是在客户端的后台的服务器上完成的,对用户不可见。</p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.8px;">(E)认证服务器核对了授权码和重定向URI,确认无误后,向客户端发送访问令牌(access token)和更新令牌(refresh token)。</p></blockquote><p>下面是上面这些步骤所需要的参数。</p><p>A步骤中,客户端申请认证的URI,包含以下参数:</p><ul style="list-style-type: none;" class=" list-paddingleft-2"><li><p>response_type:表示授权类型,必选项,此处的值固定为"code"</p></li><li><p>client_id:表示客户端的ID,必选项</p></li><li><p>redirect_uri:表示重定向URI,可选项</p></li><li><p>scope:表示申请的权限范围,可选项</p></li><li><p>state:表示客户端的当前状态,可以指定任意值,认证服务器会原封不动地返回这个值。</p></li></ul><p>下面是一个例子。</p><blockquote style="margin: 2em; padding: 1em; list-style-type: none; border: 0.3em solid rgb(224, 223, 204); color: rgb(17, 17, 17); background-color: rgb(245, 242, 240); border-radius: 1em; font-family: Consolas, Monaco, "Andale Mono", monospace; text-shadow: white 0px 1px; overflow: auto; font-size: 10px; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px;"><pre class=" language-http" style="margin-top: 0px; margin-bottom: 0px; padding: 0em 0em 0.5em; list-style-type: none; border: none; color: black; text-shadow: white 0px 1px; font-family: monospace, Consolas, Monaco, "Andale Mono"; direction: ltr; word-spacing: normal; font-size: 1.2em; tab-size: 4; hyphens: none; overflow: auto; background-color: rgb(245, 242, 240);">GET /authorize?response_type=code&client_id=s6BhdRkqt3&state=xyz &redirect_uri=https%3A%2F%2Fclient%2Eexample%2Ecom%2Fcb HTTP/1.1Host: server.example.com</pre></blockquote><p>C步骤中,服务器回应客户端的URI,包含以下参数:</p><ul style="list-style-type: none;" class=" list-paddingleft-2"><li><p>code:表示授权码,必选项。该码的有效期应该很短,通常设为10分钟,客户端只能使用该码一次,否则会被授权服务器拒绝。该码与客户端ID和重定向URI,是一一对应关系。</p></li><li><p>state:如果客户端的请求中包含这个参数,认证服务器的回应也必须一模一样包含这个参数。</p></li></ul><p>下面是一个例子。</p><blockquote style="margin: 2em; padding: 1em; list-style-type: none; border: 0.3em solid rgb(224, 223, 204); color: rgb(17, 17, 17); background-color: rgb(245, 242, 240); border-radius: 1em; font-family: Consolas, Monaco, "Andale Mono", monospace; text-shadow: white 0px 1px; overflow: auto; font-size: 10px; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px;"><pre class=" language-http" style="margin-top: 0px; margin-bottom: 0px; padding: 0em 0em 0.5em; list-style-type: none; border: none; color: black; text-shadow: white 0px 1px; font-family: monospace, Consolas, Monaco, "Andale Mono"; direction: ltr; word-spacing: normal; font-size: 1.2em; tab-size: 4; hyphens: none; overflow: auto; background-color: rgb(245, 242, 240);">HTTP/1.1 302 FoundLocation: https://client.example.com/cb?code=SplxlOBeZQQYbYS6WxSbIA &state=xyz</pre></blockquote><p>D步骤中,客户端向认证服务器申请令牌的HTTP请求,包含以下参数:</p><ul style="list-style-type: none;" class=" list-paddingleft-2"><li><p>grant_type:表示使用的授权模式,必选项,此处的值固定为"authorization_code"。</p></li><li><p>code:表示上一步获得的授权码,必选项。</p></li><li><p>redirect_uri:表示重定向URI,必选项,且必须与A步骤中的该参数值保持一致。</p></li><li><p>client_id:表示客户端ID,必选项。</p></li></ul><p>下面是一个例子。</p><blockquote style="margin: 2em; padding: 1em; list-style-type: none; border: 0.3em solid rgb(224, 223, 204); color: rgb(17, 17, 17); background-color: rgb(245, 242, 240); border-radius: 1em; font-family: Consolas, Monaco, "Andale Mono", monospace; text-shadow: white 0px 1px; overflow: auto; font-size: 10px; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px;"><pre class=" language-http" style="margin-top: 0px; margin-bottom: 0px; padding: 0em 0em 0.5em; list-style-type: none; border: none; color: black; text-shadow: white 0px 1px; font-family: monospace, Consolas, Monaco, "Andale Mono"; direction: ltr; word-spacing: normal; font-size: 1.2em; tab-size: 4; hyphens: none; overflow: auto; background-color: rgb(245, 242, 240);">POST /token HTTP/1.1Host: server.example.comAuthorization: Basic czZCaGRSa3F0MzpnWDFmQmF0M2JWContent-Type: application/x-www-form-urlencoded grant_type=authorization_code&code=SplxlOBeZQQYbYS6WxSbIA &redirect_uri=https%3A%2F%2Fclient%2Eexample%2Ecom%2Fcb</pre></blockquote><p>E步骤中,认证服务器发送的HTTP回复,包含以下参数:</p><ul style="list-style-type: none;" class=" list-paddingleft-2"><li><p>access_token:表示访问令牌,必选项。</p></li><li><p>token_type:表示令牌类型,该值大小写不敏感,必选项,可以是bearer类型或mac类型。</p></li><li><p>expires_in:表示过期时间,单位为秒。如果省略该参数,必须其他方式设置过期时间。</p></li><li><p>refresh_token:表示更新令牌,用来获取下一次的访问令牌,可选项。</p></li><li><p>scope:表示权限范围,如果与客户端申请的范围一致,此项可省略。</p></li></ul><p>下面是一个例子。</p><blockquote style="margin: 2em; padding: 1em; list-style-type: none; border: 0.3em solid rgb(224, 223, 204); color: rgb(17, 17, 17); background-color: rgb(245, 242, 240); border-radius: 1em; font-family: Consolas, Monaco, "Andale Mono", monospace; text-shadow: white 0px 1px; overflow: auto; font-size: 10px; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px;"><pre class=" language-http" style="margin-top: 0px; margin-bottom: 0px; padding: 0em 0em 0.5em; list-style-type: none; border: none; color: black; text-shadow: white 0px 1px; font-family: monospace, Consolas, Monaco, "Andale Mono"; direction: ltr; word-spacing: normal; font-size: 1.2em; tab-size: 4; hyphens: none; overflow: auto; background-color: rgb(245, 242, 240);"> HTTP/1.1 200 OK Content-Type: application/json;charset=UTF-8 Cache-Control: no-store Pragma: no-cache { "access_token":"2YotnFZFEjr1zCsicMWpAA", "token_type":"example", "expires_in":3600, "refresh_token":"tGzv3JOkF0XG5Qx2TlKWIA", "example_parameter":"example_value" }</pre></blockquote><p>从上面代码可以看到,相关参数使用JSON格式发送(Content-Type: application/json)。此外,HTTP头信息中明确指定不得缓存。</p><h2 style="margin: 1.5em 0px 0.2em; padding: 0px 0px 0.2em; list-style-type: none; font-weight: 500; border-top: none; border-right: none; border-bottom: 1px solid rgb(211, 211, 211); border-left: none; border-image: initial; font-size: 2.88em; line-height: 1.35em; letter-spacing: -0.03em; font-family: Georgia, serif; white-space: normal; word-spacing: 2.4px; background-color: rgb(245, 245, 213);">七、简化模式</h2><p>简化模式(implicit grant type)不通过第三方应用程序的服务器,直接在浏览器中向认证服务器申请令牌,跳过了"授权码"这个步骤,因此得名。所有步骤在浏览器中完成,令牌对访问者是可见的,且客户端不需要认证。</p><p><img src="/ueditor/php/upload/image/20200609/1591647716812019.png" alt="简化模式" title="" style="margin: 0px; padding: 0px; list-style-type: none; border: 0.3em solid rgb(224, 223, 204); border-radius: 1em;"/></p><p>它的步骤如下:</p><blockquote style="margin: 2em; padding: 1em; list-style-type: none; border: 0.3em solid rgb(224, 223, 204); color: rgb(17, 17, 17); background-color: rgb(245, 242, 240); border-radius: 1em; font-family: Consolas, Monaco, "Andale Mono", monospace; text-shadow: white 0px 1px; overflow: auto; font-size: 10px; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px;"><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.8px;">(A)客户端将用户导向认证服务器。</p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.8px;">(B)用户决定是否给于客户端授权。</p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.8px;">(C)假设用户给予授权,认证服务器将用户导向客户端指定的"重定向URI",并在URI的Hash部分包含了访问令牌。</p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.8px;">(D)浏览器向资源服务器发出请求,其中不包括上一步收到的Hash值。</p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.8px;">(E)资源服务器返回一个网页,其中包含的代码可以获取Hash值中的令牌。</p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.8px;">(F)浏览器执行上一步获得的脚本,提取出令牌。</p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.8px;">(G)浏览器将令牌发给客户端。</p></blockquote><p>下面是上面这些步骤所需要的参数。</p><p>A步骤中,客户端发出的HTTP请求,包含以下参数:</p><ul style="list-style-type: none;" class=" list-paddingleft-2"><li><p>response_type:表示授权类型,此处的值固定为"token",必选项。</p></li><li><p>client_id:表示客户端的ID,必选项。</p></li><li><p>redirect_uri:表示重定向的URI,可选项。</p></li><li><p>scope:表示权限范围,可选项。</p></li><li><p>state:表示客户端的当前状态,可以指定任意值,认证服务器会原封不动地返回这个值。</p></li></ul><p>下面是一个例子。</p><blockquote style="margin: 2em; padding: 1em; list-style-type: none; border: 0.3em solid rgb(224, 223, 204); color: rgb(17, 17, 17); background-color: rgb(245, 242, 240); border-radius: 1em; font-family: Consolas, Monaco, "Andale Mono", monospace; text-shadow: white 0px 1px; overflow: auto; font-size: 10px; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px;"><pre class=" language-http" style="margin-top: 0px; margin-bottom: 0px; padding: 0em 0em 0.5em; list-style-type: none; border: none; color: black; text-shadow: white 0px 1px; font-family: monospace, Consolas, Monaco, "Andale Mono"; direction: ltr; word-spacing: normal; font-size: 1.2em; tab-size: 4; hyphens: none; overflow: auto; background-color: rgb(245, 242, 240);"> GET /authorize?response_type=token&client_id=s6BhdRkqt3&state=xyz &redirect_uri=https%3A%2F%2Fclient%2Eexample%2Ecom%2Fcb HTTP/1.1 Host: server.example.com</pre></blockquote><p>C步骤中,认证服务器回应客户端的URI,包含以下参数:</p><ul style="list-style-type: none;" class=" list-paddingleft-2"><li><p>access_token:表示访问令牌,必选项。</p></li><li><p>token_type:表示令牌类型,该值大小写不敏感,必选项。</p></li><li><p>expires_in:表示过期时间,单位为秒。如果省略该参数,必须其他方式设置过期时间。</p></li><li><p>scope:表示权限范围,如果与客户端申请的范围一致,此项可省略。</p></li><li><p>state:如果客户端的请求中包含这个参数,认证服务器的回应也必须一模一样包含这个参数。</p></li></ul><p>下面是一个例子。</p><blockquote style="margin: 2em; padding: 1em; list-style-type: none; border: 0.3em solid rgb(224, 223, 204); color: rgb(17, 17, 17); background-color: rgb(245, 242, 240); border-radius: 1em; font-family: Consolas, Monaco, "Andale Mono", monospace; text-shadow: white 0px 1px; overflow: auto; font-size: 10px; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px;"><pre class=" language-http" style="margin-top: 0px; margin-bottom: 0px; padding: 0em 0em 0.5em; list-style-type: none; border: none; color: black; text-shadow: white 0px 1px; font-family: monospace, Consolas, Monaco, "Andale Mono"; direction: ltr; word-spacing: normal; font-size: 1.2em; tab-size: 4; hyphens: none; overflow: auto; background-color: rgb(245, 242, 240);"> HTTP/1.1 302 Found Location: http://example.com/cb#access_token=2YotnFZFEjr1zCsicMWpAA &state=xyz&token_type=example&expires_in=3600</pre></blockquote><p>在上面的例子中,认证服务器用HTTP头信息的Location栏,指定浏览器重定向的网址。注意,在这个网址的Hash部分包含了令牌。</p><p>根据上面的D步骤,下一步浏览器会访问Location指定的网址,但是Hash部分不会发送。接下来的E步骤,服务提供商的资源服务器发送过来的代码,会提取出Hash中的令牌。</p><h2 style="margin: 1.5em 0px 0.2em; padding: 0px 0px 0.2em; list-style-type: none; font-weight: 500; border-top: none; border-right: none; border-bottom: 1px solid rgb(211, 211, 211); border-left: none; border-image: initial; font-size: 2.88em; line-height: 1.35em; letter-spacing: -0.03em; font-family: Georgia, serif; white-space: normal; word-spacing: 2.4px; background-color: rgb(245, 245, 213);">八、密码模式</h2><p>密码模式(Resource Owner Password Credentials Grant)中,用户向客户端提供自己的用户名和密码。客户端使用这些信息,向"服务商提供商"索要授权。</p><p>在这种模式中,用户必须把自己的密码给客户端,但是客户端不得储存密码。这通常用在用户对客户端高度信任的情况下,比如客户端是操作系统的一部分,或者由一个著名公司出品。而认证服务器只有在其他授权模式无法执行的情况下,才能考虑使用这种模式。</p><p><img src="/ueditor/php/upload/image/20200609/1591647717668587.png" alt="密码模式" title="" style="margin: 0px; padding: 0px; list-style-type: none; border: 0.3em solid rgb(224, 223, 204); border-radius: 1em;"/></p><p>它的步骤如下:</p><blockquote style="margin: 2em; padding: 1em; list-style-type: none; border: 0.3em solid rgb(224, 223, 204); color: rgb(17, 17, 17); background-color: rgb(245, 242, 240); border-radius: 1em; font-family: Consolas, Monaco, "Andale Mono", monospace; text-shadow: white 0px 1px; overflow: auto; font-size: 10px; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px;"><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.8px;">(A)用户向客户端提供用户名和密码。</p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.8px;">(B)客户端将用户名和密码发给认证服务器,向后者请求令牌。</p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.8px;">(C)认证服务器确认无误后,向客户端提供访问令牌。</p></blockquote><p>B步骤中,客户端发出的HTTP请求,包含以下参数:</p><ul style="list-style-type: none;" class=" list-paddingleft-2"><li><p>grant_type:表示授权类型,此处的值固定为"password",必选项。</p></li><li><p>username:表示用户名,必选项。</p></li><li><p>password:表示用户的密码,必选项。</p></li><li><p>scope:表示权限范围,可选项。</p></li></ul><p>下面是一个例子。</p><blockquote style="margin: 2em; padding: 1em; list-style-type: none; border: 0.3em solid rgb(224, 223, 204); color: rgb(17, 17, 17); background-color: rgb(245, 242, 240); border-radius: 1em; font-family: Consolas, Monaco, "Andale Mono", monospace; text-shadow: white 0px 1px; overflow: auto; font-size: 10px; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px;"><pre class=" language-http" style="margin-top: 0px; margin-bottom: 0px; padding: 0em 0em 0.5em; list-style-type: none; border: none; color: black; text-shadow: white 0px 1px; font-family: monospace, Consolas, Monaco, "Andale Mono"; direction: ltr; word-spacing: normal; font-size: 1.2em; tab-size: 4; hyphens: none; overflow: auto; background-color: rgb(245, 242, 240);"> POST /token HTTP/1.1 Host: server.example.com Authorization: Basic czZCaGRSa3F0MzpnWDFmQmF0M2JW Content-Type: application/x-www-form-urlencoded grant_type=password&username=johndoe&password=A3ddj3w</pre></blockquote><p>C步骤中,认证服务器向客户端发送访问令牌,下面是一个例子。</p><blockquote style="margin: 2em; padding: 1em; list-style-type: none; border: 0.3em solid rgb(224, 223, 204); color: rgb(17, 17, 17); background-color: rgb(245, 242, 240); border-radius: 1em; font-family: Consolas, Monaco, "Andale Mono", monospace; text-shadow: white 0px 1px; overflow: auto; font-size: 10px; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px;"><pre class=" language-http" style="margin-top: 0px; margin-bottom: 0px; padding: 0em 0em 0.5em; list-style-type: none; border: none; color: black; text-shadow: white 0px 1px; font-family: monospace, Consolas, Monaco, "Andale Mono"; direction: ltr; word-spacing: normal; font-size: 1.2em; tab-size: 4; hyphens: none; overflow: auto; background-color: rgb(245, 242, 240);"> HTTP/1.1 200 OK Content-Type: application/json;charset=UTF-8 Cache-Control: no-store Pragma: no-cache { "access_token":"2YotnFZFEjr1zCsicMWpAA", "token_type":"example", "expires_in":3600, "refresh_token":"tGzv3JOkF0XG5Qx2TlKWIA", "example_parameter":"example_value" }</pre></blockquote><p>上面代码中,各个参数的含义参见《授权码模式》一节。</p><p>整个过程中,客户端不得保存用户的密码。</p><h2 style="margin: 1.5em 0px 0.2em; padding: 0px 0px 0.2em; list-style-type: none; font-weight: 500; border-top: none; border-right: none; border-bottom: 1px solid rgb(211, 211, 211); border-left: none; border-image: initial; font-size: 2.88em; line-height: 1.35em; letter-spacing: -0.03em; font-family: Georgia, serif; white-space: normal; word-spacing: 2.4px; background-color: rgb(245, 245, 213);">九、客户端模式</h2><p>客户端模式(Client Credentials Grant)指客户端以自己的名义,而不是以用户的名义,向"服务提供商"进行认证。严格地说,客户端模式并不属于OAuth框架所要解决的问题。在这种模式中,用户直接向客户端注册,客户端以自己的名义要求"服务提供商"提供服务,其实不存在授权问题。</p><p><img src="/ueditor/php/upload/image/20200609/1591647717537170.png" alt="客户端模式" title="" style="margin: 0px; padding: 0px; list-style-type: none; border: 0.3em solid rgb(224, 223, 204); border-radius: 1em;"/></p><p>它的步骤如下:</p><blockquote style="margin: 2em; padding: 1em; list-style-type: none; border: 0.3em solid rgb(224, 223, 204); color: rgb(17, 17, 17); background-color: rgb(245, 242, 240); border-radius: 1em; font-family: Consolas, Monaco, "Andale Mono", monospace; text-shadow: white 0px 1px; overflow: auto; font-size: 10px; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px;"><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.8px;">(A)客户端向认证服务器进行身份认证,并要求一个访问令牌。</p><p style="margin-top: 1em; margin-bottom: 0px; margin-left: 0.8em; padding: 0px; list-style-type: none; border: none; font-size: 1.6em; line-height: 28.8px;">(B)认证服务器确认无误后,向客户端提供访问令牌。</p></blockquote><p>A步骤中,客户端发出的HTTP请求,包含以下参数:</p><ul style="list-style-type: none;" class=" list-paddingleft-2"><li><p>grant<span style="margin: 0px; padding: 0px; list-style-type: none; border: none;">type:表示授权类型,此处的值固定为"client</span>credentials",必选项。</p></li><li><p>scope:表示权限范围,可选项。</p></li></ul><blockquote style="margin: 2em; padding: 1em; list-style-type: none; border: 0.3em solid rgb(224, 223, 204); color: rgb(17, 17, 17); background-color: rgb(245, 242, 240); border-radius: 1em; font-family: Consolas, Monaco, "Andale Mono", monospace; text-shadow: white 0px 1px; overflow: auto; font-size: 10px; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px;"><pre class=" language-http" style="margin-top: 0px; margin-bottom: 0px; padding: 0em 0em 0.5em; list-style-type: none; border: none; color: black; text-shadow: white 0px 1px; font-family: monospace, Consolas, Monaco, "Andale Mono"; direction: ltr; word-spacing: normal; font-size: 1.2em; tab-size: 4; hyphens: none; overflow: auto; background-color: rgb(245, 242, 240);"> POST /token HTTP/1.1 Host: server.example.com Authorization: Basic czZCaGRSa3F0MzpnWDFmQmF0M2JW Content-Type: application/x-www-form-urlencoded grant_type=client_credentials</pre></blockquote><p>认证服务器必须以某种方式,验证客户端身份。</p><p>B步骤中,认证服务器向客户端发送访问令牌,下面是一个例子。</p><blockquote style="margin: 2em; padding: 1em; list-style-type: none; border: 0.3em solid rgb(224, 223, 204); color: rgb(17, 17, 17); background-color: rgb(245, 242, 240); border-radius: 1em; font-family: Consolas, Monaco, "Andale Mono", monospace; text-shadow: white 0px 1px; overflow: auto; font-size: 10px; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px;"><pre class=" language-http" style="margin-top: 0px; margin-bottom: 0px; padding: 0em 0em 0.5em; list-style-type: none; border: none; color: black; text-shadow: white 0px 1px; font-family: monospace, Consolas, Monaco, "Andale Mono"; direction: ltr; word-spacing: normal; font-size: 1.2em; tab-size: 4; hyphens: none; overflow: auto; background-color: rgb(245, 242, 240);"> HTTP/1.1 200 OK Content-Type: application/json;charset=UTF-8 Cache-Control: no-store Pragma: no-cache { "access_token":"2YotnFZFEjr1zCsicMWpAA", "token_type":"example", "expires_in":3600, "example_parameter":"example_value" }</pre></blockquote><p>上面代码中,各个参数的含义参见《授权码模式》一节。</p><h2 style="margin: 1.5em 0px 0.2em; padding: 0px 0px 0.2em; list-style-type: none; font-weight: 500; border-top: none; border-right: none; border-bottom: 1px solid rgb(211, 211, 211); border-left: none; border-image: initial; font-size: 2.88em; line-height: 1.35em; letter-spacing: -0.03em; font-family: Georgia, serif; white-space: normal; word-spacing: 2.4px; background-color: rgb(245, 245, 213);">十、更新令牌</h2><p>如果用户访问的时候,客户端的"访问令牌"已经过期,则需要使用"更新令牌"申请一个新的访问令牌。</p><p>客户端发出更新令牌的HTTP请求,包含以下参数:</p><ul style="list-style-type: none;" class=" list-paddingleft-2"><li><p>grant<span style="margin: 0px; padding: 0px; list-style-type: none; border: none;">type:表示使用的授权模式,此处的值固定为"refresh</span>token",必选项。</p></li><li><p>refresh_token:表示早前收到的更新令牌,必选项。</p></li><li><p>scope:表示申请的授权范围,不可以超出上一次申请的范围,如果省略该参数,则表示与上一次一致。</p></li></ul><p>下面是一个例子。</p><blockquote style="margin: 2em; padding: 1em; list-style-type: none; border: 0.3em solid rgb(224, 223, 204); color: rgb(17, 17, 17); background-color: rgb(245, 242, 240); border-radius: 1em; font-family: Consolas, Monaco, "Andale Mono", monospace; text-shadow: white 0px 1px; overflow: auto; font-size: 10px; letter-spacing: -0.12px; white-space: normal; word-spacing: 2.4px;"><pre class=" language-http" style="margin-top: 0px; margin-bottom: 0px; padding: 0em 0em 0.5em; list-style-type: none; border: none; color: black; text-shadow: white 0px 1px; font-family: monospace, Consolas, Monaco, "Andale Mono"; direction: ltr; word-spacing: normal; font-size: 1.2em; tab-size: 4; hyphens: none; overflow: auto; background-color: rgb(245, 242, 240);"> POST /token HTTP/1.1 Host: server.example.com Authorization: Basic czZCaGRSa3F0MzpnWDFmQmF0M2JW Content-Type: application/x-www-form-urlencoded grant_type=refresh_token&refresh_token=tGzv3JOkF0XG5Qx2TlKWIA</pre></blockquote><p>(完)</p><p><br/></p>
CopyRight 2002~2023 精通2100网 联系邮箱:qqtxt@163.com
版权所有:精通2100网
湘ICP备2023018646号-1
MYSQl共执行 4 个查询,用时 0.0015308856964111 秒,PHP脚本用时 0.005093 秒,占用内存 0.804 MB,Gzip 已启用