{"id":171,"date":"2023-02-19T19:02:38","date_gmt":"2023-02-19T11:02:38","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=171"},"modified":"2023-02-19T19:02:42","modified_gmt":"2023-02-19T11:02:42","slug":"laravel-csrf-protection","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/19\/laravel-csrf-protection\/","title":{"rendered":"Laravel CSRF Protection"},"content":{"rendered":"<p>\u5b98\u65b9\u6587\u6863\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/laravel.com\/docs\/5.5\/csrf\">https:\/\/laravel.com\/docs\/5.5\/csrf<\/a><\/p>\n<h2>\u5ba2\u6237\u7aef\u8bf7\u6c42\u8bbe\u7f6e<\/h2>\n<p>\u8868\u5355\u6dfb\u52a0\u9690\u85cf\u57df<\/p>\n<p><!-- more --><\/p>\n<pre><code class=\"language-html\">&lt;form method=&quot;POST&quot; action=&quot;\/profile&quot;&gt;\n    {{ csrf_field() }}\n    ...\n&lt;\/form&gt;<\/code><\/pre>\n<p>AJAX\u8bbe\u7f6e<\/p>\n<pre><code class=\"language-html\">&lt;meta name=&quot;csrf-token&quot; content=&quot;{{ csrf_token() }}&quot;&gt;\n\n$.ajaxSetup({\n    headers: {\n        &#039;X-CSRF-TOKEN&#039;: $(&#039;meta[name=&quot;csrf-token&quot;]&#039;).attr(&#039;content&#039;)\n    }\n});<\/code><\/pre>\n<h2>\u8bbe\u7f6e\u6392\u9664CSRF\u8ba4\u8bc1\u7684\u8def\u7531<\/h2>\n<p><code>app\/Http\/Middleware\/VerifyCsrfToken.php<\/code><\/p>\n<pre><code class=\"language-php\">class VerifyCsrfToken extends BaseVerifier\n{\n    \/**\n     * The URIs that should be excluded from CSRF verification.\n     *\n     * @var array\n     *\/\n    protected $except = [\n        &#039;\/api\/*&#039;,\n    ];\n}<\/code><\/pre>\n<h2>\u5173\u95edCSRF\u8ba4\u8bc1<\/h2>\n<p>Laravel\u9ed8\u8ba4\u662f\u5f00\u542f\u4e86CSRF\u529f\u80fd\uff0c\u9700\u8981\u5173\u95ed\u6b64\u529f\u80fd\u6709\u4e24\u79cd\u65b9\u6cd5\uff1a<\/p>\n<h3>\u65b9\u6cd5\u4e00<\/h3>\n<p>\u6253\u5f00\u6587\u4ef6\uff1a<code>app\\Http\\Kernel.php<\/code>\uff0c\u628a\u8fd9\u884c\u6ce8\u91ca\u6389\uff1a<\/p>\n<pre><code class=\"language-php\">&#039;App\\Http\\Middleware\\VerifyCsrfToken&#039;<\/code><\/pre>\n<h3>\u65b9\u6cd5\u4e8c<\/h3>\n<p>\u6253\u5f00\u6587\u4ef6\uff1a<code>app\\Http\\Middleware\\VerifyCsrfToken.php<\/code>\uff0c\u4fee\u6539\u4e3a\uff1a<\/p>\n<pre><code class=\"language-php\">&lt;?php namespace App\\Http\\Middleware;\n\nuse Closure;\nuse Illuminate\\Foundation\\Http\\Middleware\\VerifyCsrfToken as BaseVerifier;\n\nclass VerifyCsrfToken extends BaseVerifier {\n\n    \/**\n     * Handle an incoming request.\n     *\n     * @param  \\Illuminate\\Http\\Request  $request\n     * @param  \\Closure  $next\n     * @return mixed\n     *\/\n    public function handle($request, Closure $next)\n    {\n        \/\/ \u4f7f\u7528CSRF\n        \/\/return parent::handle($request, $next);\n        \/\/ \u7981\u7528CSRF\n        return $next($request);\n    }\n\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5b98\u65b9\u6587\u6863\uff1ahttps:\/\/laravel.com\/docs\/5.5\/csrf \u5ba2\u6237\u7aef\u8bf7\u6c42\u8bbe\u7f6e \u8868\u5355\u6dfb\u52a0\u9690\u85cf\u57df  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[72],"class_list":["post-171","post","type-post","status-publish","format-standard","hentry","category-laravel","tag-laravel"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/171","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=171"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/171\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=171"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=171"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}