【问题标题】:Change Password not working Cakephp 3.0.3更改密码不起作用 Cakephp 3.0.3
【发布时间】:2015-08-06 20:29:51
【问题描述】:

我正在使用 cakephp 3.0.3,我正在编写更改用户密码的操作, 为此,我想将当前密码(由用户输入)与数据库中的现有密码进行比较。 但它每次都会给我不同的哈希值,

这是我在用户实体中的密码设置器,

protected function _setPassword($password)
{
    return (new DefaultPasswordHasher)->hash($password);
}

在控制器中,

public function changePassword()
{
    $this->request->data['current_password'] = '123456';

    $user = $this->Users->get(5); //get entity of userId 5
    $existing = $user->password; //display password of user 5 from database

    $user->password = $this->request->data['current_password'];
    $new = $user->password;
}

输出:

'$2y$10$inROlYu/ZKfowe.tTfX48OQ1q4oQBIzq3khzH5.jjITYjAxE3eMtm' //output of $existing, which is 123456 in plain
'$2y$10$A8zHGjCs/G1mlbfpzb6oIuc7TgjqC0ExR6X79kjt.1r64GSTSjpXy' //output of $new

这里两个密码相同,但我得到了差异。每次按 F5(刷新页面)时散列。

那么有什么方法可以比较两个密码并在 cakephp 3.0.3 中将其更改为新密码?

【问题讨论】:

    标签: cakephp authentication passwords cakephp-3.0


    【解决方案1】:

    这是因为 Cake 使用 bcrypt 作为其默认的散列函数。 bcrypt 并不总是为相同的输入字符串生成相同的哈希,因此您无法通过“重新哈希”密码来检查它。

    改为使用DefaultPasswordHasher::check() 方法。这将为您正确比较 bcrypt 密码。

    $verify = (new DefaultPasswordHasher)
      ->check($this->request->data['current_password'], $user->password);
    

    【讨论】:

      【解决方案2】:

      怎么样:

      return (new DefaultPasswordHasher)->hash($this->request->data['current_password']);
      

      您还需要获取当前密码的哈希值。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-04-12
        • 2016-12-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-12
        相关资源
        最近更新 更多