授权码模式(authorization code)获取access_token

1.

oauth2获取access_token的几种方式:

简化模式(implicit):在redirect_url中传递access_token,oauth客户端运行在浏览器中。

密码模式(password):将用户名和密码传过去,直接获取access_token。

客户端模式(client credentials):用户向客户端注册,然后客户端以自己的名义向“服务端”获取资源。

授权码模式(authorization code):通过客户端的后台服务器,向服务端认证。

2.

授权码模式(authorization code)过程

在浏览器中访问OAuth2 服务器的认证接口:

http://localhost:xxxx/oauth/authorize?response_type=code&client_id=test&redirect_uri=http://localhost:xxxx

response_type=code : 代表期望的请求响应类型为authorization code

client_id=test: client_id为你需要使用的客户端id

redirect_uri=http://localhost:xxxx : redirect_uri是成功获取token之后,重定向的地址

访问认证接口成功之后,浏览器会跳转到OAuth2配置的登录页或者默认的security登录,正确输入用户名/密码之后。浏览器将会在重定向的地址上返回一个code。如下:

http://localhost:xxxx?code=xxxxxx

code=xxxxx : code就是OAuth2服务器返回的

然后使用获取到的code范围OAuth2认证服务器取到access_token,如下:

http://localhost:xxxx/oauth/token?grant_type=authorization_code&code=W3ixVa&client_id=test&client_secret=secret&redirect_uri=http://localhost:8080

grant_type=authorization_code : grant_type为认证类型,当前为授权码模式

code=xxxx : code为上面获取到的code

client_id=test : client_id 与上面获取code的client_id需要一致

client_secret=secret : 为client_id对应的客户端的密钥

redirect_uri=http://localhost:xxxx : : redirect_uri是成功获取token之后,重定向的地址

您可能还会对下面的文章感兴趣: