'Illuminate\Session\TokenMismatchException' 在 Laravel 5

'Illuminate\Session\TokenMismatchException' on Laravel 5

我遇到了很多这样的错误:

[2015-06-25 18:27:00] production.ERROR: exception 'Illuminate\Session\TokenMismatchException' in /home/xxx/public_html/vendor/compiled.php:2550
Stack trace:
#0 /home/xxx/public_html/app/Http/Middleware/VerifyCsrfToken.php(17): Illuminate\Foundation\Http\Middleware\VerifyCsrfToken->handle(Object(Illuminate\Http\Request), Object(Closure))
#1 /home/xxx/public_html/vendor/compiled.php(9197): App\Http\Middleware\VerifyCsrfToken->handle(Object(Illuminate\Http\Request), Object(Closure))
#2 /home/xxx/public_html/vendor/compiled.php(12377): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#3 /home/xxx/public_html/vendor/compiled.php(9197): Illuminate\View\Middleware\ShareErrorsFromSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#4 /home/xxx/public_html/vendor/compiled.php(11067): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#5 /home/xxx/public_html/vendor/compiled.php(9197): Illuminate\Session\Middleware\StartSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#6 /home/xxx/public_html/vendor/compiled.php(12079): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#7 /home/xxx/public_html/vendor/compiled.php(9197): Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse->handle(Object(Illuminate\Http\Request), Object(Closure))
#8 /home/xxx/public_html/vendor/compiled.php(12027): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#9 /home/xxx/public_html/vendor/compiled.php(9197): Illuminate\Cookie\Middleware\EncryptCookies->handle(Object(Illuminate\Http\Request), Object(Closure))
#10 /home/xxx/public_html/vendor/compiled.php(2589): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#11 /home/xxx/public_html/vendor/compiled.php(9197): Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure))
#12 /home/xxx/public_html/vendor/platform/installer/src/Middleware/Installer.php(57): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#13 /home/xxx/public_html/vendor/compiled.php(9197): Platform\Installer\Middleware\Installer->handle(Object(Illuminate\Http\Request), Object(Closure))
#14 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#15 /home/xxx/public_html/vendor/compiled.php(9188): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#16 /home/xxx/public_html/vendor/compiled.php(1996): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#17 /home/xxx/public_html/vendor/compiled.php(1983): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#18 /home/xxx/public_html/public/index.php(68): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#19 {main}  

我无法准确地找到它们的来源,因为该网站的访问量很大。

我在每一页的顶部都有这个:

<meta name="csrf-token" content="{{ csrf_token() }}">

我所有的表格都有这个:

<input type="hidden" name="_token" value="{{ csrf_token() }}">

我不想像人们在其他帖子中建议的那样删除 CSRF。

你知道我如何调试它以找出错误的来源吗?只是寻找有助于找到错误的调试想法。

非常感谢。

好的,这些错误似乎源自外界对我网站的 POST 请求。现在我已经禁用了 CSRF 但计划实施这样的解决方案: http://www.camroncade.com/disable-csrf-for-specific-routes-laravel-5/

只需运行这些命令

php artisan clear-compiled
composer dump-autoload

您可以像这样捕获异常并闪现用户:

    if ($exception instanceof TokenMismatchException) {
        return response()->redirect('login')->with('status', 'Token expired, please try again.');
    }

将其放入 App\Exceptions\Handler::render()