问题描述
我在中间件中添加了以下代码,用于使用 JWT Auth 进行用户身份验证中间件处理的所有路由.
I have added following code in my middleware for user authentication with JWT Auth, which works fine for all the routes handled by the middleware.
public function handle($request, Closure $next)
{
if ($request->has('token')) {
try {
$this->auth = JWTAuth::parseToken()->authenticate();
return $next($request);
} catch (JWTException $e) {
return redirect()->guest('user/login');
}
}
}
但是对于使用 Post方法的一条路由,在该路由中令牌已正确传递,但仍然出现 JWTException-无法从请求中解析令牌,我尝试过的相同路线
But for one route with Post Method where the token is getting passed properly but still I am getting JWTException - The token could not be parsed from the request, on the same route when I tried
public function handle($request, Closure $next)
{
if ($request->has('token')) {
try {
dd($request->input('token'));
$this->auth = JWTAuth::parseToken()->authenticate();
return $next($request);
} catch (JWTException $e) {
return redirect()->guest('user/login');
}
}
}
输出:
"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9iaXNzIjoiaHR0cDpcL1wvbG9jYWxob3N0OjgwMDFcL2F1dGhcL2xvZ2luIiwiaWF0IjoxNDcyNTI4NDU0LCJleHAiOjE0NzI1MzIwNTQsIm5iZiI6MTQ3MjUyODQ1NCwianRpIjoiM2E0M2ExYTZlNmM5NjUxZDgxYjZhNDcxMzkxODJlYjAifQ.CH8ES2ADTCrVWeIO8uU31bGDnH7h-ZVTWxrdXraLw8s"
我能够看到有效令牌,该令牌正在用于访问其他路由,并且在所有其他路由上都正常运行.
I am able to see the Valid Token which I am using to access another routes and which is working flawlessly for all other routes.
提前谢谢!
推荐答案
从您的描述中,我检查了JWT Auth的源文件.
From your description, i checked source file of JWT Auth.
在Tymon \ JWTAuth \ JWTAuth类的第191-219行中,有两个功能:
in class Tymon\JWTAuth\JWTAuth line 191 - 219 , there have two functions:
/**
* Parse the token from the request.
*
* @param string $query
*
* @return JWTAuth
*/
public function parseToken($method = 'bearer', $header = 'authorization', $query = 'token')
{
if (! $token = $this->parseAuthHeader($header, $method)) {
if (! $token = $this->request->query($query, false)) {
throw new JWTException('The token could not be parsed from the request', 400);
}
}
return $this->setToken($token);
}
/**
* Parse token from the authorization header.
*
* @param string $header
* @param string $method
*
* @return false|string
*/
protected function parseAuthHeader($header = 'authorization', $method = 'bearer')
{
$header = $this->request->headers->get($header);
if (! starts_with(strtolower($header), $method)) {
return false;
}
return trim(str_ireplace($method, '', $header));
}
检查它们的逻辑,我认为没有正确提供您的请求标头.
check the logic of them, i believe your request header is not properly provided.
if (! $token = $this->parseAuthHeader($header, $method)) { // all your get method not passed this step
if (! $token = $this->request->query($query, false)) { // all your post method stucked here
throw new JWTException('The token could not be parsed from the request', 400);
}
}
适当的标题是这样的:
http POST http://${host}/api/v1/product/favorite/111 "Authorization: Bearer ${token}"
.
以上是我可以为您提供的服务,希望它能帮助您解决所有问题.如果没有,您仍然可以从这两个功能中进行调试.
above is what i can offers for you, hope it will help you through your thoughts. If it won't you still can debug from those two functions.
这篇关于Laravel-JWT Auth无法从请求中解析令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!