Symfony security.password_encoder isPasswordValid返回空 [英] Symfony security.password_encoder isPasswordValid is returning empty

查看:276
本文介绍了Symfony security.password_encoder isPasswordValid返回空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Symfony 2.6中,我使用以下方法对密码进行编码.密码已成功编码并保存在数据库中.

In Symfony 2.6, I am using the following method to encode my password. The password is successfully encoded and saved in the DB.

$encoder = $this->container->get('security.password_encoder');
$encodedPwd = $encoder->encodePassword($adminUser, $plainPassword);

当我尝试验证登录表单中提供的用户提供的密码时,如下所示:

When I try to validate the user supplied password provided in the login form as follows:

$adminUser = $this->getDoctrine()->getManager()->getRepository("AcmeUserBundle:AdminUser")->findOneBy(array('username' => $_username));
$_password = $request->request->get('_password');

$encoder = $this->container->get('security.password_encoder');
echo $encoder->isPasswordValid($adminUser, $_password))

最后一行总是返回空,这意味着密码没有得到验证. 我从Symfony文档中获得了此信息,并搜索了是否有人遇到过类似的问题,但似乎没有找到任何问题. 有人可以提供一些见解吗?

The last line is always returning empty, which means that the password is not getting validated. I have gotten this from Symfony documentation and have searched if anyone has encountered similar problem, but doesn't seem to find any. Can any one please provide some insights please?

谢谢! 莎拉德

推荐答案

好,我找到了这样的解决方案,它正在起作用.希望它可以帮助遇到类似问题的任何人.

Ok I found a solution like this and it is working. Hope it helps anybody encountering similar problem.

要对密码进行编码,我需要执行以下操作:

For encoding the password, I am doing the following:

$factory = $this->get('security.encoder_factory');
$encoder = $factory->getEncoder($user);
$encodedPwd = $encoder->encodePassword($plainPassword, $user->getSalt());

为验证密码,我正在执行以下操作:

For validating the password, I am doing the following:

$factory = $this->get('security.encoder_factory');
$encoder = $factory->getEncoder($user);
echo $encoder->isPasswordValid($user->getPassword(), $_password, $user->getSalt())

在对密码进行编码时,我并没有使用盐,但这很重要, 现在一切正常.

While encoding the password, I wasn't using the salt, but it is important, It all works now.

谢谢 沙拉德

这篇关于Symfony security.password_encoder isPasswordValid返回空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆