看到这里,又该掉头发了

passport安装好之后,到前端那里各种报错,顺着文档也不知道咋往下走了

《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
最佳答案

你要先搞懂OAuth 2.0 的四种授权方式

OAuth 2.0 的四种方式

一、授权码(授权码模式)

第三方应用先申请一个授权码,然后再用该码获取令牌。这种方式是最常用的流程,安全性也最高,它适用于那些有后端的 Web 应用。

  1. A 网站提供一个链接,用户点击后就会跳转到 B 网站,授权用户数据给 A 网站使用。
  2. 用户跳转后,B 网站会要求用户登录,然后询问是否同意给予 A 网站授权。用户表示同意,这时 B 网站就会跳回redirect_uri参数指定的网址。
  3. A 网站拿到授权码以后,就可以在后端,向 B 网站请求令牌。
  4. B 网站收到请求以后,就会颁发令牌。向redirect_uri指定的网址,发送一段 JSON 数据。

二、隐藏式(简化授权模式)

有些 Web 应用是纯前端应用,没有后端,必须将令牌储存在前端。这种方式没有授权码这个中间步骤,所以称为(授权码)"隐藏式"。

  1. A 网站提供一个链接,要求用户跳转到 B 网站,授权用户数据给 A 网站使用。
  2. 用户跳转到 B 网站,登录后同意给予 A 网站授权。这时,B 网站就会跳回redirect_uri参数指定的跳转网址,并且把令牌作为 URL 参数,传给 A 网站。

三、密码式(密码授权模式)

前后端分离的单页应用都可以采取这种授权模式。

如果你是开发前后端分离的应用的话,只看这个就行了

  1. A 网站要求用户提供 B 网站的用户名和密码。拿到以后,A 就直接向 B 请求令牌。
  2. B 网站验证身份通过后,直接给出令牌。

四、凭证式(客户端模式)

客户端以自己的名义,而不是以用户的名义,向"服务提供商"进行认证。适用于没有前端的命令行应用,即在命令行下请求令牌。

  1. A 应用在命令行向 B 发出请求。
  2. 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;
}

五、注册路由

AuthServiceProviderboot 方法中调用 Passport::routes()

六、修改配置

config/auth.php 中授权看守器 guardsapidriver 选项改为 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" => "",
}
3年前 评论
讨论数量: 1

你要先搞懂OAuth 2.0 的四种授权方式

OAuth 2.0 的四种方式

一、授权码(授权码模式)

第三方应用先申请一个授权码,然后再用该码获取令牌。这种方式是最常用的流程,安全性也最高,它适用于那些有后端的 Web 应用。

  1. A 网站提供一个链接,用户点击后就会跳转到 B 网站,授权用户数据给 A 网站使用。
  2. 用户跳转后,B 网站会要求用户登录,然后询问是否同意给予 A 网站授权。用户表示同意,这时 B 网站就会跳回redirect_uri参数指定的网址。
  3. A 网站拿到授权码以后,就可以在后端,向 B 网站请求令牌。
  4. B 网站收到请求以后,就会颁发令牌。向redirect_uri指定的网址,发送一段 JSON 数据。

二、隐藏式(简化授权模式)

有些 Web 应用是纯前端应用,没有后端,必须将令牌储存在前端。这种方式没有授权码这个中间步骤,所以称为(授权码)"隐藏式"。

  1. A 网站提供一个链接,要求用户跳转到 B 网站,授权用户数据给 A 网站使用。
  2. 用户跳转到 B 网站,登录后同意给予 A 网站授权。这时,B 网站就会跳回redirect_uri参数指定的跳转网址,并且把令牌作为 URL 参数,传给 A 网站。

三、密码式(密码授权模式)

前后端分离的单页应用都可以采取这种授权模式。

如果你是开发前后端分离的应用的话,只看这个就行了

  1. A 网站要求用户提供 B 网站的用户名和密码。拿到以后,A 就直接向 B 请求令牌。
  2. B 网站验证身份通过后,直接给出令牌。

四、凭证式(客户端模式)

客户端以自己的名义,而不是以用户的名义,向"服务提供商"进行认证。适用于没有前端的命令行应用,即在命令行下请求令牌。

  1. A 应用在命令行向 B 发出请求。
  2. 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;
}

五、注册路由

AuthServiceProviderboot 方法中调用 Passport::routes()

六、修改配置

config/auth.php 中授权看守器 guardsapidriver 选项改为 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" => "",
}
3年前 评论

你要先搞懂OAuth 2.0 的四种授权方式

OAuth 2.0 的四种方式

一、授权码(授权码模式)

第三方应用先申请一个授权码,然后再用该码获取令牌。这种方式是最常用的流程,安全性也最高,它适用于那些有后端的 Web 应用。

  1. A 网站提供一个链接,用户点击后就会跳转到 B 网站,授权用户数据给 A 网站使用。
  2. 用户跳转后,B 网站会要求用户登录,然后询问是否同意给予 A 网站授权。用户表示同意,这时 B 网站就会跳回redirect_uri参数指定的网址。
  3. A 网站拿到授权码以后,就可以在后端,向 B 网站请求令牌。
  4. B 网站收到请求以后,就会颁发令牌。向redirect_uri指定的网址,发送一段 JSON 数据。

二、隐藏式(简化授权模式)

有些 Web 应用是纯前端应用,没有后端,必须将令牌储存在前端。这种方式没有授权码这个中间步骤,所以称为(授权码)"隐藏式"。

  1. A 网站提供一个链接,要求用户跳转到 B 网站,授权用户数据给 A 网站使用。
  2. 用户跳转到 B 网站,登录后同意给予 A 网站授权。这时,B 网站就会跳回redirect_uri参数指定的跳转网址,并且把令牌作为 URL 参数,传给 A 网站。

三、密码式(密码授权模式)

前后端分离的单页应用都可以采取这种授权模式。

如果你是开发前后端分离的应用的话,只看这个就行了

  1. A 网站要求用户提供 B 网站的用户名和密码。拿到以后,A 就直接向 B 请求令牌。
  2. B 网站验证身份通过后,直接给出令牌。

四、凭证式(客户端模式)

客户端以自己的名义,而不是以用户的名义,向"服务提供商"进行认证。适用于没有前端的命令行应用,即在命令行下请求令牌。

  1. A 应用在命令行向 B 发出请求。
  2. 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;
}

五、注册路由

AuthServiceProviderboot 方法中调用 Passport::routes()

六、修改配置

config/auth.php 中授权看守器 guardsapidriver 选项改为 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" => "",
}
3年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!