see: github
see: OAuth2 & OpenID Connect概览
see: OAuth2/OpenID Connect Grant_type & 应用场景
see: OAuth2.1示例
see: OIDC示例
- Authorization Code 授权码模式
- Client Credentials 客户端模式
- Implicit 简化模式
- Password 密码模式
- Refresh Token
1 授权码模式 Authorization Code
步骤:获取授权码——>通过授权码获取token
1.1 获取授权码
1.1.1 Request
GET http://localhost:8080/oauth/authorize?response_type=code&client_id=clientId&scope=testScope&redirect_uri=http://localhost:8080/oauth2callback/getTokenByCode
response_type=code
必须,固定
client_id
必须
redirect_uri
必须
scope
可选,空格分隔的字符串,指示应用程序请求的权限
state
可选,应用程序生成一个随机字符串并将其包含在请求中。然后应该检查在用户授权应用程序之后是否返回相同的值。用于防止CSRF攻击
1.1.2 Response
返回url格式:
redirect_url?code=xxx
如
http://localhost:8080/oauth2callback/getTokenByCode?code=VU6HZb
1.2 获取token
1.2.1 Request
POST http://localhost:8080/oauth/token?grant_type=authorization_code&client_id=clientId&client_secret=clientSecret&code=xxx&redirect_uri=http://localhost:8080/oauth2callback/getTokenByCode
grant_type=authorization_code
必须,固定
client_id
必须
client_secret
必须
code
必须,授权码
redirect_uri
必须,与获取授权码
的redirect_uri一致
1.2.2 Response
根据oauth2不同实现和配置,返回不同格式。如
{
"access_token":"K1e1QIjDkbP07xdlvrRTMMJkGqE",
"token_type":"bearer",
"refresh_token":"zFKlhrDGtfBRP44QYFhPP-NzM14",
"expires_in":43199,
"scope":"testScope"
}
2 简化模式 Implicit
风险:泄漏风险大,原因在于access_token直接暴露在返回的url当中
2.1 Request
GET http://localhost:8080/oauth/authorize?response_type=code&client_id=clientId&scope=testScope&redirect_uri=http://localhost:8080/oauth2callback/getTokenByCode
response_type=token
必须,固定client_id
必须redirect_uri
必须scope
可选,空格分隔的字符串,指示应用程序请求的权限state
可选,应用程序生成一个随机字符串并将其包含在请求中。然后应该检查在用户授权应用程序之后是否返回相同的值。用于防止CSRF攻击
2.2 Response
返回url格式:
redirect_url#access_token=xxx&token_type=xxx&expires_in=xxx
如
https://www.baidu.com/#access_token=YXQfAirQYE46ntbum0_rZshEbIY&token_type=bearer&expires_in=43199
3 客户端模式 Client Credentials
3.1 Request
POST http://localhost:8080/oauth/token?grant_type=client_credentials&client_id=clientId&client_secret=clientSecret
grant_type=client_credentials
必须,固定
client_id
必须
client_secret
必须
scope
可选
3.2 Response
根据oauth2不同实现和配置,返回不同格式。如
{
"access_token":"2qp1Z16T-2zZQyzFFhI6w5sVB8c",
"token_type":"bearer",
"expires_in":43199,
"scope":"testScope"
}
4 密码模式 Password
4.1 Request
POST http://localhost:8080/oauth/token?grant_type=password&client_id=clientId&client_secret=clientSecret&username=user&password=password
grant_type=password
必须,固定
client_id
必须
client_secret
必须
username
必须,用户名
password
必须,密码
scope
可选
4.2 Response
根据oauth2不同实现和配置,返回不同格式。如
{
"access_token":"K1e1QIjDkbP07xdlvrRTMMJkGqE",
"token_type":"bearer",
"refresh_token":"zFKlhrDGtfBRP44QYFhPP-NzM14",
"expires_in":42918,
"scope":"testScope"
}
5 刷新token
5.1 Request
POST http://localhost:8080/oauth/token?grant_type=refresh_token&client_id=clientId&client_secret=clientSecret&refresh_token=zFKlhrDGtfBRP44QYFhPP-NzM14
grant_type=refresh_token
必须,固定
client_id
必须
client_secret
必须
refresh_token
必须
scope
可选
5.2 Response
与获取token返回的格式相同。如
{
"access_token":"TH3U2Rl6RLxNZJLE3_pSneSDsL8",
"token_type":"bearer",
"refresh_token":"43E1UZWXyPPJf8e2rBz5DRKk12E",
"expires_in":43199,
"scope":"testScope"
}