Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何向Laravel Passport API令牌添加自定义数据?_Php_Laravel_Jwt_Laravel Passport - Fatal编程技术网

Php 如何向Laravel Passport API令牌添加自定义数据?

Php 如何向Laravel Passport API令牌添加自定义数据?,php,laravel,jwt,laravel-passport,Php,Laravel,Jwt,Laravel Passport,我正在使用Laravel Passport,我想知道是否可以将自定义数据添加到Laravel\Passport\Token: //This is how I create the token $user = $request->user(); $token = $user->createToken('ACCESS_TOKEN')->accessToken; 我使用头Authorization:Bearer${token} 是否有任何方法可以将自定义信息添加到令牌?大概是这样的

我正在使用Laravel Passport,我想知道是否可以将自定义数据添加到
Laravel\Passport\Token

//This is how I create the token
$user = $request->user();
$token = $user->createToken('ACCESS_TOKEN')->accessToken;
我使用头
Authorization:Bearer${token}

是否有任何方法可以将自定义信息添加到令牌?大概是这样的:

$token->customField = 'custom value';
$value = $request->user()->token()->customField;

我已经阅读了,但没有相关信息。

不,默认情况下Passport不支持此功能

JWTs通过使用自定义声明来支持自定义数据。Passport不提供向JWT添加自定义声明的功能,但看起来有人制作了一个包来添加功能:

通过快速查看,您似乎可以将自定义声明添加到JWT中,但我没有看到任何帮助程序或任何用于读取该数据的内容。您必须解析令牌并自己获取声明:

$jwtParser = app()->make(\Lcobucci\JWT\Parser::class);
$claimValue = $jwtParser->parse('your-bearer-token')->getClaim('your-claim-name');

注意:我自己从来没有这样做过,我从来没有使用过那个软件包,这里的所有东西都没有经过测试。

不,默认情况下Passport不支持这一点

JWTs通过使用自定义声明来支持自定义数据。Passport不提供向JWT添加自定义声明的功能,但看起来有人制作了一个包来添加功能:

通过快速查看,您似乎可以将自定义声明添加到JWT中,但我没有看到任何帮助程序或任何用于读取该数据的内容。您必须解析令牌并自己获取声明:

$jwtParser = app()->make(\Lcobucci\JWT\Parser::class);
$claimValue = $jwtParser->parse('your-bearer-token')->getClaim('your-claim-name');

注意:我自己从来没有这样做过,我从来没有使用过那个软件包,这里的一切都没有经过测试。

您可以通过使用自定义字段创建迁移文件来手动添加它

public function up()
{
    Schema::table('oauth_access_tokens', function (Blueprint $table) {
        $table->string('customField',20)->after('client_id')->nullable();
    });
    
}
在标记create()之后,将字段值添加到表中:

$token = $user->createToken($user->email);
DB::table('oauth_access_tokens')
           ->where('id', $token->token->id)
           ->update(['customField' => $request->customField]);

通过使用自定义字段创建迁移文件,可以手动添加它

public function up()
{
    Schema::table('oauth_access_tokens', function (Blueprint $table) {
        $table->string('customField',20)->after('client_id')->nullable();
    });
    
}
在标记create()之后,将字段值添加到表中:

$token = $user->createToken($user->email);
DB::table('oauth_access_tokens')
           ->where('id', $token->token->id)
           ->update(['customField' => $request->customField]);