&引用;认证->;标识();始终返回false-cakephp3.5

&引用;认证->;标识();始终返回false-cakephp3.5,php,authentication,cakephp,cakephp-3.x,Php,Authentication,Cakephp,Cakephp 3.x,由于某些原因,我无法登录到我注册的帐户。更多信息如下 来自UsersController.php的函数 public function login() { if ($this->request->is('post')) { $auth = $this->Auth->identify(); // Returns false debug($this->request->getData()); // Return email

由于某些原因,我无法登录到我注册的帐户。更多信息如下

来自UsersController.php的函数

public function login() {
    if ($this->request->is('post')) {
        $auth = $this->Auth->identify(); // Returns false
        debug($this->request->getData()); // Return email & with unhashed password
        debug($auth);
        if ($auth) {
            $this->Auth->setUser($auth);
            $this->redirect($this->Auth->redirectUrl());
        } else {
            $this->Flash->error('E-mail or password is wrong.');
        }
    }
}

public function register() {
    $user = $this->Users->newEntity();
    $this->set('user', $user);

    $this->loadModel('Groups');
    $group = $this->Groups->newEntity();
    $this->set('group', $user);

    if ($this->request->is('post')) {

        // Check if passwords matches
        $pass = $this->request->getData('password');
        $con_pass = $this->request->getData('password_confirm');

        if ($pass !== $con_pass) {
            return $this->Flash->error('Passwords don\'t match');
        }

        // Patch entities
        $group = $this->Groups->patchEntity($group, $this->request->getData());
        $user = $this->Users->patchEntity($user, $this->request->getData());

        // Make group and user
        if (empty($group->errors()) && empty($user->errors())) {
            // Group
            if (!$this->Groups->save($group)) {
                return $this->Flash->error('Something went wrong');
            }

            // User
            $user->group_id = $group->id;
            if ($this->Users->save($user)) {
                $this->Flash->success('Welkom ' . $user->name . '!');
                // return $this->redirect(['action' => 'register']);
            } else {
                return $this->Flash->error('something went wrong2');
            }
        }
    }
}
验证AppController中的组件:

        $this->loadComponent('Auth', [
        'userModel' => 'Users',
        'loginAction' => [
            'controller' => 'Users',
            'action' => 'login'
        ],
        'authenticate' => [
            'Form' => [
                'fields' => [
                    'username' => 'email',
                    'password' => 'password'
                ]
            ]
        ],
        //'authError' => false,
        'storage' => 'Session'
    ]);
    <?= $this->Form->create('User'); ?>
    <?= $this->Form->email('email', ['placeholder' => 'E-mail', 'maxlength' => '42', 'label' => false]) ?>
    <?= $this->Form->password('password', ['type' => 'password', 'placeholder' => 'Wachtwoord', 'maxlength' => '32', 'label' => false]) ?>
    <?= $this->Form->submit('Login', ['class' => 'button']) ?>
<?= $this->Form->end(); ?>
class User extends Entity {

protected $_accessible = [
    'group_id' => true,
    'name' => true,
    'email' => true,
    'password' => true,
    'profile_img_url' => true,
    'pass_reset_time' => true,
    'creation_date' => true,
    'modified_date' => true
];

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

protected $_hidden = [
    'password'
];
登录表单:

        $this->loadComponent('Auth', [
        'userModel' => 'Users',
        'loginAction' => [
            'controller' => 'Users',
            'action' => 'login'
        ],
        'authenticate' => [
            'Form' => [
                'fields' => [
                    'username' => 'email',
                    'password' => 'password'
                ]
            ]
        ],
        //'authError' => false,
        'storage' => 'Session'
    ]);
    <?= $this->Form->create('User'); ?>
    <?= $this->Form->email('email', ['placeholder' => 'E-mail', 'maxlength' => '42', 'label' => false]) ?>
    <?= $this->Form->password('password', ['type' => 'password', 'placeholder' => 'Wachtwoord', 'maxlength' => '32', 'label' => false]) ?>
    <?= $this->Form->submit('Login', ['class' => 'button']) ?>
<?= $this->Form->end(); ?>
class User extends Entity {

protected $_accessible = [
    'group_id' => true,
    'name' => true,
    'email' => true,
    'password' => true,
    'profile_img_url' => true,
    'pass_reset_time' => true,
    'creation_date' => true,
    'modified_date' => true
];

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

protected $_hidden = [
    'password'
];
}

用户使用哈希密码正确保存在数据库中

当我尝试登录
$this->Auth->identify()时始终返回false

我试着去了解一些事情:

  • 我正在尝试使用电子邮件和密码登录
  • 数据库中的表名为
    users
  • 续订salt(并创建新帐户并使用该帐户登录)
  • 密码列长度为255
  • 检查
  • 检查
  • 在Stack和其他网站上检查了很多与此相关的问题,但没有任何东西能解决我的问题
  • 用户得到正确的存储。但一旦我尝试登录,它就不允许我登录
  • 我尝试在没有密码哈希函数的情况下登录,但使用了未修改的密码,也没有成功
  • 签入不同的浏览器并删除缓存

谢谢

似乎没有任何明显的错误,除了
\u setPassword()
方法中缺少空白检查,该方法将防止对空的
$password
进行哈希运算。您应该执行与文档中所示类似的操作:

if (strlen($password) > 0) {
    return (new DefaultPasswordHasher)->hash($password);
}
请参见

另外,
FormHelper::create()
方法也不接受字符串,只是出于向后兼容的原因,它不会出错。如果没有要传递的有效上下文,则根本不传递任何值

也就是说,您必须自己进行更多的调试。首先使用
DefaultPasswordHasher::validate()
方法手动验证存储在数据库中的哈希密码,以确保哈希值正确

然后在身份验证代码流中设置一些断点,以确定哪里可能出错,请检查:

  • FormAuthenticate::authenticate()
  • FormAuthenticate::\u checkFields()
  • BaseAuthenticate::\u findUser()
  • BaseAuthenticate::\u query()

是否读取了正确的请求数据、是否按预期构建了查询条件、是否返回了密码验证值以及返回了什么值等等。

除了
\u setPassword()中缺少空性检查外,似乎没有任何明显的错误
方法,该方法将防止对空的
$password
进行哈希运算。您应该执行与文档中所示类似的操作:

if (strlen($password) > 0) {
    return (new DefaultPasswordHasher)->hash($password);
}
请参见

另外,
FormHelper::create()
方法也不接受字符串,只是出于向后兼容的原因,它不会出错。如果没有要传递的有效上下文,则根本不传递任何值

也就是说,您必须自己进行更多的调试。首先使用
DefaultPasswordHasher::validate()
方法手动验证存储在数据库中的哈希密码,以确保哈希值正确

然后在身份验证代码流中设置一些断点,以确定哪里可能出错,请检查:

  • FormAuthenticate::authenticate()
  • FormAuthenticate::\u checkFields()
  • BaseAuthenticate::\u findUser()
  • BaseAuthenticate::\u query()

是否读取了正确的请求数据,是否按照预期构建了查询条件,是否返回了密码验证值以及返回了什么值等等。

好吧,我浪费了整个上午和下午

我以为我的密码列长度是255,但实际上是32。显然,我检查了4次错误列的长度


谢谢你的帮助@ndm。

好吧,我浪费了整个上午和下午

我以为我的密码列长度是255,但实际上是32。显然,我检查了4次错误列的长度

谢谢@ndm的帮助