【问题标题】:Invalid CSRF token Symfony 5.4无效的 CSRF 令牌 Symfony 5.4
【发布时间】:2021-12-13 19:48:56
【问题描述】:

我有一个 Symfony 5.4,在开发环境(docker)中登录工作正常。但是当我通过 docker on prod 尝试相同的登录时,我有:

{"message":"Invalid CSRF token."}

AppAuthenticator.php

public function authenticate(Request $request): Passport
{
.....
return new Passport(
            new UserBadge($credentials['username']),
            new PasswordCredentials($credentials['password']),
            [new CsrfTokenBadge('authenticate', $credentials['csrf_token'])]
        );
}

security.yaml

security:
  firewalls:
    main:
       form_login:
          login_path: _security_login
          check_path: _security_check
          enable_csrf: true

login.html.twig

<form method="post">
   ....
   <input type="hidden" name="_csrf_token" value="{{ csrf_token('authenticate') }}" />
</form>

【问题讨论】:

标签: php symfony token csrf


【解决方案1】:

确保$credentials['csrf_token'] 等于$request-&gt;request-&gt;get('_csrf_token')

【讨论】:

  • 虽然此代码可能会解决问题,including an explanation 关于如何以及为什么解决问题将真正有助于提高您的帖子质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人。请edit您的回答添加解释并说明适用的限制和假设。
猜你喜欢
  • 2020-05-19
  • 1970-01-01
  • 1970-01-01
  • 2023-02-20
  • 2019-11-20
  • 1970-01-01
  • 2020-01-02
  • 1970-01-01
  • 2021-10-13
相关资源
最近更新 更多