在 Laravel 5 中,如何禁用特定路由的 VerifycsrfToken 中间件?

In Laravel 5, How to disable VerifycsrfToken middleware for specific route?(在 Laravel 5 中,如何禁用特定路由的 VerifycsrfToken 中间件?)
本文介绍了在 Laravel 5 中,如何禁用特定路由的 VerifycsrfToken 中间件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在使用 Laravel 5 开发应用程序.我的应用程序与 VendHQ API 连接,我打算通过他们的 webhook 从 VendHQ 获取一些数据.根据他们的文档

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 时,我们将发送一个 POST请求到您选择的 URL.POST 请求将在UTF-8 字符集和 application/x-www-form-urlencoded 编码.

When an event happens and triggers a webhook, we’ll send a POST request to a URL of your choosing. The POST request will be in the UTF-8 charset, and application/x-www-form-urlencoded encoding.

问题是,当他们尝试向我的 Laravel 应用程序发送 POST 请求时,他们的发布请求中没有添加 CSRF 令牌,并且 VerifyCsrfToken 中间件正在寻找令牌,最后它抛出一个 <代码>TokenMismatchException.

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.

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

My question is, how can I avoid this default VerifyCsrfToken Middleware for some specific routes while keeping other post requests active?

推荐答案

Laravel 5 中所有路由默认开启 CSRF,你可以通过修改 app/Http/Middleware/VerifyCsrfToken.php 为特定路由禁用它

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);
  }

来源

这篇关于在 Laravel 5 中,如何禁用特定路由的 VerifycsrfToken 中间件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)