问题描述
当我运行$user->currentAccesstoken()->delete();
时,令牌已过期,Auth::check()
变成false
,与预期的一样。
但是,当我转到personal_access_tokens
表时,令牌仍然存在。没有软删除字段。现在令牌过期了,圣殿怎么办?
解决方法
可以在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,
,
我查看了sanctumm的源代码,似乎是守护它的守护者。
if (! $accessToken ||
($this->expiration &&
$accessToken->created_at->lte(now()->subMinutes($this->expiration))) ||
! $this->hasValidProvider($accessToken->tokenable)) {
return;
}
这意味着验证令牌过程如下所示:
- 检查令牌是否存在于数据库中
- 检查令牌creation_date是否未超过到期时间
- 检查可令牌化模型是否与提供商的模型类型匹配
- 检查可令牌化模型是否支持API令牌
一旦失败,它只是拒绝请求。不删除令牌。
但是,删除令牌是撤销令牌的手动方法。
您可以使用HasApiTokens特性提供的令牌关系,通过从数据库中删除令牌来“撤销”令牌: