【问题标题】:'Illuminate\Session\TokenMismatchException' on Laravel 5Laravel 5 上的“Illuminate\Session\TokenMismatchException”
【发布时间】:2015-09-13 01:27:18
【问题描述】:

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

[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。

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

非常感谢。

【问题讨论】:

  • 您是否通过 ajax 请求表单数据?您的会话是否正常工作?
  • 请回答@DavidDomain 的问题,并附上您的表单代码
  • 您好,是的,会话工作正常,我不使用 AJAX。您是否认为 API 在没有 CSRF 的情况下向我的网站发出 POST 请求?

标签: php debugging laravel-5 csrf


【解决方案1】:

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

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

把这个放到App\Exceptions\Handler::render()

【讨论】:

    【解决方案2】:

    只需运行这些命令

    php artisan clear-compiled
    composer dump-autoload
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

      • 禁用 CSRF 是个糟糕的主意。请看我的回答。
      猜你喜欢
      • 1970-01-01
      • 2016-06-22
      • 2016-03-06
      • 1970-01-01
      • 2015-08-10
      • 2015-05-08
      • 1970-01-01
      • 1970-01-01
      • 2015-09-30
      相关资源
      最近更新 更多