grant_type参数说明表格:html
grant_type前端 |
说明git |
authorization_codegithub |
标准的Server受权模式spring |
passwordjson |
基于用户密码的受权模式浏览器 |
client_credentialsapp |
基于APP密钥的受权模式框架 |
refresh_tokenide |
刷新accessToken |
response_type参数说明表格:
response_type |
说明 |
code |
标准的Server受权模式响应模式 |
token |
脚本的受权响应模式,直接返回token,须要对回调进行校验 |
标准的的Server受权模式,与目前开放平台的Session机制很像。第一步获取code,第二步code换token。
第一步:APP首先发送获取code请求
GET /authorize?response_type=code&client_id=s6BhdRkqt3&
redirect_uri=https%3A%2F%2Fclient%2Eexample%2Ecom%2Fcb HTTP/1.1
Host: server.example.com
容器返回code
HTTP/1.1 302 Found
Location: https://client.example.com/cb?code=i1WsRn1uB1
第二步:APP根据code发送获取token请求
POST /token HTTP/1.1
Host: server.example.com
Content-Type: application/x-www-form-urlencoded
grant_type=authorization_code&client_id=s6BhdRkqt3&
client_secret=gX1fBat3bV&code=i1WsRn1uB1&
redirect_uri=https%3A%2F%2Fclient%2Eexample%2Ecom%2Fcb
容器直接返回token
HTTP/1.1 200 OK
Content-Type: application/json
Cache-Control: no-store
{
"access_token":"SlAV32hkKG",
"token_type":"example",
"expires_in":3600,
"refresh_token":"8xLOxBtZp8",
"example_parameter":"example-value"
}
标准oauth2流程图
适用于运行于浏览器中的脚本应用,须要校验callback地址,并且只返回该应用注册的回调地址
APP直接请求token
GET /authorize?response_type=token&client_id=s6BhdRkqt3&
redirect_uri=https%3A%2F%2Fclient%2Eexample%2Ecom%2Fcb HTTP/1.1
Host: server.example.com
容器经过重定向返回token
HTTP/1.1 302 Found
Location: http://example.com/rd#access_token=FJQbwq9&
token_type=example&expires_in=3600
称之为用户名密码模式,须要提供终端用户的用户名和密码,适用于好比操做系统或者高权限的应用。
APP直接带上用户名和密码请求
POST /token HTTP/1.1
Host: server.example.com
Content-Type: application/x-www-form-urlencoded
grant_type=password&client_id=s6BhdRkqt3&
client_secret=47HDu8s&username=johndoe&password=A3ddj3w
容器直接返回token
HTTP/1.1 200 OK
Content-Type: application/json
Cache-Control: no-store
{
"access_token":"SlAV32hkKG",
"token_type":"example",
"expires_in":3600,
"refresh_token":"8xLOxBtZp8",
"example_parameter":"example-value"
}
基于APP的密钥直接进行受权,APP的权限很是大,慎用。这个模式能够考虑用于目前咱们不须要弹出受权的特殊应用,如淘江湖,前端插件等。
APP直接根据客户端的密码来请求
POST /token HTTP/1.1
Host: server.example.com
Content-Type: application/x-www-form-urlencoded
grant_type=client_credentials&client_id=s6BhdRkqt3&
client_secret=47HDu8s
容器直接返回token
HTTP/1.1 200 OK
Content-Type: application/json
Cache-Control: no-store
{
"access_token":"SlAV32hkKG",
"token_type":"example",
"expires_in":3600,
"refresh_token":"8xLOxBtZp8",
"example_parameter":"example-value"
}
代码地址1:https://github.com/favccxx/FavOAuth2 【oauth2-server】
代码地址2:http://git.oschina.net/mkk/oauth2-shiro 【oauth2-server】
代码地址3:http://git.oschina.net/mkk/spring-oauth-client/ 【oauth2-client】