代码拉取完成,页面将自动刷新
授权码是在所有请求中附带token,并交由认证服务器验证的一种权限认证模式。
在发送请求时需要在应用中判断是否携带token,以及token是否过期,如何没有token或者已经过期,需要跳转到认证服务器中登陆, 之后重新跳转到应用继续完成业务。
因为在不同应用中使用了同一个登陆服务,所以在第一次登陆之后,再次登陆会自动携带code返回到应用之中。 这样应用就可以通过code重新获取token,已实现单点登陆。
http://localhost:8080/oauth/authorize?response_type=code&client_id=client&state=xyz%20&redirect_uri=http%3A%2F%2Fwww.baidu.com
参数 | 是否允许空值 | 介绍 |
---|---|---|
response_type | 否 | 此处固定为code |
client_id | 否 | 认证服务器配置的client_id |
redirect_uri | 否 | 登陆成功后跳转的地址 |
state | 是 | 携带的参数,可以为空 |
正确时跳转页面:
https://www.baidu.com/?code=CwBT8F&state=xyz%20
参数 | 介绍 |
---|---|
code | 返回的code,可以用来获取token |
state | 登陆时附带的参数 |
curl -X POST \
http://localhost:8080/oauth/token \
-H 'Authorization: Basic Y2xpZW50OnNlY3JldA==' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-d 'grant_type=authorization_code&code=CwBT8F&redirect_uri=http%3A%2F%2Fwww.baidu.com'
参数 | 介绍 |
---|---|
Authorization | 按照client:secret的格式进行base64加密 |
grant_type | 此处固定为authorization_code |
code | 前一步获取的code |
redirect_uri | 前一步所传的url |
正确时返回的json:
{
"access_token": "3e2301a9-87ea-4b0d-9f37-847a6cfed4b2",
"token_type": "bearer",
"refresh_token": "a33d2862-4849-470c-9724-c23678a79bc6",
"expires_in": 40375,
"scope": "app"
}
参数 | 介绍 |
---|---|
access_token | 得到的token |
token_type | token类型 |
refresh_token | 刷新token所用的refresh_token |
expires_in | 过期时间,单位(秒) |
scope | 认证服务器设置的scope |
curl -X GET \
http://localhost:8080/user \
-H 'Authorization: bearer 3e2301a9-87ea-4b0d-9f37-847a6cfed4b2'
参数 | 介绍 |
---|---|
Authorization | 前一步得到的token_type,加上一个空格,再加上前一步得到的token |
正确时返回的json,可以根据自己的需求修改:
{
"authorities": [
{
"authority": "user"
}
],
"details": {
"remoteAddress": "0:0:0:0:0:0:0:1",
"sessionId": "D04DF1FF47A76BCFEB652ECC47A129D9"
},
"authenticated": true,
"principal": {
"id": "1",
"username": "user",
"enabled": true,
"roles": [
{
"id": "1",
"name": "user"
}
],
"authorities": [
{
"authority": "user"
}
],
"accountNonExpired": true,
"accountNonLocked": true,
"credentialsNonExpired": true
},
"credentials": null,
"name": "user"
}
curl -X POST \
http://localhost:8080/oauth/token \
-H 'Authorization: Basic Y2xpZW50OnNlY3JldA==' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-d 'grant_type=refresh_token&refresh_token=d218ad40-c6e9-4db6-9ffa-6ba6e57237ad'
参数 | 介绍 |
---|---|
Authorization | 按照client:secret的格式进行base64加密 |
grant_type | 此处固定为refresh_token |
refresh_token | 第二步获取的refresh_token |
正确时的返回值:
{
"access_token": "49a58fc2-bd31-4d9b-af1e-b14f8ed211ea",
"token_type": "bearer",
"refresh_token": "a33d2862-4849-470c-9724-c23678a79bc6",
"expires_in": 43199,
"scope": "app"
}
参数同第二步
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。