Dr *_*eto 2 authentication passwords cakephp cakephp-3.0
我正在使用cakephp 3.0.3,我正在编写更改用户密码的操作,因为我想将当前密码(由用户输入)与数据库中的现有密码进行比较.但它每次给我不同的哈希,
这是我在用户实体中的密码设置器,
protected function _setPassword($password)
{
return (new DefaultPasswordHasher)->hash($password);
}
Run Code Online (Sandbox Code Playgroud)
在控制器中,
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;
}
Run Code Online (Sandbox Code Playgroud)
输出:
'$2y$10$inROlYu/ZKfowe.tTfX48OQ1q4oQBIzq3khzH5.jjITYjAxE3eMtm' //output of $existing, which is 123456 in plain
'$2y$10$A8zHGjCs/G1mlbfpzb6oIuc7TgjqC0ExR6X79kjt.1r64GSTSjpXy' //output of $new
Run Code Online (Sandbox Code Playgroud)
这里两个密码都相同,但我得到了差异.每当我按F5(刷新页面)时哈希.
所以有没有办法比较两个密码,并在cakephp 3.0.3中更改新的密码?
这是因为Cake使用bcrypt默认的散列函数.bcrypt并不总是为相同的输入字符串生成相同的哈希值,因此您无法通过"重新密码"密码来检查它.
相反,请改用该DefaultPasswordHasher::check()方法.这将为您正确地比较bcrypt密码.
$verify = (new DefaultPasswordHasher)
->check($this->request->data['current_password'], $user->password);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2076 次 |
| 最近记录: |