繁体   English   中英

CakePHP 2.4使用bcrypt登录失败

[英]CakePHP 2.4 Login Failing using bcrypt

我正在尝试使用bcrypt实施登录系统。 我在用户模型的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);
}

此代码在将密码存储在数据库中之前已成功对其进行哈希处理。

对于登录过程,我在视图中具有以下表单:

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'
    ));

...然后在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 {

        }
    }
}

我的AppController.php

class AppController extends Controller {

public $components = array(
    'Auth' => array(
        'authenticate' => array(
            'Form' => array(
                'passwordHasher' => 'Blowfish'
            )
        )
    )
);

}

使用此代码的登录始终失败。 对我做错的任何猜测吗?

编辑1:

好的,我一直在研究框架,试图了解该过程在哪里失败。 并在这种方法中:

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

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

在我的调试中,此结果被检索:

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

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

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

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

听起来您没有在db中设置密码字段长度足够长,无法存储完整的哈希。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM