我使用Laravel 5.1 + Dingo API + JWT令牌。 我的路线是:
$api = app('Dingo\Api\Routing\Router');
$api->version('v1', function($api) {
$api->post('authenticate', 'App\Http\Controllers\AuthenticateController@authenticate');
$api->post('logout', 'App\Http\Controllers\AuthenticateController@logout');
$api->get('token', 'App\Http\Controllers\AuthenticateController@getToken');
$api->get('test', 'App\Http\Controllers\BitemsController@test');
});
$api->version('v1', ['middleware' => 'api.auth'], function ($api) {
$api->get('authenticated_user', 'App\Http\Controllers\AuthenticateController@authenticatedUser');
$api->get('getvoucher', 'App\Http\Controllers\BitemsController@index');
$api->post('/store', 'App\Http\Controllers\BitemsController@store');
$api->put('/update/{key}', 'App\Http\Controllers\BitemsController@update');
});
现在,当我在Postman上运行获取令牌时,一切都很好,所以我得到了:
{
"token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjExMDQsImlzcyI6Imh0dHBzOi8vYXBwLmRpbmVhbmRnaWZ0LmNvbS9hcGkvYXV0aGVudGljYXRlIiwiaWF0IjoxNTE1NDE3NDQ0LCJleHAiOjE1MTU0MjEwNDQsIm5iZiI6MTUxNTQxNzQ0NCwianRpIjoiNzk2dXZCQnhreGtpSDJneiJ9.XJiH3v95-FJOVYT_XfWIbQZ_lHqYDmWRR-jIHp6uoag"
}
但现在我想要检索auth用户,我的功能受中间件保护,但我得到了:
{
"message": "Failed to authenticate because of bad credentials or an invalid authorization header.",
"status_code": 401,
"debug": {
"line": 113,
"file": "/home/dgadmin/public_html/test/vendor/dingo/api/src/Auth/Auth.php",
"class": "Symfony\\Component\\HttpKernel\\Exception\\UnauthorizedHttpException", ... ETC...
这里有什么问题以及如何解决这个问题?
答案 0 :(得分:0)
你是否在网址中传递令牌?
http://url?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjExMDQsImlzcyI6Imh0dHBzOi8vYXBwLmRpbmVhbmRnaWZ0LmNvbS9hcGkvYXV0aGVudGljYXRlIiwiaWF0IjoxNTE1NDE3NDQ0LCJleHAiOjE1MTU0MjEwNDQsIm5iZiI6MTUxNTQxNzQ0NCwianRpIjoiNzk2dXZCQnhreGtpSDJneiJ9.XJiH3v95-FJOVYT_XfWIbQZ_lHqYDmWRR-jIHp6uoag
答案 1 :(得分:0)
如果您已注册身份验证提供程序,请检入应用程序的配置config/api.php
。如果这不起作用,请用它的内容更新您的问题。
'auth' => [
'jwt' => 'Dingo\Api\Auth\Provider\JWT',
],