{"id":412,"date":"2019-09-05T11:30:35","date_gmt":"2019-09-05T03:30:35","guid":{"rendered":"http:\/\/www.cxybj.com\/?p=412"},"modified":"2021-12-20T09:09:33","modified_gmt":"2021-12-20T01:09:33","slug":"laravel%e4%bd%bf%e7%94%a8blade%e6%a8%a1%e6%9d%bf%e5%88%9b%e5%bb%ba%e6%8f%90%e4%ba%a4%e8%a1%a8%e5%8d%95%e6%97%b6post-419-page-expired-%e9%94%99%e8%af%af","status":"publish","type":"post","link":"https:\/\/www.cxybj.com\/?p=412","title":{"rendered":"laravel\u4f7f\u7528Blade\u6a21\u677f\u521b\u5efa\u63d0\u4ea4\u8868\u5355\u65f6POST 419 | Page Expired \u9519\u8bef"},"content":{"rendered":"<div>laravel\u4f7f\u7528Blade\u6a21\u677f\u521b\u5efa\u63d0\u4ea4\u8868\u5355\u65f6POST 419 | Page Expired \u9519\u8bef<\/div>\n<div><\/div>\n<div><a href=\"https:\/\/www.cxybj.com\/wp-content\/uploads\/2019\/09\/QQ\u56fe\u724720190905112841.png\"><img loading=\"lazy\" src=\"https:\/\/www.cxybj.com\/wp-content\/uploads\/2019\/09\/QQ\u56fe\u724720190905112841.png\" alt=\"\" width=\"410\" height=\"220\"><\/a><\/div>\n<div><strong>\u89e3\u51b3\u529e\u6cd5\uff1a<\/strong><\/div>\n<div>\u5c06CSRF\u4fdd\u62a4\u6dfb\u52a0\u5230Blade\u6a21\u677f\u3002<br \/>\n\u5177\u4f53\u6765\u8bf4\uff0c@csrf\u53ea\u9700\u6dfb\u52a0\u3002<\/div>\n<div><a href=\"https:\/\/www.cxybj.com\/wp-content\/uploads\/2019\/09\/QQ\u56fe\u724720190905112950.png\"><img loading=\"lazy\" src=\"https:\/\/www.cxybj.com\/wp-content\/uploads\/2019\/09\/QQ\u56fe\u724720190905112950.png\" alt=\"\" width=\"509\" height=\"257\"><\/a><\/div>\n<div><\/div>\n<div><strong>\u76f8\u5173\u5185\u5bb9\uff1a<\/strong><\/div>\n<div>\n<p>\u8de8\u7ad9\u8bf7\u6c42\u4f2a\u9020\uff08CSRF\uff09\u662f\u4e00\u79cd\u901a\u8fc7\u4f2a\u88c5\u6388\u6743\u7528\u6237\u7684\u8bf7\u6c42\u6765\u653b\u51fb\u6388\u4fe1\u7f51\u7ad9\u7684\u6076\u610f\u6f0f\u6d1e\u3002<\/p>\n<p>Laravel \u901a\u8fc7\u81ea\u5e26\u7684 CSRF \u4fdd\u62a4\u4e2d\u95f4\u4ef6\u8ba9\u907f\u514d\u5e94\u7528\u906d\u5230\u8de8\u7ad9\u8bf7\u6c42\u4f2a\u9020\u653b\u51fb\u53d8\u5f97\u7b80\u5355\uff1aLaravel \u4f1a\u81ea\u52a8\u4e3a\u6bcf\u4e00\u4e2a\u88ab\u5e94\u7528\u7ba1\u7406\u7684\u6709\u6548\u7528\u6237\u4f1a\u8bdd\u751f\u6210\u4e00\u4e2a CSRF \u201c\u4ee4\u724c\u201d\uff0c\u7136\u540e\u5c06\u8be5\u4ee4\u724c\u5b58\u653e\u5728 Session \u4e2d\uff0c\u8be5\u4ee4\u724c\u7528\u4e8e\u9a8c\u8bc1\u6388\u6743\u7528\u6237\u548c\u53d1\u8d77\u8bf7\u6c42\u8005\u662f\u5426\u662f\u540c\u4e00\u4e2a\u4eba\u3002<\/p>\n<p>\u4efb\u4f55\u65f6\u5019\u5728 Laravel \u5e94\u7528\u4e2d\u5b9a\u4e49 HTML \u8868\u5355\uff0c\u90fd\u9700\u8981\u5728\u8868\u5355\u4e2d\u5f15\u5165 CSRF \u4ee4\u724c\u5b57\u6bb5\uff0c\u8fd9\u6837 CSRF \u4fdd\u62a4\u4e2d\u95f4\u4ef6\u624d\u80fd\u591f\u5bf9\u8bf7\u6c42\u8fdb\u884c\u9a8c\u8bc1\u3002\u8981\u60f3\u751f\u6210\u5305\u542b CSRF \u4ee4\u724c\u7684\u9690\u85cf\u8f93\u5165\u5b57\u6bb5\uff0c\u53ef\u4ee5\u4f7f\u7528 Blade \u6307\u4ee4&nbsp;@csrf\uff1a<\/p>\n<pre>&lt;form method=\"POST\" action=\"\/profile\"&gt;\n    @csrf\n    ...\n&lt;\/form&gt;<\/pre>\n<p>\u4e2d\u95f4\u4ef6\u7ec4&nbsp;web&nbsp;\u4e2d\u7684<a href=\"http:\/\/laravelacademy.org\/post\/8739.html\" target=\"_blank\" rel=\"noopener noreferrer\">\u4e2d\u95f4\u4ef6<\/a>&nbsp;VerifyCsrfToken&nbsp;\u4f1a\u81ea\u52a8\u4e3a\u6211\u4eec\u9a8c\u8bc1\u8bf7\u6c42\u8f93\u5165\u7684 token \u503c\u548c Session \u4e2d\u5b58\u50a8\u7684 token \u662f\u5426\u4e00\u81f4\uff0c\u5982\u679c\u6ca1\u6709\u4f20\u9012\u8be5\u5b57\u6bb5\u6216\u8005\u4f20\u9012\u8fc7\u6765\u7684\u5b57\u6bb5\u503c\u548c Session \u4e2d\u5b58\u50a8\u7684\u6570\u503c\u4e0d\u4e00\u81f4\uff0c\u5219\u4f1a\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n<p>\u4e3a\u4e86\u6f14\u793a\u8be5\u529f\u80fd\uff0c\u6211\u4eec\u5728&nbsp;routes\/web.php&nbsp;\u4e2d\u5b9a\u4e49\u4e00\u7ec4\u6d4b\u8bd5\u8def\u7531\uff1a<\/p>\n<pre>Route::get('form_without_csrf_token', function (){\n    return '&lt;form method=\"POST\" action=\"\/hello_from_form\"&gt;&lt;button type=\"submit\"&gt;\u63d0\u4ea4&lt;\/button&gt;&lt;\/form&gt;';\n});\n\nRoute::get('form_with_csrf_token', function () {\n    return '&lt;form method=\"POST\" action=\"\/hello_from_form\"&gt;' . csrf_field() . '&lt;button type=\"submit\"&gt;\u63d0\u4ea4&lt;\/button&gt;&lt;\/form&gt;';\n});\n\nRoute::post('hello_from_form', function (){\n   return 'hello laravel!';\n});\n\n<\/pre>\n<h3 id=\"toc_1\">\u6392\u9664\u6307\u5b9a URL \u4e0d\u505a CSRF \u5b89\u5168\u6821\u9a8c<\/h3>\n<p>\u6709\u65f6\u5019\u6211\u4eec\u9700\u8981\u4ece CSRF \u4fdd\u62a4\u4e2d\u95f4\u4ef6\u4e2d\u6392\u9664\u4e00\u4e9b URL\uff0c\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u4f7f\u7528\u4e86\u7b2c\u4e09\u65b9\u652f\u4ed8\u7cfb\u7edf\uff08\u5982\u652f\u4ed8\u5b9d\u6216\u5fae\u4fe1\u652f\u4ed8\uff09\u6765\u5904\u7406\u652f\u4ed8\u5e76\u7528\u5230\u4ed6\u4eec\u63d0\u4f9b\u7684\u56de\u8c03\u529f\u80fd\uff0c\u8fd9\u65f6\u5019\u5c31\u9700\u8981\u4ece Laravel \u7684 CSRF \u4fdd\u62a4\u4e2d\u95f4\u4ef6\u4e2d\u6392\u9664\u56de\u8c03\u5904\u7406\u5668\u8def\u7531\uff0c\u56e0\u4e3a\u7b2c\u4e09\u65b9\u652f\u4ed8\u7cfb\u7edf\u5e76\u4e0d\u77e5\u9053\u8981\u4f20\u4ec0\u4e48 token \u503c\u7ed9\u6211\u4eec\u5b9a\u4e49\u7684\u8def\u7531\u3002<\/p>\n<p>\u901a\u5e38\u6211\u4eec\u9700\u8981\u5c06\u8fd9\u79cd\u7c7b\u578b\u7684\u8def\u7531\u653e\u5230\u6587\u4ef6&nbsp;routes\/web.php&nbsp;\u4e4b\u5916\uff0c\u6bd4\u5982&nbsp;routes\/api.php\u3002\u4e0d\u8fc7\uff0c\u5982\u679c\u5fc5\u987b\u8981\u52a0\u5230&nbsp;routes\/web.php&nbsp;\u4e2d\u7684\u8bdd\uff0c\u4f60\u4e5f\u53ef\u4ee5\u5728&nbsp;VerifyCsrfToken&nbsp;\u4e2d\u95f4\u4ef6\u4e2d\u5c06\u8981\u6392\u9664\u7684 URL \u6dfb\u52a0\u5230&nbsp;$except&nbsp;\u5c5e\u6027\u6570\u7ec4\uff1a<\/p>\n<pre>&lt;?php\n\nnamespace App\\Http\\Middleware;\n\nuse Illuminate\\Foundation\\Http\\Middleware\\VerifyCsrfToken as Middleware;\n\nclass VerifyCsrfToken extends Middleware\n{\n    \/**\n     * \u4ece CSRF \u9a8c\u8bc1\u4e2d\u6392\u9664\u7684 URL\n     *\n     * @var array\n     *\/\n    protected $except = [\n        'alipay\/*',\n        'http:\/\/example.com\/foo\/bar',\n        'http:\/\/example.com\/foo\/*',\n    ];\n}\n<\/pre>\n<blockquote><p>\u6ce8\uff1a\u8fd0\u884c\u6d4b\u8bd5\u65f6 CSRF \u4e2d\u95f4\u4ef6\u4f1a\u81ea\u52a8\u7981\u6b62\u3002<\/p><\/blockquote>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>laravel\u4f7f\u7528Blade\u6a21\u677f\u521b\u5efa\u63d0\u4ea4\u8868\u5355\u65f6POST 41 &hellip; <a href=\"https:\/\/www.cxybj.com\/?p=412\" class=\"more-link\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":true,"template":"","format":"standard","meta":[],"categories":[135,2],"tags":[],"_links":{"self":[{"href":"https:\/\/www.cxybj.com\/index.php?rest_route=\/wp\/v2\/posts\/412"}],"collection":[{"href":"https:\/\/www.cxybj.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cxybj.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cxybj.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cxybj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=412"}],"version-history":[{"count":10,"href":"https:\/\/www.cxybj.com\/index.php?rest_route=\/wp\/v2\/posts\/412\/revisions"}],"predecessor-version":[{"id":927,"href":"https:\/\/www.cxybj.com\/index.php?rest_route=\/wp\/v2\/posts\/412\/revisions\/927"}],"wp:attachment":[{"href":"https:\/\/www.cxybj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=412"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cxybj.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=412"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cxybj.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=412"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}