Laravel Passport令牌寿命 [英] Laravel Passport token lifetime

查看:423
本文介绍了Laravel Passport令牌寿命的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白我在做什么. 我无法设置令牌的到期时间.

I don't get what I'm doing wrong. I can't set token expiration time.

<?php

namespace App\Providers;

class AuthServiceProvider extends ServiceProvider
{
    public function boot()
    {
        $this->registerPolicies();

        Passport::tokensExpireIn(Carbon::now()->addDays(1));
        Passport::refreshTokensExpireIn(Carbon::now()->addDays(30));
    }
}

但是当我呼叫$user->createToken()时,例如:

BUT when I call $user->createToken(), for example like this:

<?php
// as a demo
namespace App\Http\Middleware;

class ParseSpecialToken
{
    public function handle($request, Closure $next)
    {
        $user = User::find(1);
        $accessToken = $user->createToken('Some token')->accessToken;
        $request->headers->add(['Authorization' => 'Bearer '. $accessToken]);

        return $next($request);
    }
}

代币有效期仍为1年,而不是1天.为什么?如何更改exp时间?

Token expiration is still 1 year, not 1 day. Why? How to change exp time?

推荐答案

以下是用于更新所有授权类型的到期时间的方法:

Here are the methods used to update expiration time for all the grant types :

个人访问令牌:

public function boot(){
        $this->registerPolicies();

        Passport::routes();
        Passport::personalAccessTokensExpireIn(Carbon::now()->addHours(24));
        Passport::refreshTokensExpireIn(Carbon::now()->addDays(30));
}

全部休息

public function boot(){
        $this->registerPolicies();

        Passport::routes();
        Passport::tokensExpireIn(Carbon::now()->addHours(24));
        Passport::refreshTokensExpireIn(Carbon::now()->addDays(30));
}

只需在AuthServiceProvider的启动方法中更新以上代码即可.

Just update the above code in the boot method of AuthServiceProvider.

这篇关于Laravel Passport令牌寿命的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆