当我运行$user->currentAccessToken()->delete();
令牌时,它会Auth::check()
变成false
预期的那样。
但是,当我走到personal_access_tokens
桌子旁时,令牌仍然在那里。没有软删除字段。令牌过期了,Sanctum 怎么办?
当我运行$user->currentAccessToken()->delete();
令牌时,它会Auth::check()
变成false
预期的那样。
但是,当我走到personal_access_tokens
桌子旁时,令牌仍然在那里。没有软删除字段。令牌过期了,Sanctum 怎么办?
可以在 config/sanctum.php 中设置数组节点过期时间
/*
|--------------------------------------------------------------------------
| Expiration Minutes
|--------------------------------------------------------------------------
|
| This value controls the number of minutes until an issued token will be
| considered expired. If this value is null, personal access tokens do
| not expire. This won't tweak the lifetime of first-party sessions.
|
*/
'expiration' => 60 * 24 * 7,
在撰写此答案时,令牌现在从数据库中删除,以便解决。
Sanctum 如何知道令牌是否过期非常简单:
要检查是否过期,它会从C中减去N。如果N - C小于E,则令牌尚未过期。如果它更大,则令牌已过期。
例子:
当您从 5 中减去 8 时,您会得到 3。这距您创建令牌仅 3 小时。达不到你设定的 5 小时。
如果您在上午 11:00 访问数据,则时间范围变为 6 小时,即超过 5 小时,这意味着令牌已过期。
我查看了 sanctumm 的源代码,它似乎是一个处理它的守卫。
if (! $accessToken ||
($this->expiration &&
$accessToken->created_at->lte(now()->subMinutes($this->expiration))) ||
! $this->hasValidProvider($accessToken->tokenable)) {
return;
}
这意味着验证令牌过程如下所示:
如果失败,它只是拒绝请求。不删除令牌。
然而,删除令牌是撤销令牌的手动方式。
您可以通过使用 HasApiTokens 特征提供的令牌关系从数据库中删除令牌来“撤销”令牌: