【问题标题】:Broken passwords in Symfony 3.0Symfony 3.0 中的密码损坏
【发布时间】:2015-12-13 07:19:44
【问题描述】:

我的 Symfony 应用程序loads its user entities from the database。这是security.yml中的条目:

security:
    encoders:
        MyProject\MyBundle\Entity\UserEntity:
            algorithm: bcrypt

    providers:
        main:
            entity:
                class: MyBundle:UserEntity
                property: username

当我实现这个时,我遵循 Symfony 的建议,为每个用户存储一个唯一的盐,在密码被编码时使用它:

$encoder = $this->get('security.encoder_factory')->getEncoder($user);
$encodedPassword = $encoder->encodePassword($form->getData()->getPassword(), $user->getSalt());
if ($encodedPassword === $user->getPassword()) {
      // Success
}

Symfony 3.0 no longer allows you to pass a salt,所以我更新了我的逻辑:

$encoder = $this->get('security.password_encoder');
$encodedPassword = $encoder->encodePassword($user, $form->getData()->getPassword());
if ($encodedPassword === $user->getPassword()) {
      // Success
}

但是没有盐的编码显然是行不通的。有什么方法可以让我的用户密码在 3.0 上工作,而不是要求他们每个人重置他们的凭据?

【问题讨论】:

  • 默认编码可能已更改,但编码器界面仍然需要密码和盐。只需将编码器配置为使用您为 2.x 所做的任何事情,就可以了。
  • 你能告诉我编码器接口如何接受盐吗?我在文档中找不到这一点,并且编码功能现在肯定会忽略盐:github.com/symfony/security-core/blob/master/Encoder/…
  • 请发布您的 security.yml 文件的编码器部分。在我的 S3 上,默认编码器 UserPasswordEncoder 支持用户盐。需要弄清楚为什么要使用 bycrypt 编码器。您是否在 S2 和 S3 之间进行了任何更改?
  • 感谢 Cerad,我已将编码器部分添加到我原来的问题中。 S2 和 S3 之间没有变化。
  • 已解决!感谢您的帮助。

标签: bcrypt symfony


【解决方案1】:

问题是我没有使用isPasswordValid 函数。这可以识别现在存储在 bcrypt 加密密码中的盐:

$encoder = $this->get('security.encoder_factory')->getEncoder($user);
$passwordValid = $encoder->isPasswordValid(
    $user->getPassword(),
    $form->getData()->getPassword(),
    null
);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-14
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-14
    相关资源
    最近更新 更多