Forms 在Symfony 4上验证用户身份的正确方法
我正在与Symfony身份验证进行斗争。我读过很多手册,但没有结果。我想了解如何使用Forms 在Symfony 4上验证用户身份的正确方法,forms,symfony,twig,bootstrap-4,Forms,Symfony,Twig,Bootstrap 4,我正在与Symfony身份验证进行斗争。我读过很多手册,但没有结果。我想了解如何使用bootstrap\u 4\u layout.html.twig正确呈现登录表单以显示错误。因为当我尝试登录时,它会显示一条丑陋的消息无效凭据。 因此,我的UserLoginType是: class UserLoginType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $op
bootstrap\u 4\u layout.html.twig
正确呈现登录表单以显示错误。因为当我尝试登录时,它会显示一条丑陋的消息无效凭据
。
因此,我的UserLoginType
是:
class UserLoginType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array
$options)
{
$builder
->add('username', TextType::class,
['constraints' => array(new Length(array('min' => 3)))])
->add('password', PasswordType::class)
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => User::class,
));
}
}
我的security.yaml
文件与文档中的文件完全相同:
当然还有其他字段名(因为我使用symfony表单进行渲染),
下面是我的测试登录方法:
public function login(Request $request, AuthenticationUtils $authenticationUtils)
{
$error = $authenticationUtils->getLastAuthenticationError();
$lastUsername = $authenticationUtils->getLastUsername();
$form = $this->createForm(UserLoginType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
dump($form->getData());
die();
}
return $this->render('Security/login.html.twig',
['form' => $form->createView(),
'last_username' => $lastUsername,
'error' => $error,
]);
}
这里是我的login.html.twig
:
{% extends 'layout.html.twig' %}
{% block title %}Login page{% endblock %}
{% block description %}This is login page{% endblock %}
{% form_theme form 'bootstrap_4_layout.html.twig' %}
{% block content %}
<div class="container">
{{ form_start(form) }}
{{ form_widget(form) }}
<input class="btn btn-primary" type="submit" value="Login" />
{{ form_end(form) }}
</div>
{% endblock %}
{%extends'layout.html.twig%}
{%block title%}登录页面{%endblock%}
{%block description%}这是登录页{%endblock%}
{%form_主题表单'bootstrap_4_layout.html.twig%}
{%block content%}
{{form_start(form)}}
{{form_widget(form)}
{{form_end(form)}}
{%endblock%}
因此,当我尝试登录时:
我怎样才能得到这样的东西:
您将收到“无效凭证”消息,因为Symfony处理身份验证,您不需要自己处理表单。从文档中:
不要让这个控制器迷惑你。稍后您将看到,当用户提交表单时,安全系统会自动为您处理表单提交。如果用户提交了无效的用户名或密码,此控制器将从安全系统读取表单提交错误,以便将其显示回用户
为什么在登录表单上会出现“用户名应为3个字符或更长”这样的错误?我认为登录表单不需要这些错误消息
不过,您可以通过以下方式自己验证请求:
$errors = $validator->validate($user);
然后将错误传递给您的视图或在表单中设置错误。框架的美妙之处,尤其是像Symfony这样令人敬畏的框架,在于它们为您带来了很多“魔力”。需要登录吗?将带有字段的post表单端点指向,例如/login/check。需要验证表单吗?只是添加一个约束?太美了。这种美丽也可能成为一个问题,当你试图把你的头围绕什么是错误的。验证用户身份的方法有很多种。我几乎总是喜欢手动操作。像这样:-
// To-DO, get Username and Password from request
if (!$username || !$password) {
throw error;
}
// Query DB for user example using.
$user = $em->findUserByUsernameOrEmail($username);
if (!$user) {
throw 404 error;
}
// The juice of it
// Let get the password encoder and encode the submitted password to see if it matches
$encoder_service = $this->get('security.encoder_factory');
$encoder = $encoder_service->getEncoder($user);
if (!$encoder->isPasswordValid($user->getPassword(), $password, $user->getSalt())) {
throw bad password exception;
}
// We're good to go, the user with the name and password was found in the DB so let's log him/her in
// Get the security firewall name, and login the user
$providerKey = 'firewall_name'; // e.g main
$token = new UsernamePasswordToken($user, $password, $providerKey, $user->getRoles());
$this->get("security.token_storage")->setToken($token);
// Fire the login event to notify any listeners
$event = new InteractiveLoginEvent($request, $token);
$this->get("event_dispatcher")->dispatch("security.interactive_login", $event);
// Continue your logic
正如我所说,给这只猫剥皮有很多方法,所以选择你的毒药。你的方法有很多地方不对劲。特别是,您需要_username和_password表单元素,或者需要调整配置。还有很多其他的错误。我建议您创建一个新项目,然后一步一步地浏览安全文档,看看所有内容是如何组合在一起的。然后定制。