我想在Laravel护照中使用UUID,而不是在我的Laravel项目中使用默认ID。
我在迁移中将所有user_id
和client_id
列更改为uuid,并在AppServiceProvider
的注册方法中添加了Passport::ignoreMigrations()
。
当我通过在用户表中创建一个新的原始数据在邮递员中进行测试时,我在响应中获得了令牌,但是当我在安全路由中使用此令牌时,我总是得到响应状态:
401 Unauthorized and in the response body message:Unauthenticated.
我为纠正这个问题做了什么:
- 在
AppServiceProvider
和register
函数中添加:
Passport::ignoreMigrations()
- 运行
php artisan vendor:publish --tag=passport-migrations
- 在
CreateOauthAuthCodesTable
更改为
$table->uuid('id');
$table->primary('id');
$table->uuid('user_id')->index();
等等其他护照表和移民
- 在应用程序服务提供程序和引导方法中添加以下代码:
use LaravelPassportClient;
use RamseyUuidUuid;
Client::creating(function (Client $client) {
$client->incrementing = false;
$client->id = RamseyUuidUuid::uuid4()->toString();
});
Client::retrieved(function (Client $client) {
$client->incrementing = false;
});
- 然后我用
php artisan migrate:fresh
迁移我的表(警报:此命令将删除数据库的所有当前表) - 关键说明是不要忘记添加
protected $primaryKey = 'Id'; // in my case I capitalize id to Id
你在这儿!
oauth_access_tokens
中的user_id
更改为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();
});
}
php artisan passport:install --uuids