Symfony 5 登录表单操作返回无效的 CSRF 令牌
Posted
技术标签:
【中文标题】Symfony 5 登录表单操作返回无效的 CSRF 令牌【英文标题】:Symfony 5 Login form action return Invalid CSRF token 【发布时间】:2021-01-01 17:04:52 【问题描述】:我正在使用 symfony 5 的安全组件来创建身份验证系统,我使用了 symfony cli 的 make:auth 并在注册表单后启用了登录,当注销并尝试使用登录表单登录时,它返回 Invalid CSRF token 错误!
这是我的登录表单:
% extends 'base.html.twig' %
% block title %Log in!% endblock %
% block body %
<div class="container">
<br>
<div class="row justify-content-center" >
<form method="post">
% if error %
<div class="alert alert-danger"> error.messageKey|trans(error.messageData, 'security') </div>
% endif %
% if app.user %
<div class="mb-3">
You are logged in as app.user.username , <a href=" path('app_logout') ">Logout</a>
</div>
% endif %
<div class="card">
<article class="card-body">
<a href=" path('app_register') " class="float-right btn btn-outline-primary">S'inscrire</a>
<h4 class="card-title mb-4 mt-1">Connection</h4>
<p>
<a href="" class="btn btn-block btn-outline-info"> <i class="fab fa-twitter"></i> Login via Twitter</a>
<a href="" class="btn btn-block btn-outline-primary"> <i class="fab fa-facebook-f"></i> Login via facebook</a>
</p>
<hr>
<form>
<div class="form-group">
<input type="email" value=" last_username " name="email" id="inputEmail" class="form-control" required autofocus>
</div> <!-- form-group// -->
<div class="form-group">
<input type="password" name="password" id="inputPassword" class="form-control" required>
</div> <!-- form-group// -->
<div class="row">
<div class="col-md-12">
<div class="form-group">
<button type="submit" class="btn btn-primary btn-block"> Login </button>
</div> <!-- form-group// -->
</div>
</div> <!-- .row// -->
<div class="row">
<div class="col-md-12 mt-5">
<a href=" path('app_forgot_password_request') " class="small">Mot de passe oubliée</a>
</div>
</div>
<div class="checkbox mb-3">
<label>
<input type="checkbox" name="_remember_me"> Remember me
</label>
</div>
</form>
</article>
</div>
</form>
</div>
</div>
% endblock %
/**
* @Route("/login", name="app_login")
* @param AuthenticationUtils $authenticationUtils
* @return Response
*/
public function login(AuthenticationUtils $authenticationUtils): Response
// if ($this->getUser())
// return $this->redirectToRoute('target_path');
//
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('security/login.html.twig', ['last_username' => $lastUsername, 'error' => $error]);
我不明白登录系统是如何在注册表格后直接工作的,但在登录操作中却没有
【问题讨论】:
您的登录模板中没有 csrf 令牌input
,但生成的身份验证器需要一个。检查身份验证器类和docs 以找出名称。
这能回答你的问题吗? Symfony 4 - how to add csrf token without building form?
【参考方案1】:
在security.yml中:
config/packages/security.yaml 安全: # ...
firewalls:
secured_area:
# ...
form_login:
# ...
csrf_token_generator: security.csrf.token_manager
并尝试在表单末尾添加
<input type="hidden" name="_csrf_token" value="
csrf_token('authenticate') ">
【讨论】:
以上是关于Symfony 5 登录表单操作返回无效的 CSRF 令牌的主要内容,如果未能解决你的问题,请参考以下文章
Symfony3 中的错误“CSRF 令牌无效。请尝试重新提交表单”
直接访问登录表单时未设置会话 cookie,导致 CSRF 令牌无效
Symfony 5 / 使用 API 编写自定义身份验证总是返回我无效的凭据