醉菜鸟 2021-12-24 09:00:01

laravel 安装使用 Passport (Api认证)

composer安装passport:

composer require laravel/passport

创建数据表

php artisan migrate

生成密钥

php artisan passport:install

用户模型中引用

<?php

namespace App\Models;


use Laravel\Passport\HasApiTokens;

class User extends Authenticatable
{
    use HasApiTokens;
}

配置config/auth/php

'guards' => [
    'api' => [
      'driver' => 'passport',
      'provider' => 'users'
    ]
]

'providers' => [
    'users' => [
    'driver' => 'eloquent',
    'model' => App\Models\User::class,
  ]
]

创建令牌

use App\Models\User;

$user = User::find(1);

// 创建没有作用域的令牌...
$token = $user->createToken('Token Name')->accessToken;

// 创建有作用域的令牌...
$token = $user->createToken('My Token', ['place-orders'])->accessToken;
意见反馈