其他分享
首页 > 其他分享> > 我使用Laravel护照创建了API身份验证.当授权令牌出错时,会向我发送错误“未定义路由[登录]”

我使用Laravel护照创建了API身份验证.当授权令牌出错时,会向我发送错误“未定义路由[登录]”

作者:互联网

我使用laravel护照创建了API身份验证.授权令牌出错时,尽管我需要JSON响应(例如“未经授权的401”),但会向我发送错误“未定义路由[登录]”

这是api.php.这里的users / authenticate是一个登录路由,但是当我使用auth:api中间件内部的其他路由时.如果令牌是错误的,则会向我发送错误消息“未定义路由[登录]”,但我不需要此错误.我需要JSON错误,例如{error:unauthorized,代码:401}.

<?php

use Illuminate\Http\Request;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
    Route::post('users/authenticate', ['uses' => 'Auth\LoginController@login']);






Route::group(['middleware' => ['auth:api']], function() {    
    /* Business details for New register business, get all business, update business, get single business */
    Route::post('businesses/create', ['uses' => 'Business\BusinessController@businessRegister']);
    Route::post('businesses/{id}', ['uses' => 'Business\BusinessController@businessUpdate']);
    Route::get('businesses/{id}', ['uses' => 'Business\BusinessController@businessGet']);
    Route::get('businesses-info/{id}', ['uses' => 'Business\BusinessController@businessInfoGet']);

});

这是auth.php

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'api' => [
        'driver' => 'passport',
        'provider' => 'users',
    ],
],

这是AuthServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Laravel\Passport\Passport;

class AuthServiceProvider extends ServiceProvider
{
    /**
     * The policy mappings for the application.
     *
     * @var array
     */
    protected $policies = [
        'App\Model' => 'App\Policies\ModelPolicy',
    ];

    /**
     * Register any authentication / authorization services.
     *
     * @return void
     */
    public function boot()
    {
        $this->registerPolicies();

        Passport::routes();
    }
}

这是app.php

'providers' => [

        /*
         * Laravel Framework Service Providers...
         */
        Illuminate\Auth\AuthServiceProvider::class,
        Illuminate\Broadcasting\BroadcastServiceProvider::class,
        Illuminate\Bus\BusServiceProvider::class,
        Illuminate\Cache\CacheServiceProvider::class,
        Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class,
        Illuminate\Cookie\CookieServiceProvider::class,
        Illuminate\Database\DatabaseServiceProvider::class,
        Illuminate\Encryption\EncryptionServiceProvider::class,
        Illuminate\Filesystem\FilesystemServiceProvider::class,
        Illuminate\Foundation\Providers\FoundationServiceProvider::class,
        Illuminate\Hashing\HashServiceProvider::class,
        Illuminate\Mail\MailServiceProvider::class,
        Illuminate\Notifications\NotificationServiceProvider::class,
        Illuminate\Pagination\PaginationServiceProvider::class,
        Illuminate\Pipeline\PipelineServiceProvider::class,
        Illuminate\Queue\QueueServiceProvider::class,
        Illuminate\Redis\RedisServiceProvider::class,
        Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
        Illuminate\Session\SessionServiceProvider::class,
        Illuminate\Translation\TranslationServiceProvider::class,
        Illuminate\Validation\ValidationServiceProvider::class,
        Illuminate\View\ViewServiceProvider::class,

        /*
         * Package Service Providers...
         */

        /*
         * Application Service Providers...
         */
        App\Providers\AppServiceProvider::class,
        App\Providers\AuthServiceProvider::class,
        // App\Providers\BroadcastServiceProvider::class,
        App\Providers\EventServiceProvider::class,
        App\Providers\RouteServiceProvider::class,
        Laravel\Passport\PassportServiceProvider::class,

    ],

这是Handler.php

<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Auth\AuthenticationException;
class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that are not reported.
     *
     * @var array
     */
    protected $dontReport = [
        //
    ];

    /**
     * A list of the inputs that are never flashed for validation exceptions.
     *
     * @var array
     */
    protected $dontFlash = [
        'password',
        'password_confirmation',
    ];

    /**
     * Report or log an exception.
     *
     * @param  \Exception  $exception
     * @return void
     */
    public function report(Exception $exception)
    {
        parent::report($exception);
    }

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Exception  $exception
     * @return \Illuminate\Http\Response
     */
    public function render($request, Exception $exception)
    {
        return parent::render($request, $exception);
    }


}

解决方法:

我遇到了同样的问题,然后添加了Auth :: routes();在web.php的末尾,然后它开始工作.

我不知道它是否正确,但它解决了问题.

另一个原因可能是您没有发送laravel必需的标头,即

'accept' => 'application/json', //it tells server to send only json response
'content-type' => 'application/json'   // it tells front-end app to send data in json format

希望它能解决您的问题

标签:laravel,laravel-5-7,php
来源: https://codeday.me/bug/20191108/2006200.html