我使用护照对我的 API 进行身份验证 我运行此命令来安装护照:
php artisan passport:install --force
并使用以下代码生成 token :
$objToken = $user->createToken('Token');
$strToken = $objToken->accessToken;
$expiration = $objToken->token->expires_at->diffForHumans();
return response()->json([
token' => $strToken,
'ExpireTime' => $expiration,
], 200);
我发现我的 token 生命周期是一年,我只想将expire_at列设置为1小时 我阅读官方文档并向AuthServiceProvider添加以下代码:
Passport::tokensExpireIn(now()->addDays(15));
Passport::refreshTokensExpireIn(now()->addDays(30));
但是这些代码不起作用,当我记录 expire_at 时,这仍然是一年 请帮助我更改 token 的到期时间。 谢谢很多:)
最佳答案
您正在尝试创建个人访问 token 。
// Passport::tokensExpireIn(now()->addDays(15));
// Passport::refreshTokensExpireIn(now()->addDays(30));
# Get or set when personal access tokens expire.
Passport::personalAccessTokensExpireIn(now()->addHour(1));
结果:
array:2 [
"token" => "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9...."
"ExpireTime" => "59 minutes from now"
]
更新
对于 Laravel < v5.7
Personal access tokens are always long-lived. Their lifetime is not modified when using the
tokensExpireIn
orrefreshTokensExpireIn
methods.
关于php - Laravel Passport token 过期方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59454486/