Laravel5.2不需要的VerifyCsrfToken

时间:2016-07-15 22:38:06

标签: php routing laravel-5.2 csrf middleware

我在更改后设置了新的L5.2和我的路径文件:

<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/

Route::get('/', function () {
    return view('welcome');
});

Route::group(['middleware' =>'api', 'prefix' => '/api/v1'], function () {
    Route::post('/api/v1/login', 'Api\V1\Auth\AuthController@postLogin');

});

当我去邮递员并发帖时:http://kumarajiva.dev/api/v1/login我得到:TokenMismatchException in VerifyCsrfToken.php line 67

但是我的内核文件看起来像那样:

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,
    ],

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

我什么都不做改变。路线&#39;登录&#39;是在&#39; api&#39; middelware组(不是&#39; web&#39; whereCs是VerifyCsrfToken),但令人惊讶的是我遇到了错误。所以我想知道 - wtf?它有效吗?做网络&#39; middelware组是否总是执行(针对每个请求)?

1 个答案:

答案 0 :(得分:1)

默认情况下,看起来所有路线都包含在“网络”组中。

RouteServiceProvider内有此功能。

    /**
     * Define the "web" routes for the application.
     *
     * These routes all receive session state, CSRF protection, etc.
     *
     * @param  \Illuminate\Routing\Router  $router
     * @return void
     */
    protected function mapWebRoutes(Router $router)
    {
        $router->group([
            'namespace' => $this->namespace, 'middleware' => 'web',
        ], function ($router) {
            require app_path('Http/routes.php');
        });
    }

如果您希望特定的uri不检查CSRF令牌,请转到App\Http\Middleware\VerifyCsrfToken并将uri添加到$except阵列。

您还可以使用CLI和php artisan route:list查看中间件背后的路由。