Cakephp 3.7.4身份验证哈希密码不起作用

时间:2019-04-23 08:18:01

标签: cakephp cakephp-3.0 cakephp-3.7

我正在尝试实现身份验证登录并注册cakephp 3.7.4

我已将以下代码用于UsersController添加方法

public function add()
{
    $this->viewBuilder()->setLayout('login');
    $user = $this->Users->newEntity();
    if ($this->request->is('post')) {
        $post = $this->request->getData();
        $post['created'] = date('Y-m-d H:i:s');
        $post['modified'] = date('Y-m-d H:i:s');
        $user = $this->Users->patchEntity($user, $post);
        if ($this->Users->save($user)) {
            $this->Flash->success(__('The user has been saved.'));
            return $this->redirect(['action' => 'login']);
        }
        $this->Flash->error(__('Unable to add the user.'));
    }
    $this->set('user', $user);
}

但是它不能以哈希格式保存密码

我也创建了实体并使用了此功能,但也没有帮助

 class User extends Entity
 {
   protected $_accessible = [
     'email' => true,
    'password' => true
  ];
 protected $_hidden = [
    'password'
 ];
 protected function _setPassword($password){
  return(new  DefaultPasswordHasher)->hash($password);
 }
}

2 个答案:

答案 0 :(得分:0)

我安装了全新的Cake 3.7.4安装,烘烤了users表,users控制器和users表/实体。以下针对用户类的代码在将密码写入数据库之前已成功对密码进行了哈希处理。请与您的代码进行比较。

namespace App\Model\Entity;

use Cake\Auth\DefaultPasswordHasher;
use Cake\ORM\Entity;

class User extends Entity
{
    protected $_accessible = [
        'email' => true,
        'password' => true,
        'modified' => true,
        'created' => true
    ];
    protected $_hidden = [
        'password'
    ];

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

答案 1 :(得分:0)

只需在DefaultPasswordHaser()周围加上括号:

return(new  DefaultPasswordHasher())->hash($password);