在 laravel 中为特定路由禁用 csrf

disable csrf in laravel for specific route(在 laravel 中为特定路由禁用 csrf)
本文介绍了在 laravel 中为特定路由禁用 csrf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个支付系统,数据被提交到第 3 方网站然后被拖回......

I've a payment system, where data is submitted to 3rd party site and than hauled back...

当数据返回时,它会访问特定的 url,让我们说/ok 路由.$_REQUEST['transaction'].

When data returns it hits specific url lets say /ok route. $_REQUEST['transaction'].

但是由于 Laravel 中间件,我的令牌不匹配.第三方支付 API 无法生成令牌,那么我如何禁用它?只针对这条路线?

But because of laravel middleware I'm getting token mismatch. There is no way 3rd party payment API can generate token, so how I disable it? only for this route?

或者有更好的选择吗?

Route::get('/payment/ok',   'TransactionsController@Ok');
Route::get('/payment/fail', 'TransactionsController@Fail');

public function Ok( Request $request )
{
    $transId = $request->get('trans_id');

    if ( isset( $transId ) )
    {

        return $transId;

    }

}

推荐答案

5.1 版本开始,LaravelVerifyCsrfToken 中间件允许指定从 CSRF 验证中排除的路由.为了实现这一点,您需要将路由添加到 AppHttpMiddlewareVerifyCsrfToken.php 类中的 $except 数组:

Since version 5.1 Laravel's VerifyCsrfToken middleware allows to specify routes, that are excluded from CSRF validation. In order to achieve that, you need to add the routes to $except array in your AppHttpMiddlewareVerifyCsrfToken.php class:

<?php namespace AppHttpMiddleware;

use IlluminateFoundationHttpMiddlewareVerifyCsrfToken as BaseVerifier;

class VerifyCsrfToken extends BaseVerifier
{
  protected $except = [
    'payment/*',
  ];
}

有关详细信息,请参阅文档.

See the docs for more information.

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

In PHP how can you clear a WSDL cache?(在 PHP 中如何清除 WSDL 缓存?)
failed to open stream: HTTP wrapper does not support writeable connections(无法打开流:HTTP 包装器不支持可写连接)
Stop caching for PHP 5.5.3 in MAMP(在 MAMP 中停止缓存 PHP 5.5.3)
Caching HTTP responses when they are dynamically created by PHP(缓存由 PHP 动态创建的 HTTP 响应)
Memcached vs APC which one should I choose?(Memcached 与 APC 我应该选择哪一个?)
What is causing quot;Unable to allocate memory for poolquot; in PHP?(是什么导致“无法为池分配内存?在 PHP 中?)