简体   繁体   English

419 页面在 Laravel 中过期 即使在添加 CSRF 令牌之后

[英]419 Page Expired In Laravel Even after adding CSRF token

I am working on a Laravel 8 Framework, I have added the application on the live Cpanel server and then it started showing below Error:我正在开发一个 Laravel 8 框架,我在实时 Cpanel 服务器上添加了应用程序,然后它开始显示以下错误:

419 PAGE EXPIRED 419 页面过期

I know generally missing CSRF token will be the main issue but in this, I have added the CSRF token, I am using LARAVEl blade syntax so adding LARAVEL blade form syntax the "Token" (CSRF) will get added directly.我知道通常缺少 CSRF 令牌将是主要问题,但在此,我添加了 CSRF 令牌,我使用的是 LARAVEl 刀片语法,因此添加 LARAVEL 刀片形式语法将直接添加“令牌”(CSRF)。

 {{ Form::open( [ "url" => \URL::route("front.login.check"), "autocomplete"=>false,"id" => "login_form" ] ) }}

This will add the CSRF automatically, I have tried adding directly, But every POST request end up on the 419 PAGE EXPIRED page.这将自动添加 CSRF,我试过直接添加,但是每个POST请求最终都会出现在419 PAGE EXPIRED页面上。

What do I have checked already?我已经检查了什么?

  • CSRF Token Is not missing in the Form Form中没有丢失CSRF Token
  • I have checked middleware also but this request did not reach the middleware after form submit it will take to the 419 page我也检查了中间件,但是这个请求在表单提交后没有到达中间件,它将带到419页面
  • Also try to php artisan cache:clear and dump-autoload command but the issue is still.也尝试php artisan cache:cleardump-autoload命令,但问题仍然存在。
  • Added 755 permission to storage , vendor and cache folder also.还为storagevendorcache文件夹添加了755权限。

Please help me on this What next should I need to check for solve this issue?请帮我解决这个问题接下来我需要检查什么来解决这个问题?

Laravel "419 Page Expired" Error Troubleshooting Steps Laravel “419 页面已过期”错误故障排除步骤

Apply/go through all steps up to "step 12" BEFORE testing your application for this error.在测试您的应用程序是否存在此错误之前,应用/执行直到“第 12 步”的所有步骤。

  1. Increase your session expiration time (Ie 24 hours).增加你的session 到期时间(即 24 小时)。
  2. Make sure that the " session domain " is the same as the "app URL".确保“ session 域”与“应用程序 URL”相同
  3. Ensure that the session cookies are sent back to the server for both "HTTP" & "HTTPS" browser connections .确保将 session cookies 发送回服务器以用于“HTTP”和“HTTPS”浏览器连接

.env file contents applying the above 3 steps. .env 文件内容应用上述 3 个步骤。

Change myapp.local to your application domain.将 myapp.local更改为您的应用程序域。

APP_URL="http://myapp.local"
SESSION_LIFETIME=1440
SESSION_DOMAIN=myapp.local
SESSION_SECURE_COOKIE=false
  1. Make sure you submit a CSRF token along with your ( PUT / POST / DELETE /etc.) HTTP requests.确保您提交 CSRF 令牌以及您的( PUT / POST / DELETE /等)HTTP 请求。
  • (Ie: Ensure that this request parameter is submitted along with your HTML form requests <input type="hidden" name="_token" value="{{ csrf_token() }}" /> ). (即:确保此请求参数与您的 HTML 表单请求<input type="hidden" name="_token" value="{{ csrf_token() }}" /> )。
  • If in case you make AJAX requests in your application, you may configure ALL AJAX requests to send the CSRF token at all times.如果您在应用程序中发出 AJAX 请求,您可以配置所有 AJAX 请求以始终发送 CSRF 令牌。
    • Add this " <meta> tag" inside the <head> tag of all your master VIEW templates/layouts.在所有主视图模板/布局的<head>标签内添加此“ <meta>标签”。 Ie: resources/views/layouts/app.blade.php and resources/views/layouts/guest.blade.php and resources/views/welcome.blade.php即: resources/views/layouts/app.blade.phpresources/views/layouts/guest.blade.phpresources/views/welcome.blade.php
      • <meta name="csrf-token" content="{{ csrf_token() }}">
    • Then, define the required HTTP request headers and recompile your app's static assets ( npm run dev ).然后,定义所需的 HTTP 请求标头并重新编译您应用的 static 资产( npm run dev )。 resources/js/app.js资源/js/app.js
$.ajaxSetup({
    headers: {
        "X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content"),
        "X-Requested-With": "XMLHttpRequest"
    }
});
  1. Regenerate your application key automatically.自动重新生成您的应用程序密钥 (Ie: php artisan key:generate ). (即: php artisan key:generate )。
  2. Clear your application cache.清除您的应用程序缓存。 (Ie: php artisan cache:clear ). (即: php artisan cache:clear )。
  3. Confirm that the application caller has read & write permissions in the application's "sessions" & "cache" folder.确认应用程序调用者在应用程序的“会话”和“缓存”文件夹中具有读写权限。 (Ie: chmod -R 755 "storage/framework/sessions" && chmod -R 755 "bootstrap/cache" ). (即: chmod -R 755 "storage/framework/sessions" && chmod -R 755 "bootstrap/cache" )。

Addendum 1:附录 1:

  1. If in case you have Laravel Sanctum installed and enabled, add your application domain among the whitelist of "sanctum stateful domains".如果您安装并启用了Laravel Sanctum ,请将您的应用程序域添加到“sanctum stateful domains”的白名单中。

.env file contents .env 文件内容

Change myapp.local to your application domain.将 myapp.local更改为您的应用程序域。

SANCTUM_STATEFUL_DOMAINS="myapp.local"

Addendum 2:附录 2:

  1. Ensure that your "session driver" isn't empty.确保您的“会话驱动程序”不为空。 The default value is "file" . 默认值为“文件”

.env file contents .env 文件内容

SESSION_DRIVER=file

Addendum 3:附录 3:

  1. Disable the browser cache .禁用浏览器缓存 This may be beneficial during your development process.这在您的开发过程中可能是有益的。

  2. Open your web browser, navigate to your application's home page, reload the current page, ignoring cached content.打开您的 web 浏览器,导航到您的应用程序主页,重新加载当前页面,忽略缓存内容。 (Ie: On Windows : Shift + F5 or Ctrl + Shift + r and on Mac : ⌘ + Shift + r ). (即:在Windows 上Shift + F5Ctrl + Shift + r ,在Mac上: ⌘ + Shift + r )。

  3. TEST YOUR APP!测试您的应用程序! Check if you still receive the error.检查您是否仍然收到错误。

Addendum 4 (Optional):附录 4(可选):

Only perform the steps below if you reached step 12 and are still having the same error.如果您到达第 12 步并且仍然遇到相同的错误,请仅执行以下步骤。

A. Clear ALL web browser cache & cookies . A. 清除所有 web 浏览器缓存和 cookies TEST YOUR APP!测试您的应用程序!

B. Open an entirely different web browser and test again. B. 打开一个完全不同的 web 浏览器并再次测试。 If you've been using Google Chome / Safari all along, try testing using Firefox. TEST YOUR APP!如果您一直在使用 Google Chome / Safari,请尝试使用 Firefox 进行测试。测试您的应用程序!

C. Restart your computer and test again. C,重启电脑再测试。 TEST YOUR APP!测试您的应用程序!

1- php artisan route:clear 1- php artisan route:clear

2- go to CSRF middleware and try to add "*" to your except array 2- go 到 CSRF 中间件并尝试将“*”添加到您的 except 数组

When this happened to me it was because I forgot to add 'name' attribute to my input in the form当这发生在我身上时,是因为我忘记在表单中的输入中添加“名称”属性

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

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