zz:https://blog.csdn.net/daihuimaozideren/article/details/77508642
OAuth2.0有五种授权模式。
(1)授权码模式(Authorization Code)
(2)授权码简化模式(Implicit)
(3)Pwd模式(Resource Owner Password Credentials)
(4)Client模式(Client Credentials)
(5)扩展模式(Extension)
注:文中的client,可理解为浏览器或APP。
但不论哪种模式,都是为了从认证服务器获取Access Token,用来访问资源服务器。
而申请Access Token,需要提交相应信息。例如,client_ID(我是谁),response_type或grant_typt(申请哪种模式),scope(申请哪些权限,由授权服务器定义),redirect_uri(申请结果跳转至哪儿)等。当然不同的模式,提交信息内容也不同。
我们先从简单的模式开始。因为简单模式,申请流程短,安全级别较低,个人感觉可用场景不多。
(一)Client模式
先上流程图。
这里写图片描述
该模式下,并不存在对个体用户授权的行为,被授权的主体为client。因此,该模式可用于对某类用户进行集体授权。
申请该模式时,需要在HTTP request entity-body中提交以下信息。
grant_type: REQUIRED. Value MUST be set to "client_credentials". scope: OPTIONAL. The scope of the access request
当然,可以根据授权服务器的实现,提交其它必要信息。
若申请成功,服务器将返回access token和token有效时间。
附规范中的例子。
Request:
POST /token HTTP/1.1 Host: server.example.com Authorization: Basic czZCaGRSa3F0MzpnWDFmQmF0M2JW Content-Type: application/x-www-form-urlencoded grant_type=client_credentials
Response:
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" }
注:expires_in为token有效时长,单位为秒。
(二)Pwd模式
这里写图片描述
该模式下,需要用户将自身的account ID和password交由client,client将使用它们来申请access token,整个过程会将用户信息暴露。因此,除非client十分可靠(例如硬件设备,系统APP),否则,不建议使用该模式。
申请该模式时,需要在HTTP request entity-body中提交以下信息。
grant_type: REQUIRED. Value MUST be set to "password". username: REQUIRED. The resource owner username. password: REQUIRED. The resource owner password. scope: OPTIONAL. The scope of the access request
申请成功后,授权服务器将返回access token和token有效时间,以及可选的refresh token,用于在access token过期时进行token更新。
附规范中的例子。
Request:
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
Response:
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" }
(三)授权码模式
这里写图片描述
该模式是五种授权中最啰嗦的。从流程上看,申请分为两个阶段。首先,需要申请Authorization Code,之后,使用Authorization Code来申请Access Token。
我们以优酷为例,讲述流程。
(A)优酷向用户展示,“我”可以支持QQ、微信、支付宝等第三方式登录。用户选择其中一种,例如QQ,则跳转至QQ界面(User-Agent),通常为WEB界面。此时,若用户未登录,则要求用户登录,若已登录,则询问是否授权,以及展示授权后会获得哪些权限。
(B)用户点击授权,触发申请。
(C)假设授权通过,QQ认证服务器将用户导向优酷事先指定的”重定向URI”(redirection URI),同时附上一个Authorization Code。
(D)优酷收到授权码,附上早先的”重定向URI”,向认证服务器申请Access Token。这一步是在优酷的后台的服务器上完成的,对用户不可见。
(E)认证服务器核对了授权码和重定向URI,确认无误后,向优酷发送访问令牌(access token)和更新令牌(refresh token)。
Authorization Code只能使用一次,且有时间限制,规范建议为10分钟。
申请Authorization Code时,需要在URI的query component中附加以下信息
response_type: REQUIRED. Value MUST be set to "code". client_id: REQUIRED. The client identifier redirect_uri: OPTIONAL. scope: OPTIONAL. The scope of the access request state: RECOMMENDED. An opaque value used by the client to maintain state between the request and callback. The redirecting the user-agent back to the client. The parameter SHOULD be used for preventing cross-site request forgery.
Authorization Code返回时,URI的query component中的附加信息如下
code: REQUIRED. The authorization code generated by the authorization server. The authorization code MUST of expire shortly after it is issued to mitigate the risk of leaks. A maximum authorization code lifetime of 10 minutes is RECOMMENDED. The client MUST NOT use the authorization code more than once. If an authorization code is used more than once, the authorization server MUST deny the request and SHOULD revoke (when possible) all tokens previously issued based on that authorization code. The authorization code is bound to the client identifier and redirection URI. state: REQUIRED if the "state" parameter was present in the client authorization request. The exact value received from the client.
使用Authorization Code申请Access Token时,需要在HTTP request entity-body中提交以下信息。
grant_type: REQUIRED. Value MUST be set to "authorization_code". code: REQUIRED. The authorization code received from the authorization server. redirect_uri: REQUIRED, if the "redirect_uri" parameter was included in the authorization request, and their values MUST be identical. client_id: REQUIRED, if the client is not authenticating with the authorization server.
申请成功后,授权服务器将返回access token和token有效时间,以及可选的refresh token。
附规范中的例子。
Request Authorization Code:
GET /authorize?response_type=code&client_id=s6BhdRkqt3&state=xyz&redirect_uri=https%3A%2F%2Fclient%2Eexample%2Ecom%2Fcb HTTP/1.1 Host: server.example.com
Authorization Code Response:
HTTP/1.1 302 Found Location: https://client.example.com/cb?code=SplxlOBeZQQYbYS6WxSbIA&state=xyz
Request Access Token:
POST /token HTTP/1.1 Host: server.example.com Authorization: Basic czZCaGRSa3F0MzpnWDFmQmF0M2JW Content-Type: application/x-www-form-urlencoded grant_type=authorization_code&code=SplxlOBeZQQYbYS6WxSbIA &redirect_uri=https%3A%2F%2Fclient%2Eexample%2Ecom%2Fcb
Access Token Response:
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" }
(四)授权码简化模式
这里写图片描述
在授权码模式中,Authorization Code和Access Token都由授权服务器生成和验证,而最终只用到Access Token,这让Authorization Code显得无足轻重。因此,授权码简化模式,去掉了Authorization Code的申请流程,从而通过User-Agent(Browser)直接申请Access Token。
我们还以优酷为例,讲述流程。
(A)优酷向用户展示,“我”可以支持QQ、微信、支付宝等第三方式登录。用户选择其中一种,例如QQ,则跳转至QQ界面(User-Agent),通常为WEB界面。此时,若用户未登录,则要求用户登录,若已登录,则询问是否授权,以及展示授权后会获得哪些权限。
(B)用户点击授权,触发申请。
(C)假设授权通过,QQ认证服务器将用户导向优酷事先指定的”重定向URI”(redirection URI),同时附上Access Token。
(D)QQ界面(User-Agent)收到重定向响应后,向优酷服务器提出请求,表示想提取URI中的Access Token。
(E)优酷服务器返回带有解析脚本的页面,用于解析重定向URI fragment中的Access Token。
(F)User-Agent使用解析脚本,获取Access Token。
(G)User-Agent将Access Token转交给优酷。
申请Access Token时,需要在URI的query component中附加以下信息
response_type: REQUIRED. Value MUST be set to "token". client_id: REQUIRED. The client identifier. redirect_uri: OPTIONAL. scope: OPTIONAL. The scope of the access request. state: RECOMMENDED. An opaque value used by the client to maintain state between the request and callback. The authorization server includes this value when redirecting the user-agent back to the client. The parameter SHOULD be used for preventing cross-site request forgery.
Access Token返回时,URI的query component中的附加信息如下
access_token: REQUIRED. The access token issued by the authorization server. token_type: REQUIRED. The type of the token issued. Value is case insensitive. expires_in: RECOMMENDED. The lifetime in seconds of the access token. For example, the value "3600" denotes that the access token will expire in one hour from the time the response was generated. If omitted, the authorization server SHOULD provide the expiration time via other means or document the default value. scope: OPTIONAL, if identical to the scope requested by the client; otherwise, REQUIRED. The scope of the access token. state: REQUIRED if the "state" parameter was present in the client authorization request. The exact value received from the client.
注:授权码简化模式,不生成refresh token。
附规范例子。
Access Token Request:
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
Access Token Response:
HTTP/1.1 302 Found Location: http://example.com/cb#access_token=2YotnFZFEjr1zCsicMWpAA &state=xyz&token_type=example&expires_in=3600
注:此处存在质疑。规范例子中URI不一致,个人认为Response中的URI应为:https://client.example.com/cb。若同学有其他见解,望赐教。
(五)扩展模式
扩展模式,其实是一种自定义模式。规范中仅对“grant type”参数提出了须为URI的要求。对于其他申请数据,可以根据需求进行自定义。
附规范例子。
POST /token HTTP/1.1 Host: server.example.com Content-Type: application/x-www-form-urlencoded grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Asaml2- bearer&assertion=PEFzc2VydGlvbiBJc3N1ZUluc3RhbnQ9IjIwMTEtMDU [...omitted for brevity...]aG5TdGF0ZW1lbnQ-PC9Bc3NlcnRpb24-
相关推荐
OAuth2.0授权码模式.doc OAuth2.0授权码模式的文档,希望可以帮助学习者
通过点击viewbutton获取用户openid,实现方式oauth2.0认证
基于Django2.1.2的OAuth2.0授权登录 大学生课程设计 基于Django2.1.2的OAuth2.0授权登录的课程设计 自己大二写的课程设计
在服务端,基于 Owin OAuth, 针对 Resource Owner Password Credentials Grant 的授权方式,只需重载 OAuthAuthorizationServerProvider.GrantResourceOwnerCredentials() 方法即可
开放平台标准协议简介之RFC 6749-OAuth 2.0授权框架; The OAuth 2.0 authorization framework enables a third-party application to obtain limited access to an HTTP service, either on behalf of a resource ...
RFC 6749 OAuth 2.0 授权框架 (正式版中文翻译,PDF)
springboot 集成oauth2.0服务器,基于oauth2.0授权码形式集成
asp.net WebAPI OWIN OAuth2.0授权自定义返回结果及错误或异常问题处理核心代码,详情: https://www.cnblogs.com/wgx0428/p/12315546.html
OAuth2.0是OAuth协议的延续版本,但不向前兼容OAuth 1.0(即完全废止了OAuth1.0)。 OAuth 2.0关注客户端开发者的简易性。要么通过组织在资源拥有者和HTTP服务商之间的被批准的交互动作代表用户,要么允许第三方应用...
实现OAuth2授权,并且实现单点登录的小例子,请使用vs2015打开
Java,腾讯微博通过OAuth2.0授权,可发文本微博的示例源代码。
完整Oauth 2.0 代码实现,包含数据库脚本,使用说明,导入数据库脚本,修改数据库配置可直接运行。
基于Owin中间件的OAuth2.0身份认证,文章位置https://blog.csdn.net/u013938578/article/details/82956188
Oauth2.0 密码模式单点登录客户端及服务端实现,导入MAVEN工程,导入Client1-root ,包含7个子工程,client1-web,client2-web是客户端项目,sessmgr是服务端,运行这3个项目 ,可测试单点登录功能
换句话说,如果你的项目使用OWIN实现OAuth 2.0 之客户端模式,但是没有配置machineKey,也没有在认证服务中自定义认证。那么我就可以利用OWIN实现OAuth 2.0 之客户端模式的基本原理生成一个token,你会发现我这个...
Spring Security OAuth2.0学习笔记 ...OAuth2.0认证的四种模式?它们的大体流程是什么? Spring cloud Security OAuth2包括哪些组件?职责? 分布式系统认证需要解决的问题? 掌握学习方法,掌握思考方式。
该套代码主要用于开发微信公众账号,定制菜单和消息自动回复。这两个功能可以参考imooc里的两个微信开发教程来学习。该代码在这基础上集成了OAuth2.0网页授权。
Spring boot+Spring Security Oauth2.0,Sprint cloud+Spring Security Oauth2集成。四种认证方式。附带有代码,和案例,案例,还有视频链接。我保证看完就回,如果视频链接失效,评论回复我,我单独再给你一份。