Laravel护照的UUID问题

时间:2018-12-24 09:46:23

标签: laravel rest laravel-5 laravel-passport

我想在Laravel护照中使用UUID,而不是在laravel项目中使用默认ID。

在迁移过程中,我将所有user_idclient_id列都更改为uuid,并在Passport::ignoreMigrations()的register方法中添加了AppServiceProvider

当我通过在用户表中创建一个新的raw原始邮件在邮递员中进行测试时,我会收到响应令牌,但是当我在安全的路由中使用此令牌时,我始终会得到响应状态:

  

401未经授权,并且在响应正文消息中:未经验证。

有人可以帮忙吗,我被完全封锁了

2 个答案:

答案 0 :(得分:1)

我为纠正这个问题所做的事情:

  1. AppServiceProviderregister 函数中添加:
Passport::ignoreMigrations()
  1. 运行php artisan vendor:publish --tag=passport-migrations
  2. CreateOauthAuthCodesTable 处更改为
$table->uuid('id');
$table->primary('id');
$table->uuid('user_id')->index();
<块引用>

其他护照表和迁移等

  1. 在 appServiceProvider 和 boot 方法中添加以下代码:
use Laravel\Passport\Client;
use Ramsey\Uuid\Uuid;

Client::creating(function (Client $client) {
    $client->incrementing = false;
    $client->id = \Ramsey\Uuid\Uuid::uuid4()->toString();
});

Client::retrieved(function (Client $client) {
    $client->incrementing = false;
});
  1. 然后我用 php artisan migrate:fresh
  2. 迁移我的表
  3. 重点是不要忘记添加
protected $primaryKey = 'Id'; // in my case I capitalize id to Id

你来了!

答案 1 :(得分:0)

您必须将 user_id 中的 oauth_access_tokens 更改为 uuid 类型,请按照以下步骤操作:

创建一个迁移来改变表oauth_access_tokens

php artisan make:migration alter_table_oauth_access_tokens

public function up()
{
    Schema::table('oauth_access_tokens', function (Blueprint $table) {
        $table->dropColumn('user_id');
    });
    Schema::table('oauth_access_tokens', function (Blueprint $table) {
        $table->uuid('user_id')->index()->nullable();
    });
}