Laravel Auth simple API总是返回Unauthorized 401错误.

问题描述 投票:-1回答:2

这是让我疯狂... 所有的一切都应该是正确的,但我总是在我的API路由中得到未经授权的错误!

Api.php routes:

Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();

});

RegisterController:

    protected function create(array $data)
{
    return User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => Hash::make($data['password']),
        'api_token' => Str::random(80),
    ]);
}

数据库:

enter image description here

要求:

enter image description here

接受applicationjson已设置。

enter image description here

我也试过用jquery用查询参数api_key来请求,但是也不行!!!!!!!。

我找了很多地方都不能解决这个问题. 我有另一个新的Laravel和auth的应用程序,它的工作完美,但这个项目是旧的,我最近更新到Laravel 7,我不知道为什么这不工作.

更多信息:

我使用php artisan serve来服务这个应用程序.

我的kernel.php:

<?php

namespace App\Http;

使用Illuminate/Foundation/Http/Kernel作为HttpKernel。

class Kernel extends HttpKernel{ ** *应用程序的全局HTTP中间件栈。 * 这些中间件在你的应用程序的每个请求中都会运行。 * @var array * protected $middleware = [ \App/Http/Middleware/TrustProxies::class, \App/Http/Middleware/CheckForMaintenanceMode:: class, \Illuminate\Foundation\Http\Middleware/ValidatePostSize::class, \App\Http\Middleware/TrimStrings::class, \Illuminate\Foundation\Http\Middleware/ConvertEmptyStringsToNull::class, ];

/**
 * The application's route middleware groups.
 *
 * @var array
 */
protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        // \Illuminate\Session\Middleware\AuthenticateSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\VerifyCsrfToken::class,
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
    ],

    'api' => [
        'throttle:60,1',
        'bindings',
        'auth:api',
    ],
];

/**
 * The application's route middleware.
 *
 * These middleware may be assigned to groups or used individually.
 *
 * @var array
 */
protected $routeMiddleware = [
    'auth' => \App\Http\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
    'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
    'can' => \Illuminate\Auth\Middleware\Authorize::class,
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
    'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
    'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
    'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
    'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
    'language' => \App\Http\Middleware\GetRequestLanguage::class,
    'setLocale' => \App\Http\Middleware\SetLocale::class,
];

/**
 * The priority-sorted list of middleware.
 *
 * This forces non-global middleware to always be in the given order.
 *
 * @var array
 */
protected $middlewarePriority = [
    \Illuminate\Session\Middleware\StartSession::class,
    \Illuminate\View\Middleware\ShareErrorsFromSession::class,
    \App\Http\Middleware\Authenticate::class,
    \Illuminate\Routing\Middleware\ThrottleRequests::class,
    \Illuminate\Session\Middleware\AuthenticateSession::class,
    \Illuminate\Routing\Middleware\SubstituteBindings::class,
    \Illuminate\Auth\Middleware\Authorize::class,
];

}

谢谢你... 我希望你能帮我...

laravel api authentication request unauthorized
2个回答
0
投票

你只需要在postman中以Body的形式发送你的token,就像下面这样 邮递员发送认证图片示例你必须在body(表单数据)中发送你的token,你的token的key必须是api_token。


0
投票

我找到了解决办法.

当我从Laravel 6升级到7的时候, 我没有在auth.php中修改这个:

'hash' => 'false''hash' => false,没有引号... 这个废话让我很头疼,最后我发现是它造成的。

        'api' => [
        'driver' => 'token',
        'provider' => 'users',
        'hash' => false,
    ],
© www.soinside.com 2019 - 2024. All rights reserved.