php 如何从 Laravel 的请求中获取 Bearer 令牌

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47158522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:32:36  来源:igfitidea点击:

How to get Bearer token from a request in Laravel

phplaraveljwt

提问by Barnabas Kecskes

I am expecting a JWT token from all the incoming request, and it should be included on request headers like: Authorization => 'Bearer: some token here'

我期待来自所有传入请求的 JWT 令牌,它应该包含在请求标头中,例如: Authorization => 'Bearer: some token here'

I want to get this token and verify it: here is what I am trying:

我想获得这个令牌并验证它:这是我正在尝试的:

$token = $request->header('Authorization');

and this is what I get:

这就是我得到的:

"Authorization: Bearer: eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJleGFtcGxlLm9yZyIsImF1ZCI6ImV4YW1wbGUuY29tIiwiaWF0IjoxMzU2OTk5NTI0LCJuYmYiOjEzNTcwMDAwMDB9.UQUJV7KmNWPiwiVFAqr4Kx6O6yd69lfbtyWF8qa8iMN2dpZZ1t6xaF8HUmY46y9pZN76f5UMGA0p_CMqymRdYfNiKsiTd2V_3Qpt9LObaLg6rq18j3GLHfdr8nyBzO3v7gTpmNaU6Xy47aMDsbcs593Lx_lD3PnO41oEHgih7CsRKW1WcW1radnpEhdDO7-GpmGOF6xUnpAlQ9EHqpqnIlZPbVoJg92Iwozn-07uuWrkyKUpYN4IPpstd1ks3cKlJ6FH-2ROiC4N0MVLxp4lhUyKhLdwgDWYH4tjtdrEVK0a3_zVtK1ukvriEJqMkfYHnE6Bwv_pv_-lRNy_y7m-YQ"

Question is there any way to grab only the token not including "Authorization: Bearer"and of course I could parse the whole string and get the token, but I am just wondering if there is another way of getting it without parsing.

问题是有什么方法可以只获取不包括的令牌"Authorization: Bearer",当然我可以解析整个字符串并获取令牌,但我只是想知道是否有另一种方法可以在不解析的情况下获取它。

回答by Barnabas Kecskes

There is a bearerToken()method on the Illuminate\Http\Requestobject, so you should be able to just do $token = $request->bearerToken();and get back what you expect (that's in Laravel 5.5 - I'm not sure of previous versions).

对象bearerToken()上有一个方法Illuminate\Http\Request,所以你应该能够做$token = $request->bearerToken();并得到你期望的(在 Laravel 5.5 中 - 我不确定以前的版本)。

回答by Chandan Sharma

To Get the Bearer token from Header in API call, I used below method. It is working for me in Laravel 6.6.0

为了从 API 调用中的 Header 获取 Bearer 令牌,我使用了以下方法。它在 Laravel 6.6.0 中对我有用

$request = request();
$token = $request->bearerToken();

Hope this will work for you.

希望这对你有用。

Used in Laravel 6.6.0

在 Laravel 6.6.0 中使用

回答by Plabon Dutta

The method bearerToken()was introduced Laravel 5.2. You can use: $token = $request->bearerToken();to get the token. In case you're planning to get token from a header with a changed text from "Bearer" to something else, you can define your own function like below:

该方法bearerToken()是在 Laravel 5.2 中引入的。您可以使用: $token = $request->bearerToken();来获取令牌。如果您打算从标题中获取标记,并将文本从“Bearer”更改为其他内容,您可以定义自己的函数,如下所示:

  public function bearerToken()
  {
       $header = $this->header('Authorization', '');
       if (Str::startsWith($header, 'Bearer ')) {
           return Str::substr($header, 7);
       }
  }

回答by Raza Mehdi

You may do something like:

您可以执行以下操作:

$response = explode(':', $request->header('Authorization'));
$token = trim($response[2]);

回答by the_hasanov

if you use auth:api don't need set guard name 'api'

如果您使用 auth:api 不需要设置保护名称 'api'

\Auth::guard('api')->getTokenForRequest();