Laravel 中如何更方便的修改 Passport Personal Access Token 過期時間
認真看過ofollow,noindex" target="_blank">Laravel Passport 文件
的人應該知道,它的 Personal Access Token 是不支援自定義過期時間的,tokensExpireIn
對此類 token 無效,原文如下:
Personal access tokens are always long-lived. Their lifetime is not modified when using the tokensExpireIn or refreshTokensExpireIn methods.
預設時間為 1 年,但是這可能不滿足我們的需求,我們想要改成其它更短的時間怎麼辦呢?今天嘗試了一下,應該算是全網可以找到的最簡單方法了,直接在app/Providers/AppServiceProvider
中新增一句就可以搞定,下面以改為有效期為 1 周的示例來演示:
app/Providers/AppServiceProvider.php
<?php //... use Laravel\Passport\Bridge\PersonalAccessGrant; use League\OAuth2\Server\AuthorizationServer; //... class AppServiceProvider extends ServiceProvider { /** * Bootstrap any application services. */ public function boot() { $this->app->get(AuthorizationServer::class) ->enableGrantType(new PersonalAccessGrant(), new \DateInterval('P1W')); } //... } //...
關於時間值的寫法,請參考:
https://secure.php.net/manual/en/dateinterval.construct.php