Laravel 圣殿 : How to configure the expiration date of the token?

标签 laravel laravel-sanctum

背景和需要
在我的 api路线文件,我已经按照文档( https://laravel.com/docs/8.x/sanctum#introduction )编写了以下代码:

Route::post('/tokens/create', function (Request $request) {
    $token = $request->user()->createToken($request->token_name);
    return ['token' => $token->plainTextToken];
});
我想设置一个过期延迟,用于比较 token 的创建日期与 token 过期的当前检查日期: token 的创建日期为 x , 当前日期为 y , 延迟是 d所以如果 y > x + d token 就会过期.
我所做的
所以我在目录 vendor/laravel/sanctum 中阅读了一些代码,我找到了类 Guard.php .
类(class)Guard.php包含一个名为 $expiration 的对象属性,设置它的构造函数(除其他外)和 __invoke包含以下过期检查的方法:
if (! $accessToken ||
                ($this->expiration &&
                 $accessToken->created_at->lte(now()->subMinutes($this->expiration))) ||
                ! $this->hasValidProvider($accessToken->tokenable)) {
                return;
            }
如您所见,它完全符合我的要求。但是,我不知道如何为属性 $expiration 设置我自己的值。 .
在同一个文件中,有一些对现有配置文件的暗示,例如:config('sanctum.guard', 'web') .此外,类 SanctumServiceProvider实例化 Guard并将以下值传递给其构造函数:config('sanctum.expiration') .但我不知道如何/在哪里定义这个配置值。也许 https://laravel.com/docs/8.x/configuration config(['sanctum.expiration' => '1277126']); ?你能确认一下吗? ( 但是把这条线放在哪里? )

我的问题是:在 Laravel 8 Sanctum 中,我如何为变量 $expiration 设置自己的值用于 Sanctum token 检查?我应该编辑配置文件,如果是,如何编辑?我应该在终端中输入配置命令吗?

最佳答案

您可以发布 Laravel 配置:

php artisan vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider"
https://laravel.com/docs/8.x/sanctum#installation
在此之后,您可以更改 config/santum.php 中的所有配置选项。 . config中的配置文件将覆盖 vendors默认配置。

关于Laravel 圣殿 : How to configure the expiration date of the token?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65372739/

相关文章:

php - 在 laravel 5.4 中选择下拉列表的选定值

php - 从字符串值运行方法链

angular - 未为使用 Laravel Sanctum 和 Angular 的跨域请求设置 CSRF Coo​​kie

laravel-8 - Laravel Sanctum token ()未定义

php - 拉维尔 4 : Where Not Exists

laravel - 是否可以将图像放在 mpdf Laravel 库中表的 **td** 标签中

php - OctoberCMS 发布表单

Laravel Sanctum 可以使用 Multiauth 防护

Laravel Sanctum 与 Passport