会话到期时抛出TokenMismatchException

时间:2017-08-10 14:49:28

标签: php laravel session

会话设置为 1分钟
我正在使用Laravel&我在表单中添加了{!! csrf_field() !!}

场景
我正在登录页面...
会话到期后1分钟后我尝试使用正确的用户名和密码登录...抛出ERROR说TokenMismatchException并且该错误以local.ERROR身份登录

local.ERROR: Illuminate\Session\TokenMismatchException in /home/dchauhan/projects/zonehawk/user-portal/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php:67 Stack trace: #0 [internal function]: Illuminate\Foundation\Http\Middleware\VerifyCsrfToken->handle(Object(Illuminate\Http\Request), Object(Closure)) #1 /home/dchauhan/projects/zonehawk/user-portal/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(136): call_user_func_array(Array, Array) #2 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request)) #3 /home/dchauhan/projects/zonehawk/user-portal/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(32): call_user_func(Object(Closure), Object(Illuminate\Http\Request)) #4 /home/dchauhan/projects/zonehawk/user-portal/vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php(49): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request)) #5 [internal function]: Illuminate\View\Middleware\ShareErrorsFromSession->handle(Object(Illuminate\Http\Request), Object(Closure)) #6 /home/dchauhan/projects/zonehawk/user-portal/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(136): call_user_func_array(Array, Array) #7 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request)) #8 /home/dchauhan/projects/zonehawk/user-portal/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(32): call_user_func(Object(Closure), Object(Illuminate\Http\Request)) #9 /home/dchauhan/projects/zonehawk/user-portal/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php(62): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request)) #10 [internal function]: Illuminate\Session\Middleware\StartSession->handle(Object(Illuminate\Http\Request), Object(Closure)) #11 /home/dchauhan/projects/zonehawk/user-portal/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(136): call_user_func_array(Array, Array) #12 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request)) #13 /home/dchauhan/projects/zonehawk/user-portal/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(32): call_user_func(Object(Closure), Object(Illuminate\Http\Request)) #14 /home/dchauhan/projects/zonehawk/user-portal/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/AddQueuedCookiesToResponse.php(37): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request)) #15 [internal function]: Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse->handle(Object(Illuminate\Http\Request), Object(Closure)) #16 /home/dchauhan/projects/zonehawk/user-portal/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(136): call_user_func_array(Array, Array) #17 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request)) #18 /home/dchauhan/projects/zonehawk/user-portal/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(32): call_user_func(Object(Closure), Object(Illuminate\Http\Request)) #19 /home/dchauhan/projects/zonehawk/user-portal/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php(59): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request)) #20 [internal function]: Illuminate\Cookie\Middleware\EncryptCookies->handle(Object(Illuminate\Http\Request), Object(Closure)) #21 /home/dchauhan/projects/zonehawk/user-portal/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(136): call_user_func_array(Array, Array) #22 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request)) #23 /home/dchauhan/projects/zonehawk/user-portal/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(32): call_user_func(Object(Closure), Object(Illuminate\Http\Request)) #24 [internal function]: Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request)) #25 /home/dchauhan/projects/zonehawk/user-portal/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(102): call_user_func(Object(Closure), Object(Illuminate\Http\Request)) #26 /home/dchauhan/projects/zonehawk/user-portal/vendor/laravel/framework/src/Illuminate/Routing/Router.php(726): Illuminate\Pipeline\Pipeline->then(Object(Closure)) #27 /home/dchauhan/projects/zonehawk/user-portal/vendor/laravel/framework/src/Illuminate/Routing/Router.php(699): Illuminate\Routing\Router->runRouteWithinStack(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request)) #28 /home/dchauhan/projects/zonehawk/user-portal/vendor/laravel/framework/src/Illuminate/Routing/Router.php(675): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request)) #29 /home/dchauhan/projects/zonehawk/user-portal/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(246): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request)) #30 [internal function]: Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}(Object(Illuminate\Http\Request)) #31 /home/dchauhan/projects/zonehawk/user-portal/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(52): call_user_func(Object(Closure), Object(Illuminate\Http\Request)) #32 /home/dchauhan/projects/zonehawk/user-portal/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php(44): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request)) #33 [internal function]: Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure)) #34 /home/dchauhan/projects/zonehawk/user-portal/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(136): call_user_func_array(Array, Array) #35 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request)) #36 /home/dchauhan/projects/zonehawk/user-portal/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(32): call_user_func(Object(Closure), Object(Illuminate\Http\Request)) #37 [internal function]: Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request)) #38 /home/dchauhan/projects/zonehawk/user-portal/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(102): call_user_func(Object(Closure), Object(Illuminate\Http\Request)) #39 /home/dchauhan/projects/zonehawk/user-portal/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(132): Illuminate\Pipeline\Pipeline->then(Object(Closure)) #40 /home/dchauhan/projects/zonehawk/user-portal/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(99): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request)) #41 /home/dchauhan/projects/zonehawk/user-portal/public/index.php(53): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request)) #42 {main} [] []


我还在 app / Exceptions / Handler.php

中添加了以下代码
public function render($request, Exception $e)
{
    if ($e instanceof \ErrorException) {
        return response()->view('errors.500', [], 500);
    }

    if ($e instanceof TokenMismatchException) {
        return redirect()->back()->with('flash_error' , trans('general.token_mismatch_message'));
    }

    return parent::render($request, $e);
}


结论
我怎么能:

  • 将TokenMismatchException记录为警告而不是ERROR
  • 我应该在 handler.php 中进行哪些更改,或者说任何其他文件以确保我在UI上显示相应的消息,即它是否为TokenMismatchException或sesion已过期

任何帮助将不胜感激。谢谢。

0 个答案:

没有答案