cakephp3-哈希密码在比较时不匹配
CakePHP版本:3.3.5 我正在构建一个简单的系统,用户可以登录(使用电子邮件和密码),登录后可以更改密码 为此,我使用了cakephp3-哈希密码在比较时不匹配,php,cakephp,cakephp-3.0,cakephp-3.x,cakephp-3.3,Php,Cakephp,Cakephp 3.0,Cakephp 3.x,Cakephp 3.3,CakePHP版本:3.3.5 我正在构建一个简单的系统,用户可以登录(使用电子邮件和密码),登录后可以更改密码 为此,我使用了DefaultPasswordHasher 我的数据库中已经有一些用户。他们的记录已经存在。因此,当我使用登录功能时,它工作正常。我将用户输入的密码与数据库中已经存在的hased密码进行了比较。检查成功,用户可以登录 现在登录后,我编写了更改密码函数,更新了用户密码。新的哈希字符串替换了旧的密码字符串,但当我再次尝试登录时,登录失败 我将在这里共享我的控制器。这很基本
DefaultPasswordHasher
我的数据库中已经有一些用户。他们的记录已经存在。因此,当我使用登录功能时,它工作正常。我将用户输入的密码与数据库中已经存在的hased密码进行了比较。检查成功,用户可以登录
现在登录后,我编写了更改密码函数,更新了用户密码。新的哈希字符串替换了旧的密码字符串,但当我再次尝试登录时,登录失败
我将在这里共享我的控制器。这很基本
namespace Api\Controller;
use Cake\Utility\Security;
use Cake\Utility\Hash;
use Cake\Auth\DefaultPasswordHasher;
use Api\Controller\AppController;
class LoginController extends AppController
{
public function initialize()
{
parent::initialize();
$this->loadComponent('RequestHandler');
}
//Function to reset the password
public function resetPassword()
{
$pass = $this->request->data['pass'];
$hasher = new DefaultPasswordHasher();
$hashedPass = $hasher->hash($pass);
$this->loadModel('Login');
//save it to db
$responseArray = $this->Login->resetPassword($hashedPass);
$this->set(compact('responseArray'));
$this->set('_serialize', ['responseArray']);
}
//Function to login
public function login()
{
if ($this->request->is('post'))
{
//Password submitted via form
$pass = $this->request->data['pass'];
//Hashed password fetched from db via a function call
$actualPassword = 'hashedPasswordString'
//Compare password submitted and hash from db
if($this->checkPassword($pass,$actualPassword))
{
$result = 'password matched';
}
else
{
$result = 'password doesnot match';
}
}
$this->set(compact('result'));
$this->set('_serialize', ['result']);
}
//Function to compare password and hash
public function checkPassword($passedPassword , $actualPassword)
{
if ((new DefaultPasswordHasher)->check($passedPassword, $actualPassword)) {
return true;
} else {
return false;
}
}
}
有人能告诉我为什么密码不匹配吗。我是CakePHP框架的新手。提前谢谢 这就是我的重置密码工作流的样子。我无法从你的帖子中看出你的实体和表是什么样子 只要发布的数据被转换成用户实体,它就会被散列 Admin/userscocontroller.php
public function password($id = null)
{
$user = $this->Users->get($id, [
'fields' => ['id', 'first_name', 'last_name', 'username']
]);
if ($this->request->is('put')) {
if ($this->request->data['password'] == $this->request->data['password2']) {
$this->Users->patchEntity($user, ['password' => $this->request->data['password']]);
$this->Users->save($user);
$this->Flash->success('Password has been updated');
return $this->redirect('/admin/users/password/' . $id);
} else {
$this->Flash->error('Passwords do not match');
}
}
$this->set(compact('user'));
}
protected function _setPassword($password)
{
if (strlen($password) > 0) {
return (new DefaultPasswordHasher)->hash($password);
}
}
Model/Entity/User.php
public function password($id = null)
{
$user = $this->Users->get($id, [
'fields' => ['id', 'first_name', 'last_name', 'username']
]);
if ($this->request->is('put')) {
if ($this->request->data['password'] == $this->request->data['password2']) {
$this->Users->patchEntity($user, ['password' => $this->request->data['password']]);
$this->Users->save($user);
$this->Flash->success('Password has been updated');
return $this->redirect('/admin/users/password/' . $id);
} else {
$this->Flash->error('Passwords do not match');
}
}
$this->set(compact('user'));
}
protected function _setPassword($password)
{
if (strlen($password) > 0) {
return (new DefaultPasswordHasher)->hash($password);
}
}
$responseArray=$this->Login->resetPassword($hashedPass)的具体功能是什么
do?为什么不使用CakePHP附带的身份验证组件?另外,请不要删除对工作至关重要的代码,例如获取当前密码的代码,您可能认为/知道它可以被导出,但情况可能并非如此,读者不看到它就无法知道。也就是说,您能给出任何调试结果吗?从密码读取的值是否与数据库中存储的值相同?新的散列是否首先正确存储(实际上是散列的,不是切分的,不是双重/三重/散列的)。。。提示:可能更简单,因为在编辑操作中只需要一行(添加行为):)