本文介绍了护照 - “未经认证".- Laravel 5.3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有人能解释为什么我已经执行了成功的 Oauth 2 身份验证过程后,为什么我 未经身份验证.

I hope someone could explain why I'm unauthenticated when already has performed a successfull Oauth 2 authentication process.

我已经设置了 Passport 包,就像在 Laravel 的文档中一样,我成功地通过了身份验证,收到了一个令牌值等等.但是,当我尝试在 /api/user 上执行 get 请求时,我收到一个 Unauthenticated 错误作为响应.我使用令牌值作为带有键名 Authorization 的标头,正如文档中所述.

I've set up the Passport package like in Laravel's documentation and I successfully get authenticated, receives a token value and so on. But, when I try to do a get request on, let say, /api/user, I get a Unauthenticated error as a response. I use the token value as a header with key name Authorization, just as described in the docs.

Route::get('/user', function (Request $request) {
    return $request->user();
})->middleware("auth:api");

这个功能是想把我自己作为经过身份验证的用户还给我,但我只得到Unauthenticated.同样,如果我只返回第一个用户,我将再次获得 Unauthenticated.

This function is suppose to give back my self as the authenticated user, but I'm only getting Unauthenticated. Likewise, if I just return the first user, I'm again getting Unauthenticated.

Route::get('/test', function(Request $request) {
    return AppUser::whereId(1)->first();
})->middleware("auth:api");

在来自 Laracast 的教程中,指导完成 Passport 的设置,向导没有->他的路由中的中间件(auth:api").但如果它不存在,那么根本不需要身份验证!

In a tutorial from Laracast, guiding through the setup of Passport, the guider doesn't have the ->middleware("auth:api") in his routes. But if its not there, well then there's no need for authentication at all!

拜托,欢迎提出任何建议或答案!

Please, any suggestions or answers are more then welcome!

推荐答案

你必须为你生成的令牌设置一个过期日期,

You have to set an expiration date for the tokens you are generating,

将 AuthServiceProvider 中的启动方法设置为类似于下面的代码,然后尝试生成新令牌.护照默认过期返回负数

set the boot method in your AuthServiceProvider to something like the code below and try generating a new token. Passports default expiration returns a negative number

public function boot()
{
  $this->registerPolicies();
   Passport::routes();
   Passport::tokensExpireIn(Carbon::now()->addDays(15));
   Passport::refreshTokensExpireIn(Carbon::now()->addDays(30));
}

这篇关于护照 - “未经认证".- Laravel 5.3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-29 13:01