VerifyCsrfToken.php第67行中的TokenMismatchException:Laravel 5.2

时间:2017-04-13 21:58:29

标签: php laravel laravel-5.2 token

有人可以解释一下为什么我有时会遇到这个错误:

TokenMismatchException in VerifyCsrfToken.php line 67:

in VerifyCsrfToken.php line 67
at VerifyCsrfToken->handle(object(Request), object(Closure))
at call_user_func_array(array(object(VerifyCsrfToken), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(ShareErrorsFromSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in StartSession.php line 64
at StartSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(StartSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure))
at call_user_func_array(array(object(AddQueuedCookiesToResponse), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136

有时它工作正常。我在控制器中使用这样的中间件:

    $this->middleware(['admin', 'user'], ['only' => [
        'show',
    ]]);

    $this->middleware('admin', ['only' => [
        'index',
        'store',
        'create',
    ]]);

我在route.php中没有任何中间件,我尝试使用{!! csrf_field()!!}和/或{!! csrf_token()!!}在我的表单中,但有时我仍然会收到错误。

这是登录表格的一个例子

{!! Form::open(['route'=>'log.store', 'method'=>'POST']) !!}
                                {!! csrf_field() !!}
                                {!! csrf_token() !!}
                                {!!Form::text('user',null,array('placeholder' => 'user'))!!}
                                {!!Form::password('password',array('placeholder' => 'Password'))!!}<br>
                                {!!Form::select('logType', [
                                'A' => 'A', 
                                'E' => 'E',
                                ])!!}
                                {!!Form::submit('Log in',['class'=>'button'])!!}
                            {!!Form::close()!!}

1 个答案:

答案 0 :(得分:1)

https://laravelcollective.com/docs/5.2/html

如果您使用带有POST,PUT或DELETE的Form :: open或Form :: model方法,Laravel用于CSRF保护的CSRF令牌将自动作为隐藏字段添加到表单中。

-

{!! Form::open(['route'=>'log.store', 'method'=>'POST']) !!} {!! csrf_field() !!} {!! csrf_token() !!} User'))!!}

尝试更改为

{!! Form::open(['route'=>'log.store', 'method'=>'POST']) !!}

在你提供的代码片段中呈现令牌3次,只需检查HTML即可。仅使用Form::open。多次调用和渲染可能会更改令牌,最终渲染的元素会覆盖前一个但在会话中它可能没有更改。

同时避免过于频繁地发送请求,因为可能会发生不匹配。