Laravel 5 Auth Post Submit - VerifyCsrfToken.php 第 46 行中的 TokenMismatchException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28875788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:07:20  来源:igfitidea点击:

Laravel 5 Auth Post Submit - TokenMismatchException in VerifyCsrfToken.php line 46

phpformslaravel-5

提问by trenthogan

Have just statred a new app in Laravel 5 and I am having some trouble using the out of the box auth...

刚刚在 Laravel 5 中开发了一个新应用程序,我在使用开箱即用的身份验证时遇到了一些问题...

I keep getting : TokenMismatchException in VerifyCsrfToken.php line 46: on submitting the login or signup forms...

我不断收到:VerifyCsrfToken.php 第 46 行中的 TokenMismatchException:在提交登录或注册表单时...

I can see on the login form page the token codes that are in the hidden form field and Session at that point are the same...

我可以在登录表单页面上看到隐藏表单字段中的令牌代码和当时的会话是相同的......

As a test I have also tried as some other posts suggested commenting out //'App\Http\Middleware\VerifyCsrfToken', in app/Http/kernal.php to see what would happen. After doing this every time I submit a form I get a message which says redirecting to: /auth/login or /auth/register depending on where I came from with no success.

作为测试,我也尝试过其他一些帖子建议在 app/Http/kernal.php 中注释掉 //'App\Http\Middleware\VerifyCsrfToken' 以查看会发生什么。每次提交表单后,我都会收到一条消息,指出重定向到:/auth/login 或 /auth/register,具体取决于我来自哪里,但没有成功。

The weird thing was this was working when I first installed the framework. All I have done since then is run a few migrations and setup some of my models and controllers and seeded the db with some user data.

奇怪的是,当我第一次安装框架时,这正在工作。从那以后我所做的就是运行一些迁移并设置我的一些模型和控制器,并用一些用户数据为数据库播种。

UPDATE:

更新:

Looking into this further in the function tokensMatch() on line 55 of VerifyCsrfToken.php if I :

如果我:

var_dump($request->session()->token());

var_dump($request->input('_token'));

I can see the two tokens are different but at the form using:

我可以看到这两个令牌是不同的,但在形式上使用:

var_dump(Session::all());

{{{ csrf_token() }}}

They are the same. The Session token has changed some how before it gets to the function tokensMatch() on line 55 of VerifyCsrfToken.php

他们是一样的。会话令牌在到达 VerifyCsrfToken.php 的第 55 行的函数 tokensMatch() 之前发生了一些变化

My stack trace is as follows:

我的堆栈跟踪如下:

in VerifyCsrfToken.php line 46
at VerifyCsrfToken->handle(object(Request), object(Closure)) in VerifyCsrfToken.php line 17
at VerifyCsrfToken->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in ShareErrorsFromSession.php line 55
at ShareErrorsFromSession->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in StartSession.php line 61
at StartSession->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 36
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in EncryptCookies.php line 40
at EncryptCookies->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in CheckForMaintenanceMode.php line 42
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 101
at Pipeline->then(object(Closure)) in Kernel.php line 111
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 84
at Kernel->handle(object(Request)) in index.php line 53

回答by Mahmoud Nassar

I first just got it working removing the line:

我首先让它工作,删除该行:

'Illuminate\Foundation\Http\Middleware\VerifyCsrfToken'

'Illuminate\Foundation\Http\Middleware\VerifyCsrfToken'

from /app/Http/Resquests/Kernel.php. However, this means the CSRF token checkwill be removed, which implies that your website will not be protected from cross-site request forgeries.

来自 /app/Http/Resquests/Kernel.php。但是,这意味着CSRF 令牌检查将被删除,这意味着您的网站将不会受到跨站点请求伪造的保护。

UpdateAccording to the documentation, you should add the CSRF token to your form by adding this snippet to your code:

更新根据文档,您应该通过将此代码段添加到您的代码中来将 CSRF 令牌添加到您的表单中:

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

I used first way in backend services for mobile application but I find I can send send CSRF header within requests.

我在移动应用程序的后端服务中使用了第一种方式,但我发现我可以在请求中发送 CSRF 标头。

回答by Javier dc

According to documentation may be why:

根据文档可能是原因:

Insert The CSRF Token Into A Form

将 CSRF 令牌插入表单

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

回答by Phoenix1331

I had the same issue. I solved it by changing the following line in config/session.php

我遇到过同样的问题。我通过更改 config/session.php 中的以下行来解决它

'domain' => env('DOMAIN', 'yourdomainnamehere.co.uk'),

Then add the following line in you .env

然后在你的 .env 中添加以下行

DOMAIN=null

回答by Myone

Check your routes.phpfile. I also had this error and it turned out to be caused by a blank line at the top (just before the opening <?phptag). Such a stupid error, hopefully this could help someone.

检查您的routes.php文件。我也有这个错误,结果证明是由顶部的空行引起的(就在开始<?php标签之前)。如此愚蠢的错误,希望这可以帮助某人。

回答by Xabir

I had the same problem, my solution was

我遇到了同样的问题,我的解决方案是

<form method="POST" action="path_to_action">
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
    <input type="text" name="some_name">
</form>

回答by Organic Advocate

If you are using blade templates you can use put in your form

如果您使用刀片模板,则可以在表单中使用

{{ csrf_field() }}

instead of

代替

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

It worked with me in Laravel 5.1.

它在 Laravel 5.1 中与我一起工作。

回答by Chandz

If you want to get rid of TokenMismatchException in VerifyCsrfToken.php, check this link for simple solution by @Tariq Khan: TokenMismatchException in VerifyCsrfToken.php

如果您想摆脱 VerifyCsrfToken.php 中的 TokenMismatchException,请查看此链接以获取@Tariq Khan 的简单解决方案: VerifyCsrfToken.php 中的 TokenMismatchException

回答by msonowal

Just Log out and re login thats the only way It's a unknown bug even I get it in my form posting sometimes but all people in the forums they all say for putting but that does not solve the problem just logout and re-login

只是注销并重新登录这是唯一的方法这是一个未知的错误,即使我有时会在我的表单中发布它,但论坛中的所有人都说要放置,但这并不能解决问题,只需注销并重新登录

回答by Frank Liu

Interestingly, I encounter the similar problem recently. I found there're two different tokens generated by my Laravel 5.1 app. I tackled the issue by generating a new application key [php artisan key:generate]!

有趣的是,我最近遇到了类似的问题。我发现我的 Laravel 5.1 应用程序生成了两个不同的令牌。我通过生成一个新的应用程序密钥 [php artisan key:generate] 解决了这个问题!

回答by khatri rohan

goto file called ... VrifyCsrfToken.php . located at app/Http/Middleware/

goto 文件名为 ... VrifyCsrfToken.php 。位于 app/Http/Middleware/

folder.

文件夹。

and change following....

并更改以下....

namespace App\Http\Middleware;

命名空间 App\Http\Middleware;

use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as BaseVerifier;

使用 Illuminate\Foundation\Http\Middleware\VerifyCsrfToken 作为 BaseVerifier;

class VerifyCsrfToken extends BaseVerifier {

类 VerifyCsrfToken 扩展了 BaseVerifier {

protected $except = [
    "*" .   //make * here . as is did.
];

}

}