【问题标题】:Laravel middleware verifyCsrfToken put variable [duplicate]Laravel中间件verifyCsrfToken放置变量[重复]
【发布时间】:2019-08-19 09:23:00
【问题描述】:

我需要为某些引用者禁用 csrf 令牌。我该怎么做?

我试过了:

/**
 * The URIs that should be excluded from CSRF verification.
 *
 * @var array
 */
protected $except = [
    $_SERVER['HTTP_REFERER'] == 'http://example.com' ? '/example' : '',
];

但我得到错误:expression is not allowed as field default value

【问题讨论】:

  • 您不能在类定义中使用表达式。它必须是一个静态值。相反,在 __construct 方法中分配它

标签: php laravel


【解决方案1】:

$except 字段用于从 CSRF 检查中排除指定的 URL。如果您想跳过检查来自特定引用者的请求,则需要扩展您的 VerifyCsrfToken 中间件类并提供新的 handle 方法,如下所示:

/**
 * Handle an incoming request.
 *
 * @param \Illuminate\Http\Request $request
 * @param \Closure                 $next
 *
 * @return mixed
 */
public function handle($request, Closure $next)
{
    // If request comes from specific referer...
    if ($request->headers->get('referer') == 'http://example.com') {
        // ... then we append $except with URL to ignore.
        $this->except[] = '/example';
    }

    // After that we pass the control to original method's implementation
    // that will perform the check as usual.
    return parent::handle($request, $next);
}

【讨论】:

    猜你喜欢
    • 2015-11-14
    • 2016-09-29
    • 1970-01-01
    • 2017-05-24
    • 2015-09-22
    • 2019-04-02
    • 2020-09-13
    • 1970-01-01
    • 2019-03-16
    相关资源
    最近更新 更多