Laravel 中如何更方便的修改 Passport Personal Access Token 过期时间

栏目: PHP · 发布时间: 7年前

内容简介:认真看过默认时间为 1 年,但是这可能不满足我们的需求,我们想要改成其它更短的时间怎么办呢?今天尝试了一下,应该算是全网可以找到的最简单方法了,直接在app/Providers/AppServiceProvider.php

认真看过 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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Bulletproof Web Design

Bulletproof Web Design

Dan Cederholm / New Riders Press / 28 July, 2005 / $39.99

No matter how visually appealing or packed with content a Web site is, it isn't succeeding if it's not reaching the widest possible audience. Designers who get this guide can be assured their Web site......一起来看看 《Bulletproof Web Design》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具