php - CakePHP 2.4 使用 bcrypt 登录失败

标签 php cakephp cakephp-2.4

我正在尝试使用 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);
}

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

对于登录过程,我在 View 中有此表单:

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 个额外的字符。

最佳答案

If you notice they are the same except that the result of the method has 10 extra chars.

听起来您没有在数据库中设置足够长的密码字段长度来存储完整的哈希值。

关于php - CakePHP 2.4 使用 bcrypt 登录失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18872917/

相关文章:

php - 在 laravel 中创建文件夹

php - 如何只使用某些验证集来验证 Cake PHP 中的数据?

cakephp - 如何使用 CakePHP 的 HtmlHelper 创建安全链接 (https)

javascript - UnlockField 在 CakePHP 中不起作用

php - 代码点火器。如何在表 int() 列中插入时间戳格式的字符串值

PHP 测试 JSON 包含 SQLite

cakephp Controller 的 jquery getJSON() 响应为空,但 200 成功

php - CakePHP 2.x 无法更新最近添加到 MySQL 表的字段

php - 仅显示数据库表中的选定结果

php - CakePHP 插件 - 如何为虚 URL 设置 Apache Rewrite