CakePHP 2.4使用bcrypt登录失败
我正在尝试使用bcrypt实现一个登录系统。我在用户模型的beforeSave()方法中有以下代码:CakePHP 2.4使用bcrypt登录失败,php,cakephp,cakephp-2.4,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*/ $
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()方法中:
我的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$F39M7NJBX3QQ/9TZEueNJICJiO1dq1LZKlneF7Y(与用户表密码列中存储的内容匹配)
Security::hash()方法的结果:$2a$10$f39m7njbx3fibreqq/9tzeuenjicjiio1dq1lzklnef7ykvm35emcpm
如果您注意到它们是相同的,只是方法的结果有10个额外的字符
如果您注意到它们是相同的,只是方法的结果有10个额外的字符
听起来你没有设置足够长的密码字段长度(以db为单位)来存储完整的散列。beforeSave()如何使save()工作,而你在这里却从未返回true?我不认为储蓄真的会发生在这里。。。相信我,这是节约。我认为这是因为父方法是返回true的方法。不,父调用被吞没,而void在这里返回。即使这不是问题所在,您也应该解决此问题。
// class BlowfishPasswordHasher
public function check($password, $hashedPassword) {
return $hashedPassword === Security::hash($password, 'blowfish', $hashedPassword);
}