CakePHP 2.4使用bcrypt登录失败

bfi*_*res 2 php cakephp cakephp-2.4

我正在尝试使用bcrypt实现登录系统.我在User模型的beforeSave()方法上有这个代码:

public function beforeSave($options = array()) {



    if (!$this->id && !isset($this->data[$this->alias][$this->primaryKey])) { // insert
        /*Hash the password*/
        $this->data['User']['password'] = Security::hash($this->data[$this->alias]['password'], 'blowfish');

        /*Set the username the same as the email*/
        $this->data['User']['username'] = $this->data['User']['email'];

    }
    parent::beforeSave($options);
}
Run Code Online (Sandbox Code Playgroud)

此代码在将密码存储到数据库之前成功地对密码进行哈希处理.

对于登录过程,我在视图中有这个表单:

echo $this->Form->create('User', array('action' => 'login'));

    echo $this->Form->input('username', array(
        'class' => 'login-input',
        'placeholder' => $input_username_default_text,
        'id' => 'username',
        'label' => false,
        'div' => false,
        'type' => 'text'
    ));
    echo $this->Form->input('password', array(
        'class' => 'login-input',
        'placeholder' => $input_password_default_text,
        'id' => 'password',
        'label' => false,
        'div' => false,
        'type' => 'text'
    ));
    echo $this->Form->submit(__('SIGN IN'), array(
        'class' => 'login-input',
        'type' => 'submit'
    ));
Run Code Online (Sandbox Code Playgroud)

...然后在UsersController的login()方法中:

public function login() {
    $this->set('body_class', 'login-page');
    if ($this->request->is('post')) {
        if ($this->Auth->login()) { //Always fails...

            debug('HELLO '.$this->session->read('Auth.User'));
        } else {

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我的AppController.php

class AppController extends Controller {

public $components = array(
    'Auth' => array(
        'authenticate' => array(
            'Form' => array(
                'passwordHasher' => 'Blowfish'
            )
        )
    )
);
Run Code Online (Sandbox Code Playgroud)

}

使用此代码的登录始终失败.对我做错了什么的猜测?

编辑1:

好吧,我一直在挖掘框架,试图了解程序失败的地方.并在这种方法:

// class BlowfishPasswordHasher
public function check($password, $hashedPassword) {
        return $hashedPassword === Security::hash($password, 'blowfish', $hashedPassword);
    }
Run Code Online (Sandbox Code Playgroud)

... $ hashedPassword(存储在数据库中的内容)与从Security :: hash($ password,'blowfish',$ hashedPassword)返回的内容不同.所以基本上登录失败了.但是我不知道为什么会这样.

在我的调试中,检索到了这些结果:

$ hashedPassword - $ 2a $ 10 $ f39m7NJBx3fIBrqq/9TZEueNJICJiO1dq1LZKlneF7Y(匹配用户表的密码列中存储的内容)

Security :: hash()方法的结果: $ 2a $ 10 $ f39m7NJBx3fIBrqq/9TZEueNJICJiO1dq1LZKlneF7Ykvm35emcPm

如果你注意到它们是相同的,除了方法的结果有10个额外的字符.

ADm*_*mad 5

如果你注意到它们是相同的,除了方法的结果有10个额外的字符.

听起来你没有在db中设置你的密码字段长度足以存储完整的哈希值.