你要先搞懂OAuth 2.0 的四种授权方式
OAuth 2.0 的四种方式
一、授权码(授权码模式)
第三方应用先申请一个授权码,然后再用该码获取令牌。这种方式是最常用的流程,安全性也最高,它适用于那些有后端的 Web 应用。
- A 网站提供一个链接,用户点击后就会跳转到 B 网站,授权用户数据给 A 网站使用。
- 用户跳转后,B 网站会要求用户登录,然后询问是否同意给予 A 网站授权。用户表示同意,这时 B 网站就会跳回redirect_uri参数指定的网址。
- A 网站拿到授权码以后,就可以在后端,向 B 网站请求令牌。
- B 网站收到请求以后,就会颁发令牌。向redirect_uri指定的网址,发送一段 JSON 数据。
二、隐藏式(简化授权模式)
有些 Web 应用是纯前端应用,没有后端,必须将令牌储存在前端。这种方式没有授权码这个中间步骤,所以称为(授权码)"隐藏式"。
- A 网站提供一个链接,要求用户跳转到 B 网站,授权用户数据给 A 网站使用。
- 用户跳转到 B 网站,登录后同意给予 A 网站授权。这时,B 网站就会跳回redirect_uri参数指定的跳转网址,并且把令牌作为 URL 参数,传给 A 网站。
三、密码式(密码授权模式)
前后端分离的单页应用都可以采取这种授权模式。
如果你是开发前后端分离的应用的话,只看这个就行了
- A 网站要求用户提供 B 网站的用户名和密码。拿到以后,A 就直接向 B 请求令牌。
- B 网站验证身份通过后,直接给出令牌。
四、凭证式(客户端模式)
客户端以自己的名义,而不是以用户的名义,向"服务提供商"进行认证。适用于没有前端的命令行应用,即在命令行下请求令牌。
- A 应用在命令行向 B 发出请求。
- B 网站验证通过以后,直接返回令牌。
更新令牌
B 网站颁发令牌的时候,一次性颁发两个令牌,一个用于获取数据,另一个用于获取新的令牌(refresh token 字段)。令牌到期前,用户使用 refresh token 发一个请求,去更新令牌。
Laravel Passport OAuth 认证
一、安装
composer require laravel/passport
二、数据库迁移
php artisan migrate
三、创建密钥
php artisan passport:keys
四、模型中添加HasApiTokens
use Laravel\Passport\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens;
}
五、注册路由
AuthServiceProvider
的 boot
方法中调用 Passport::routes()
六、修改配置
config/auth.php
中授权看守器 guards
的 api
的 driver
选项改为 passport
七、获取令牌
前后端分离使用密码授权令牌
- client_id: 客户端id,在
oauth_clients
表中 - client_secret: 客户端秘钥,在
oauth_clients
表中
$http = new GuzzleHttp\Client;
$response = $http->post('http://your-app.com/oauth/token', [
'form_params' => [
'grant_type' => 'password',
'client_id' => 'client-id',
'client_secret' => 'client-secret',
'username' => 'taylor@laravel.com',
'password' => 'my-password',
'scope' => '',
],
]);
return json_decode((string) $response->getBody(), true);
或者直接请求http://your-app.com/oauth/token
,传递的json
如下
{
"grant_type": "password",
"client_id": "client-id",
"client_secret": "client-secret",
"username": "taylor@laravel.com",
"password": "my-password",
"scope": ",
}
八、刷新令牌
- client_id: 同上
- client_secret: 同上
- refresh_token: 上面接口返回的
refresh_token
$http = new GuzzleHttp\Client;
$response = $http->post('http://your-app.com/oauth/token', [
'form_params' => [
'grant_type' => 'refresh_token',
'refresh_token' => 'the-refresh-token',
'client_id' => 'client-id',
'client_secret' => 'client-secret',
'scope' => '',
],
]);
return json_decode((string) $response->getBody(), true);
或者直接请求http://your-app.com/oauth/token
,传递的json
如下
{
"grant_type" => "refresh_token",
"refresh_token" => "the-refresh-token",
"client_id" => "client-id",
"client_secret" => "client-secret",
"scope" => "",
}
你要先搞懂OAuth 2.0 的四种授权方式
OAuth 2.0 的四种方式
一、授权码(授权码模式)
第三方应用先申请一个授权码,然后再用该码获取令牌。这种方式是最常用的流程,安全性也最高,它适用于那些有后端的 Web 应用。
- A 网站提供一个链接,用户点击后就会跳转到 B 网站,授权用户数据给 A 网站使用。
- 用户跳转后,B 网站会要求用户登录,然后询问是否同意给予 A 网站授权。用户表示同意,这时 B 网站就会跳回redirect_uri参数指定的网址。
- A 网站拿到授权码以后,就可以在后端,向 B 网站请求令牌。
- B 网站收到请求以后,就会颁发令牌。向redirect_uri指定的网址,发送一段 JSON 数据。
二、隐藏式(简化授权模式)
有些 Web 应用是纯前端应用,没有后端,必须将令牌储存在前端。这种方式没有授权码这个中间步骤,所以称为(授权码)"隐藏式"。
- A 网站提供一个链接,要求用户跳转到 B 网站,授权用户数据给 A 网站使用。
- 用户跳转到 B 网站,登录后同意给予 A 网站授权。这时,B 网站就会跳回redirect_uri参数指定的跳转网址,并且把令牌作为 URL 参数,传给 A 网站。
三、密码式(密码授权模式)
前后端分离的单页应用都可以采取这种授权模式。
如果你是开发前后端分离的应用的话,只看这个就行了
- A 网站要求用户提供 B 网站的用户名和密码。拿到以后,A 就直接向 B 请求令牌。
- B 网站验证身份通过后,直接给出令牌。
四、凭证式(客户端模式)
客户端以自己的名义,而不是以用户的名义,向"服务提供商"进行认证。适用于没有前端的命令行应用,即在命令行下请求令牌。
- A 应用在命令行向 B 发出请求。
- B 网站验证通过以后,直接返回令牌。
更新令牌
B 网站颁发令牌的时候,一次性颁发两个令牌,一个用于获取数据,另一个用于获取新的令牌(refresh token 字段)。令牌到期前,用户使用 refresh token 发一个请求,去更新令牌。
Laravel Passport OAuth 认证
一、安装
composer require laravel/passport
二、数据库迁移
php artisan migrate
三、创建密钥
php artisan passport:keys
四、模型中添加HasApiTokens
use Laravel\Passport\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens;
}
五、注册路由
AuthServiceProvider
的 boot
方法中调用 Passport::routes()
六、修改配置
config/auth.php
中授权看守器 guards
的 api
的 driver
选项改为 passport
七、获取令牌
前后端分离使用密码授权令牌
- client_id: 客户端id,在
oauth_clients
表中 - client_secret: 客户端秘钥,在
oauth_clients
表中
$http = new GuzzleHttp\Client;
$response = $http->post('http://your-app.com/oauth/token', [
'form_params' => [
'grant_type' => 'password',
'client_id' => 'client-id',
'client_secret' => 'client-secret',
'username' => 'taylor@laravel.com',
'password' => 'my-password',
'scope' => '',
],
]);
return json_decode((string) $response->getBody(), true);
或者直接请求http://your-app.com/oauth/token
,传递的json
如下
{
"grant_type": "password",
"client_id": "client-id",
"client_secret": "client-secret",
"username": "taylor@laravel.com",
"password": "my-password",
"scope": ",
}
八、刷新令牌
- client_id: 同上
- client_secret: 同上
- refresh_token: 上面接口返回的
refresh_token
$http = new GuzzleHttp\Client;
$response = $http->post('http://your-app.com/oauth/token', [
'form_params' => [
'grant_type' => 'refresh_token',
'refresh_token' => 'the-refresh-token',
'client_id' => 'client-id',
'client_secret' => 'client-secret',
'scope' => '',
],
]);
return json_decode((string) $response->getBody(), true);
或者直接请求http://your-app.com/oauth/token
,传递的json
如下
{
"grant_type" => "refresh_token",
"refresh_token" => "the-refresh-token",
"client_id" => "client-id",
"client_secret" => "client-secret",
"scope" => "",
}
你要先搞懂OAuth 2.0 的四种授权方式
OAuth 2.0 的四种方式
一、授权码(授权码模式)
二、隐藏式(简化授权模式)
三、密码式(密码授权模式)
如果你是开发前后端分离的应用的话,只看这个就行了
四、凭证式(客户端模式)
更新令牌
Laravel Passport OAuth 认证
一、安装
二、数据库迁移
三、创建密钥
四、模型中添加
HasApiTokens
五、注册路由
AuthServiceProvider
的boot
方法中调用Passport::routes()
六、修改配置
config/auth.php
中授权看守器guards
的api
的driver
选项改为passport
七、获取令牌
oauth_clients
表中oauth_clients
表中或者直接请求
http://your-app.com/oauth/token
,传递的json
如下八、刷新令牌
refresh_token
或者直接请求
http://your-app.com/oauth/token
,传递的json
如下