当前位置: 首页 > 知识库问答 >
问题:

如何检索Laravel护照访问令牌过期时间或日期时间?

东门令
2023-03-14

我用的是Laravel 5.4和Passport 4。我只想使用第一方应用程序。因此,正如这个答案所建议的,我不想把ClientID和ClientSecret放在应用程序中。我已经输入了AuthServiceProvider的boot()方法

Passport::routes();
Passport::tokensExpireIn(Carbon::now()->addDays(30));
Passport::refreshTokensExpireIn(Carbon::now()->addDays(60));

我在api中添加了自己的路由。php接受应用程序登录:

Route::post('login', 'Auth\LoginController@apiLogin');

这是我的行动:

public function apiLogin(Request $request)
{
    $credentials = $request->only('email', 'password');

    if (Auth::attempt($credentials)) {
        // Authentication passed...
        $user = Auth::user();
        $token = $user->createToken('API Access')->accessToken;

        return response()->json(["token_type" =>"Bearer","expires_in" => 2592000,"access_token" => $token]);
    }

    return response()->json(["error" => "invalid_credentials", "message" => "The user credentials were incorrect."], 401);
}

是否有任何方法可以检索中过期的秒数(30天)=


共有2个答案

井昊乾
2023-03-14
<?php
//...
use Laravel\Passport\Bridge\PersonalAccessGrant;
use League\OAuth2\Server\AuthorizationServer;
//...

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     */
    public function boot()
    {
        // http://php.net/manual/zh/dateinterval.construct.php
        $lifetime = new \DateInterval('P1W');

        $this->app->get(AuthorizationServer::class)
              ->enableGrantType(
                 new PersonalAccessGrant(), 
                 $lifetime
              );
    }

    //...
}
//...
戚鸿
2023-03-14

下面是我如何设法从对象中获得它:

正如蒂姆·刘易斯在评论中指出的,有一个$令牌属性,$user-

public function apiLogin(Request $request)
{
    $credentials = $request->only('email', 'password');

    if (Auth::attempt($credentials)) {
        // Authentication passed...

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

        $user = Auth::user();
        $objToken = $user->createToken('API Access');
        $strToken = $objToken->accessToken;

        $expiration = $objToken->token->expires_at->diffInSeconds(Carbon::now());

        return response()->json(["token_type" => "Bearer", "expires_in" => $expiration, "access_token" => $strToken]);
    }

    return response()->json(["error" => "invalid_credentials", "message" => "The user credentials were incorrect."], 401);
}

但是,如果在AuthServiceProvider引导()中使用这两行,请小心:

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

因为根据这个Laravel护照问题,它不会在Laravel 5.4的密码授予类型中用个人访问令牌替换到期。

 类似资料:
  • 我正在尝试使用Laravel护照,我不知道我是否发现了一个bug或者我遗漏了什么。 登录控制器: 因此,我在用户登录时创建令牌。在数据库中,我看到生成的令牌在字段处过期:一周后,所以它是正确的。但如果我更改了旧日期的值,我仍然可以使用此令牌。。。 拉威尔只是忽略了这个领域?为什么? 我做了研究,但我有点困惑。。。很多人说代币不会过期或在一年内过期。但后来我发现了一些关于这个片段的帖子: 我明白To

  • 我正在开发标准的LaravelRESTAPI。我正在关注Laravel5.4API认证(Passport)文档。 我需要的是,用户应该通过API登录并获得不会过期的访问令牌。 表具有列。默认值为一年。我把expires_at日期改成了昨天。但它仍然有效。我搜索关于Laravel Passport令牌寿命。有人说过期日期没有根据任何要求进行检查。 是真的吗?如果是真的,那么列的目的是什么?有人能解释

  • 当我从Google API获得一个< code>access_token时,它带有一个< code>expires_in值。根据文档,该值表示“访问令牌的剩余寿命”。 这个值的单位是多少?

  • 我用ReactJS做单页网页登录。问题是如何以及在哪里保存令牌过期时间。我需要保存在sessionStore中,但当浏览器关闭时,所有的数据都将被删除。本地商店?但数据将永远。或者我可以在localStore中保存并在每个事件中添加函数,该函数检查localStore的过期时间,当事件触发成功时再次更新localStore?但代码看起来很可怕...性能问题呢?这大概可以接受吗?

  • 我正在尝试更改访问令牌Laravel Passport的过期日期。 以下是我尝试过的: AuthServiceProvider 用户控制器 但是没有成功。在字段的数据库中,过期日期没有更改,默认情况下仍然是一年。 我正在尝试这样做,因为我想在访问令牌过期时重定向到登录表单。我怎么能做到呢? 我也不确定刷新令牌会发生什么,它是否会返回另一个访问令牌,并且用户不需要授权?