更改密码不起作用 Cakephp 3.0.3
Change Password not working Cakephp 3.0.3
我正在使用 cakephp 3.0.3,我正在编写更改用户密码的操作,
为此,我想将当前密码(由用户输入)与数据库中的现有密码进行比较。
但它每次都给我不同的哈希,
这是我在用户实体中的 setter 密码,
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;
}
输出:
'y$inROlYu/ZKfowe.tTfX48OQ1q4oQBIzq3khzH5.jjITYjAxE3eMtm' //output of $existing, which is 123456 in plain
'y$A8zHGjCs/G1mlbfpzb6oIuc7TgjqC0ExR6X79kjt.1r64GSTSjpXy' //output of $new
这两个密码相同,但我得到了不同。每次按 F5(刷新页面)时哈希。
那么有什么方法可以比较两个密码并在 cakephp 3.0.3 中用新密码更改密码吗?
怎么样:
return (new DefaultPasswordHasher)->hash($this->request->data['current_password']);
您需要获取当前密码的哈希值。
这是因为 Cake 使用 bcrypt
作为其默认的散列函数。 bcrypt
并不总是为相同的输入字符串生成相同的散列,因此您无法通过 "rehashing" 密码来检查它。
而是使用 DefaultPasswordHasher::check()
方法。这将为您正确比较 bcrypt
个密码。
$verify = (new DefaultPasswordHasher)
->check($this->request->data['current_password'], $user->password);
我正在使用 cakephp 3.0.3,我正在编写更改用户密码的操作, 为此,我想将当前密码(由用户输入)与数据库中的现有密码进行比较。 但它每次都给我不同的哈希,
这是我在用户实体中的 setter 密码,
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;
}
输出:
'y$inROlYu/ZKfowe.tTfX48OQ1q4oQBIzq3khzH5.jjITYjAxE3eMtm' //output of $existing, which is 123456 in plain
'y$A8zHGjCs/G1mlbfpzb6oIuc7TgjqC0ExR6X79kjt.1r64GSTSjpXy' //output of $new
这两个密码相同,但我得到了不同。每次按 F5(刷新页面)时哈希。
那么有什么方法可以比较两个密码并在 cakephp 3.0.3 中用新密码更改密码吗?
怎么样:
return (new DefaultPasswordHasher)->hash($this->request->data['current_password']);
您需要获取当前密码的哈希值。
这是因为 Cake 使用 bcrypt
作为其默认的散列函数。 bcrypt
并不总是为相同的输入字符串生成相同的散列,因此您无法通过 "rehashing" 密码来检查它。
而是使用 DefaultPasswordHasher::check()
方法。这将为您正确比较 bcrypt
个密码。
$verify = (new DefaultPasswordHasher)
->check($this->request->data['current_password'], $user->password);