有时您可能希望从 CSRF 保护中排除一组 URI。例如,如果您使用 Stripe 处理付款并使用他们的 webhook 系统,则需要将您的 Stripe webhook 处理程序路由从 CSRF 保护中排除,因为 Stripe 不会知道要向您的路由发送什么 CSRF 令牌。
常,您应该将这些类型的路由放在 App\Providers\RouteServiceProvider 应用于 routes/web.php 文件中的所有路由的 Web 中间件组之外。但是,您也可以通过将路由的 URI 添加到 VerifyCsrfToken 中间件的 $except 属性来排除路由:
<?php
namespace App\Http\Middleware;
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as Middleware;
class VerifyCsrfToken extends Middleware
{
/**
* The URIs that should be excluded from CSRF verification.
*
* @var array
*/
protected $except = [
'stripe/*',
'http://example.com/foo/bar',
'http://example.com/foo/*',
];
}
也就是说在特殊的post方式的时候,没有token,或者不想传递token的时候。
例如:
/response
则从文件中的 CSRF 保护中排除/app/Http/Middleware/VerifyCsrfToken.php
;
/** * The URIs that should be excluded from CSRF verification. * * @var array */ protected $except = [ 'response', ];
或者
protected $except = [
'http://www.test.com/response',
];