I am using Laravel 5 for developing an app. My app is connected with VendHQ API and I am intended to get some data from VendHQ through their webhook. As per their Documentation

当事件发生并触发webhook时,我们将发送一条帖子

The problem is, when they try to send a POST request to my Laravel app, no CSRF Token is added in their post request and VerifyCsrfToken middleware is looking for a token and finally it throws a TokenMismatchException.

我的问题是,如何在保持其他POST请求处于活动状态的同时避免某些特定路由的默认VerifyCsrfToken中间件?

推荐答案

CSRF is enabled by default on all Routes in Laravel 5, you can disable it for specific routes by modifying app/Http/Middleware/VerifyCsrfToken.php

//app/Http/Middleware/VerifyCsrfToken.php

//add an array of Routes to skip CSRF check
private $openRoutes = ['free/route', 'free/too'];

//modify this function
public function handle($request, Closure $next)
    {
        //add this condition 
    foreach($this->openRoutes as $route) {

      if ($request->is($route)) {
        return $next($request);
      }
    }

    return parent::handle($request, $next);
  }

source

Laravel相关问答推荐

未将Laravel Blade属性传递给组件或类

laravel如何在Blade 模板中将元素添加到数组

如何返回Blade Laravel 中的按钮?

Laravel Debugbar 中的 Laravel api routes路由

Laravel:在失败时处理 findOrFail()

向 Laravel 模型查询添加计算字段

Laravel/Eloquent:致命错误:在非对象上调用成员函数 connection()

如何在 Laravel Passport 中获取刷新令牌?

Laravel 的 APP_ENV 变量(在 .env 文件中找到)是否有建议的值列表?

Laravel 查询构建器 - 如何按别名分组,或进行原始 groupBy

Laravel - 如何恢复本地存储符号链接并刷新

如何在 Laravel Eloquent 中 Select 某些字段?

我如何从给定的日期时间 struct 创建Carbon 对象?

Laravel Eloquent ORM 复制

扩展 Eloquent 的类的构造函数

为什么 BroadCastEvent 在 Laravel 中排队?如何阻止它?

将 Laravel 集合排序为 ID 数组

Laravel X-CSRF-Token 与 POSTMAN 不匹配

登录后 DevTools 无法解析 SourceMap 错误将重定向到该 js 文件

Laravel or where