Laravel 5.4 Dingo/JWT API: 验证有效令牌失败
Laravel 5.4 Dingo/JWT API: Failed to authenticate for valid token
我已经用 Laravel 5.4 实现了 Dingo REST API。我能够从服务器获取令牌。以及令牌已被服务器成功接收。但它抛出 401 异常,这是调试堆栈跟踪:
我已经尝试通过 url 相同的结果传递令牌。
{
"message": "Failed to authenticate because of bad credentials or an invalid authorization header.",
"status_code": 401,
"debug": {
"line": 113,
"file": "C:\Users\ghgf\PhpstormProjects\edwheel\vendor\dingo\api\src\Auth\Auth.php",
"class": "Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException",
"trace": [
"#0 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\dingo\api\src\Auth\Auth.php(96): Dingo\Api\Auth\Auth->throwUnauthorizedException(Array)",
"#1 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\dingo\api\src\Http\Middleware\Auth.php(52): Dingo\Api\Auth\Auth->authenticate(Array)",
"#2 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(148): Dingo\Api\Http\Middleware\Auth->handle(Object(Dingo\Api\Http\Request), Object(Closure))",
"#3 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Dingo\Api\Http\Request))",
"#4 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\dingo\api\src\Http\Middleware\PrepareController.php(45): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Dingo\Api\Http\Request))",
"#5 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(148): Dingo\Api\Http\Middleware\PrepareController->handle(Object(Dingo\Api\Http\Request), Object(Closure))",
"#6 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Dingo\Api\Http\Request))",
"#7 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(102): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Dingo\Api\Http\Request))",
"#8 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\laravel\framework\src\Illuminate\Routing\Router.php(576): Illuminate\Pipeline\Pipeline->then(Object(Closure))",
"#9 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\laravel\framework\src\Illuminate\Routing\Router.php(535): Illuminate\Routing\Router->runRouteWithinStack(Object(Illuminate\Routing\Route), Object(Dingo\Api\Http\Request))",
"#10 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\laravel\framework\src\Illuminate\Routing\Router.php(513): Illuminate\Routing\Router->dispatchToRoute(Object(Dingo\Api\Http\Request))",
"#11 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\dingo\api\src\Routing\Adapter\Laravel.php(81): Illuminate\Routing\Router->dispatch(Object(Dingo\Api\Http\Request))",
"#12 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\dingo\api\src\Routing\Router.php(513): Dingo\Api\Routing\Adapter\Laravel->dispatch(Object(Dingo\Api\Http\Request), 'V1')",
"#13 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\dingo\api\src\Http\Middleware\Request.php(126): Dingo\Api\Routing\Router->dispatch(Object(Dingo\Api\Http\Request))",
"#14 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(114): Dingo\Api\Http\Middleware\Request->Dingo\Api\Http\Middleware\{closure}(Object(Dingo\Api\Http\Request))",
"#15 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TransformsRequest.php(30): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Dingo\Api\Http\Request))",
"#16 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(148): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Dingo\Api\Http\Request), Object(Closure))",
"#17 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TransformsRequest.php(30): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Dingo\Api\Http\Request))",
"#18 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(148): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Dingo\Api\Http\Request), Object(Closure))",
"#19 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\ValidatePostSize.php(27): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Dingo\Api\Http\Request))",
"#20 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(148): Illuminate\Foundation\Http\Middleware\ValidatePostSize->handle(Object(Dingo\Api\Http\Request), Object(Closure))",
"#21 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode.php(46): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Dingo\Api\Http\Request))",
"#22 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(148): Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Dingo\Api\Http\Request), Object(Closure))",
"#23 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(102): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Dingo\Api\Http\Request))",
"#24 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\dingo\api\src\Http\Middleware\Request.php(127): Illuminate\Pipeline\Pipeline->then(Object(Closure))",
"#25 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\dingo\api\src\Http\Middleware\Request.php(103): Dingo\Api\Http\Middleware\Request->sendRequestThroughRouter(Object(Dingo\Api\Http\Request))",
"#26 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(148): Dingo\Api\Http\Middleware\Request->handle(Object(Illuminate\Http\Request), Object(Closure))",
"#27 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))",
"#28 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(102): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))",
"#29 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(149): Illuminate\Pipeline\Pipeline->then(Object(Closure))",
"#30 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(116): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))",
"#31 C:\Users\ghgf\PhpstormProjects\edwheel\public\index.php(54): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))",
"#32 C:\Users\ghgf\PhpstormProjects\edwheel\server.php(21): require_once('C:\\Users\\ghgf\\P...')",
"#33 {main}"
]
}
很久以前我终于解决了这个问题。实际上是在用 Volley 向 Dingo API 提出请求时。我们必须将内容类型 header 设置为 JSON。
希望对大家有所帮助。
我已经用 Laravel 5.4 实现了 Dingo REST API。我能够从服务器获取令牌。以及令牌已被服务器成功接收。但它抛出 401 异常,这是调试堆栈跟踪:
我已经尝试通过 url 相同的结果传递令牌。
{
"message": "Failed to authenticate because of bad credentials or an invalid authorization header.",
"status_code": 401,
"debug": {
"line": 113,
"file": "C:\Users\ghgf\PhpstormProjects\edwheel\vendor\dingo\api\src\Auth\Auth.php",
"class": "Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException",
"trace": [
"#0 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\dingo\api\src\Auth\Auth.php(96): Dingo\Api\Auth\Auth->throwUnauthorizedException(Array)",
"#1 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\dingo\api\src\Http\Middleware\Auth.php(52): Dingo\Api\Auth\Auth->authenticate(Array)",
"#2 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(148): Dingo\Api\Http\Middleware\Auth->handle(Object(Dingo\Api\Http\Request), Object(Closure))",
"#3 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Dingo\Api\Http\Request))",
"#4 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\dingo\api\src\Http\Middleware\PrepareController.php(45): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Dingo\Api\Http\Request))",
"#5 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(148): Dingo\Api\Http\Middleware\PrepareController->handle(Object(Dingo\Api\Http\Request), Object(Closure))",
"#6 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Dingo\Api\Http\Request))",
"#7 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(102): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Dingo\Api\Http\Request))",
"#8 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\laravel\framework\src\Illuminate\Routing\Router.php(576): Illuminate\Pipeline\Pipeline->then(Object(Closure))",
"#9 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\laravel\framework\src\Illuminate\Routing\Router.php(535): Illuminate\Routing\Router->runRouteWithinStack(Object(Illuminate\Routing\Route), Object(Dingo\Api\Http\Request))",
"#10 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\laravel\framework\src\Illuminate\Routing\Router.php(513): Illuminate\Routing\Router->dispatchToRoute(Object(Dingo\Api\Http\Request))",
"#11 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\dingo\api\src\Routing\Adapter\Laravel.php(81): Illuminate\Routing\Router->dispatch(Object(Dingo\Api\Http\Request))",
"#12 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\dingo\api\src\Routing\Router.php(513): Dingo\Api\Routing\Adapter\Laravel->dispatch(Object(Dingo\Api\Http\Request), 'V1')",
"#13 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\dingo\api\src\Http\Middleware\Request.php(126): Dingo\Api\Routing\Router->dispatch(Object(Dingo\Api\Http\Request))",
"#14 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(114): Dingo\Api\Http\Middleware\Request->Dingo\Api\Http\Middleware\{closure}(Object(Dingo\Api\Http\Request))",
"#15 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TransformsRequest.php(30): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Dingo\Api\Http\Request))",
"#16 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(148): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Dingo\Api\Http\Request), Object(Closure))",
"#17 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TransformsRequest.php(30): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Dingo\Api\Http\Request))",
"#18 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(148): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Dingo\Api\Http\Request), Object(Closure))",
"#19 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\ValidatePostSize.php(27): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Dingo\Api\Http\Request))",
"#20 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(148): Illuminate\Foundation\Http\Middleware\ValidatePostSize->handle(Object(Dingo\Api\Http\Request), Object(Closure))",
"#21 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode.php(46): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Dingo\Api\Http\Request))",
"#22 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(148): Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Dingo\Api\Http\Request), Object(Closure))",
"#23 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(102): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Dingo\Api\Http\Request))",
"#24 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\dingo\api\src\Http\Middleware\Request.php(127): Illuminate\Pipeline\Pipeline->then(Object(Closure))",
"#25 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\dingo\api\src\Http\Middleware\Request.php(103): Dingo\Api\Http\Middleware\Request->sendRequestThroughRouter(Object(Dingo\Api\Http\Request))",
"#26 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(148): Dingo\Api\Http\Middleware\Request->handle(Object(Illuminate\Http\Request), Object(Closure))",
"#27 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))",
"#28 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(102): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))",
"#29 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(149): Illuminate\Pipeline\Pipeline->then(Object(Closure))",
"#30 C:\Users\ghgf\PhpstormProjects\edwheel\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(116): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))",
"#31 C:\Users\ghgf\PhpstormProjects\edwheel\public\index.php(54): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))",
"#32 C:\Users\ghgf\PhpstormProjects\edwheel\server.php(21): require_once('C:\\Users\\ghgf\\P...')",
"#33 {main}"
]
}
很久以前我终于解决了这个问题。实际上是在用 Volley 向 Dingo API 提出请求时。我们必须将内容类型 header 设置为 JSON。
希望对大家有所帮助。