简体   繁体   English

Laravel Passport API身份验证不适用于承载令牌

[英]Laravel Passport API Authentication not working with Bearer Token

i'm currently developing a Laravel Application. 我目前正在开发Laravel应用程序。 Inside the Application I need to create a public api, which need to be accessible if a client got an access token. 在应用程序内部,我需要创建一个公共api,如果客户端获得了访问令牌,则需要可以对其进行访问。

Therefor i'm using the laravel/passport package. 因此,我正在使用laravel / passport软件包。

Retrieving the access_token is working fine. 检索access_token工作正常。 The critical part is to authenticate with this access token and get json data as response from a protected route. 关键部分是使用此访问令牌进行身份验证,并从受保护的路由获取json数据作为响应。

The Setup is the following: 安装程序如下:

  • Laravel 5.7.6 Laravel 5.7.6
  • Laravel Passport 6.0.7 Laravel Passport 6.0.7

With Laravel 5.6.30 and Passport 6.0.7 the cookie serialization got disabled. 使用Laravel 5.6.30和Passport 6.0.7时,cookie序列化被禁用。

/app/Providers/AuthServiceProvider.php /app/Providers/AuthServiceProvider.php

$this->registerPolicies();
Passport::withoutCookieSerialization(); // because of the Laravel Update
Passport::routes();
Passport::tokensExpireIn(Carbon::now()->addDays(20));
Passport::refreshTokensExpireIn(Carbon::now()->addDays(20));

As mentioned over here i changed the expiration time of the tokens so they don't overflow or start with a - . 如前所述在这里我改变了令牌的到期时间,使他们不溢出或用开始-

Over here @adamj mentioned to have a look at the mapApiRoutes() 这里 @adamj提到要看看mapApiRoutes()

/app/Providers/RouteServiceProviders.php /app/Providers/RouteServiceProviders.php

protected function mapApiRoutes()
{
    Route::prefix('api')
        ->middleware('auth:api')
        ->namespace($this->namespace)
        ->group(base_path('routes/api.php'));
}

The Request i'm sending towards my API got the following Headers set: 我向我的API发送的请求设置了以下标头:

Accept: application/json 
Authorization: Bearer {my_access_token}

The Route i'm trying to access within Postman is the following: 我尝试在邮递员内访问的路线如下:

Route::group(['middleware' => ['auth:api']], function () {
  Route::get('/hello/world', function () {
      return 'Hello World';
  });
});

The Response is then always a HTTP Status of 401. Can somebody help? 这样,响应始终是401的HTTP状态。有人可以帮忙吗?

I really got no more idea on how to fix this. 我真的不知道如何解决这个问题。

Thanks in regards! 谢谢问候!

The error was not inside Laravel. 该错误不在Laravel内部。 The error was related to Postman. 该错误与邮递员有关。

Inside the Authorization Tab you could choose a type. 在“授权”选项卡中,您可以选择一种类型。 Unless you don't choose No Auth your manually set Header got replaced by Postman. 除非您不选择No Auth,否则您手动设置的Header将被Postman取代。

Hope this may help anybody. 希望这可以对任何人有帮助。

This is because Apache does not, by default, pass authorization headers to PHP. 这是因为默认情况下,Apache不会将授权标头传递给PHP。 I found this solution: 我找到了这个解决方案:

https://support.deskpro.com/en/kb/articles/missing-authorization-headers-with-apache https://support.deskpro.com/cn/kb/articles/missing-authorization-headers-with-apache

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM