简体   繁体   English

如何解决 Laravel 不生成 CSRF 令牌

[英]How to solve Laravel not generating CSRF token

Okay, so I've been using jQuery to connect to a controller function that authenticates data and submits it via AJAX usingthis code . Okay, so I've been using jQuery to connect to a controller function that authenticates data and submits it via AJAX usingthis code . This was working flawlessly until I took a day off on Saturday.直到周六我请了一天假,这一切都完美无缺。 Coming back to the project yesterday I keep encountering this error with HTTP code 419昨天回到项目我一直遇到这个错误 HTTP 代码 419

"message": "CSRF token mismatch.",
"exception": "Symfony\\Component\\HttpKernel\\Exception\\HttpException",
"file": "C:\\workspace\\app_name\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Exceptions\\Handler.php",
"line": 387,

So I tried to echo the CSRF token and it's blank, Keep in mind it was working perfectly Friday then come Sunday (yesterday), with no external input.所以我尝试回显 CSRF 令牌,但它是空白的,请记住它在周五工作得很好,然后到了周日(昨天),没有外部输入。 it just randomly hits me with this?它只是随机地打我这个? What could be the reason for it not generating a token?它不生成令牌的原因可能是什么?

Okay, so I simply added this code quickly to my code just before the workday ended so didn't have time to test hence failed to notice the error.好的,所以我只是在工作日结束之前快速将此代码添加到我的代码中,因此没有时间测试因此未能注意到错误。

$request->session()->flush();

This line of code is responsible for clearing sessions.这行代码负责清除会话。 What it DOESN'T mention in the Laravel docs though is that since CSRF tokens are sessions AS WELL, using this basically makes IO to your databases impossible since it clears ALL sessions including said tokens.不过,它在 Laravel 文档中没有提到的是,由于 CSRF 令牌也是会话,因此使用它基本上会使 IO 无法访问您的数据库,因为它会清除包括所述令牌在内的所有会话。 So until this is resolved (ideally with a code snippet that clears dev-created sessions while sparing inbuilt Laravel ones), avoid using this line of code.因此,在解决此问题之前(理想情况下,使用代码片段清除开发人员创建的会话,同时保留内置的 Laravel 会话),避免使用这行代码。 Instead, use this to clear single sessions:相反,使用它来清除单个会话:

// Forget a single key...
$request->session()->forget('name');

Or this to clear multiple sessions或者这个清除多个会话

// Forget multiple keys...
$request->session()->forget(['name', 'status']);

You can also learn more about this beautiful framework's sessions here .您还可以在此处了解有关这个漂亮框架会话的更多信息。

Lastly, thank you to @Indra Kumar S, @John Lobo, @Manjeet and @Paras Raiyani for taking the time to browse the platform and to offer assistance to others.最后,感谢@Indra Kumar S、@John Lobo、@Manjeet 和 @Paras Rayani 抽出时间浏览该平台并为他人提供帮助。 Will definitely be doing the same.肯定会这样做。

Run below commands:运行以下命令:

php artisan key:generate

and then clear cache然后清除缓存

php artisan cache:clear 

If you want to see if the token value is changing then try below code如果您想查看令牌值是否正在更改,请尝试以下代码

Route::get('/token', function (Request $request) {
    $token = $request->session()->token();
    echo $token;
    $token = csrf_token();
    echo $token;

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM