如何从Laravel中的请求获取Bearer令牌 [英] How to get Bearer token from a request in Laravel
问题描述
我希望所有传入请求中都包含JWT令牌,并且应将其包含在请求标头中,例如:Authorization => 'Bearer: some token here'
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'
我想获取此令牌并进行验证:这是我正在尝试的操作:
I want to get this token and verify it: here is what I am trying:
$token = $request->header('Authorization');
这就是我得到的:
"Authorization: Bearer: eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJleGFtcGxlLm9yZyIsImF1ZCI6ImV4YW1wbGUuY29tIiwiaWF0IjoxMzU2OTk5NTI0LCJuYmYiOjEzNTcwMDAwMDB9.UQUJV7KmNWPiwiVFAqr4Kx6O6yd69lfbtyWF8qa8iMN2dpZZ1t6xaF8HUmY46y9pZN76f5UMGA0p_CMqymRdYfNiKsiTd2V_3Qpt9LObaLg6rq18j3GLHfdr8nyBzO3v7gTpmNaU6Xy47aMDsbcs593Lx_lD3PnO41oEHgih7CsRKW1WcW1radnpEhdDO7-GpmGOF6xUnpAlQ9EHqpqnIlZPbVoJg92Iwozn-07uuWrkyKUpYN4IPpstd1ks3cKlJ6FH-2ROiC4N0MVLxp4lhUyKhLdwgDWYH4tjtdrEVK0a3_zVtK1ukvriEJqMkfYHnE6Bwv_pv_-lRNy_y7m-YQ"
有什么方法可以只获取不包含"Authorization: Bearer"
的令牌
我当然可以解析整个字符串并获得令牌,但是我只是想知道是否还有另一种无需解析就可以获取它的方法.
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.
推荐答案
Illuminate\Http\Request
对象上有一个bearerToken()
方法,因此您应该能够执行$token = $request->bearerToken();
并返回您的期望(即在Laravel 5.5中-我不确定以前的版本.
There is a bearerToken()
method on the Illuminate\Http\Request
object, 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).
这篇关于如何从Laravel中的请求获取Bearer令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!