【Laravel】TokenMismatchExceptionのエラーが出た
概要
LaravelをAPIサーバーとして利用したが、POST時にTokenMismatchException
と出てきたのでメモ。
対応方法
CSRFのチェックを外すためにapp\http\Middleware\VerifyCsrfToken.php
の中に以下を記載。
protected $except = [ 'api/*' ];
しかしこれはセキュアではないためしっかりとCSRFの対策をしなければいけない。 このWeb APIってCSRF対策出来てますか?って質問にこたえよう を読むとどういったAPIサーバにおけるCSRFの対策の仕方がわかる。
参考
https://stackoverflow.com/questions/37383165/tokenmismatchexception-for-api-in-laravel-5-2-31 https://qiita.com/rana_kualu/items/3f9d0d6b9a363fd2108e https://qiita.com/maruloop/items/e14d02299bd136f4b1fc