LoginSignup
1
3

More than 5 years have passed since last update.

Laravel5でCSRFチェックを外す

Posted at

Laravelはセキュリティも万全なのでPOSTのformにはCSRF対策用のトークンが自動的に埋め込まれる。
これによって不正なPOSTリクエストにはTokenMismatchExceptionを出して処理を止めるようになっている。
しかし、ときにはこれが問題となることもある。
例えばAPIを作って外部からのPOSTリクエストを受け付けようとしたとき、送られてくるフォームデータにCSRFトークンは含まれていない。
なのでTokenMismatchExceptionとなって正常な処理が実行されない。
それだといけないので敢えてCSRFのチェックを外す場合、Laravelではどうするか。
それはミドルウェアのVerifyCsrfTokenに除外するURIを追加すればよい。

app/Http/Middleware/VerifyCsrfToken.php

namespace App\Http\Middleware;

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

class VerifyCsrfToken extends BaseVerifier
{
    /**
     * The URIs that should be excluded from CSRF verification.
     *
     * @var array
     */
    protected $except = [
        '除外するURI',
    ];
}

これでOK:thumbsup_tone1:
なんでも受け付けるようになるので、別に認証の仕組みを作ったほうが安全だと思うがそれはまた別の話。

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3