認證Laravel API路線我已經在現有的Laravel 5.2項目啓動工作。我需要爲應用程序的前端構建一些基本的API請求來與數據庫交談。這些路由需要由用戶的會話進行驗證。由用戶會話
我試着使用身份驗證設置中間件:API驅動程序,並在../config/auth.php設置API [「司機」]到「會話」。但是,即使用戶使用所有權限和角色進行了身份驗證,我也始終將302重定向到登錄頁面。
有人可以推薦一些閱讀或如何基於用戶會話來實現API認證等解決思路?
從路由/ api.php:
Route::group(["middleware" => ["auth:api"]], function() {
// results in 302 redirect to /login
Route::get('test', function(){
return "TEST";
});
});
從配置/ auth.php
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'session',
'provider' => 'users',
],
應用程序/ HTTP/Kernel.php
<?php
namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
{
/**
* The application's global HTTP middleware stack.
*
* These middleware are run during every request to your application.
*
* @var array
*/
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\App\Http\Middleware\NoCache::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\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
'api' => [
'throttle:60,1',
'bindings',
],
];
/**
* 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,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'role' => \Zizaco\Entrust\Middleware\EntrustRole::class,
'permission' => \Zizaco\Entrust\Middleware\EntrustPermission::class,
'ability' => \Zizaco\Entrust\Middleware\EntrustAbility::class,
'dashboard' => \App\Http\Middleware\dashboardMiddleware::class,
'system' => \App\Http\Middleware\systemMiddleware::class
];
}
發佈您的代碼示例。聽起來中間件可能沒有正確設置。也許這可能是你的路由層次結構有點混亂 – idelara
@JackGal我已經爲你添加了一個代碼的副本。 – kirgy
您是否試圖從瀏覽器訪問此類路線? – idelara