【问题标题】:cakephp3- hashed password doesnt match when comparedcakephp3-比较时散列密码不匹配
【发布时间】:2017-04-05 12:38:07
【问题描述】:

CakePHP 版本:3.3.5

我正在构建一个简单的系统,用户可以使用该系统登录(使用电子邮件和密码),登录后他们可以更改密码。

为此,我使用DefaultPasswordHasher

我的数据库中已经有一些用户。他们的记录已经存在。因此,当我执行登录功能时,它起作用了。我将用户输入的密码与数据库中已经存在的密码进行了比较。检查成功,用户可以登录。

现在登录后,我写了更改密码功能,更新了用户密码。新的哈希字符串替换了旧的密码字符串,但是当我再次尝试登录时,登录失败。

我将在这里分享我的控制器。这是非常基本的。

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 框架的新手。提前致谢!

【问题讨论】:

  • $responseArray = $this->Login->resetPassword($hashedPass); 到底是做什么的?
  • 为什么不使用 CakePHP 附带的身份验证组件?另外请不要删除对工作至关重要的代码,例如获取当前密码的代码,您可能认为/知道它按预期工作,但情况可能并非如此,读者无法知道没有看到它。话虽如此,您能提供任何调试结果吗?从密码读取的值是否与存储在数据库中的值相同?新的散列是否首先被正确存储(实际上是散列,没有截断,不是双/三/..散列)? ...
  • 提示:Passwordable behavior 可能更简单,因为它只需要在您的编辑操作中添加一行(添加行为):)

标签: php cakephp cakephp-3.0 cakephp-3.x cakephp-3.3


【解决方案1】:

这就是我的重置密码工作流程。我无法从您的帖子中看出您的实体和表格是什么样的。

任何时候发布的数据都被转换为用户实体,现在它会被散列

Admin/UsersController.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'));
}

模型/实体/用户.php

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

【讨论】:

    【解决方案2】:
    public function changePassword(){
        if ($this->request->is('post')) {
            $data = $this->request->data();
            $res['success'] = FALSE;
            $user = $this->Users->get($this->Auth->user('id'))->toArray();
            if ((new DefaultPasswordHasher)->check($data['oldPassword'], $user['password'])) {
                if($data['newPassword'] == $data['confPassword']){
                    $userEntity = $this->Users->get($this->Auth->user('id'));
                    $userEntity->password = $data['newPassword'];
                    if($this->Users->save($userEntity)){
                        $res['success'] = TRUE;
                        $res['message'] = 'Password Changed Successfully.';
                    }
                }else{
                    $res['success'] = FALSE;
                    $res['message'] = 'Confirm password is not same as new password. please enter both password again!!';
                }
    
            }else{
                 $res['success'] = FALSE;
                 $res['message'] = 'Your old password is wrong!';
            }
            echo json_encode($res);
            exit();
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-10-05
      • 2016-06-19
      • 1970-01-01
      • 1970-01-01
      • 2021-06-18
      • 1970-01-01
      • 2015-01-29
      • 1970-01-01
      • 2016-09-19
      相关资源
      最近更新 更多