Cakephp bcrypt保存空字段

时间:2013-08-05 18:34:45

标签: cakephp bcrypt

我正在尝试为我的cakephp应用设置bcrypt。我以前在另一个应用程序上进行了设置,这很有效。但是,在将加密代码从一个应用程序基本上复制/粘贴到另一个应用程序之后,它将密码保存为空白。

数据库设置正确,密码字段为varchar(225)。

我得出结论,以下代码行是造成问题的原因;

public function beforeSave($options = array()) {
    if (isset($this->data[$this->alias]['password'])) {
        $hash = Security::hash($this->data['User']['password'], 'blowfish');
        $this->data['User']['password'] = $hash;
    }
    return true;
}

如果我要取出这个beforeSave函数,我的密码将以明文正确保存。如果我要替换

$this->data['User']['password'] = $hash;

$this->data['User']['password'] = 'testpassword';

它会将密码正确保存为testpassword。

我的AppController:

public $components = array(
    'Session',
    'Auth' => array(
        'authenticate' =>'Blowfish',
        'logoutRedirect' => array('controller'=>'fronts', 'action'=>'index'),
        'authorize' => array('Controller')
    )
);

我的表格:

<?php echo $this->Session->flash('auth'); ?>
<?php echo $this->Form->create('User'); ?>
<fieldset>
    <?php 
        echo $this->form->input('username', array('placeholder' => 'Username', 'label' => false));
        echo $this->form->input('password', array('placeholder' => 'Password', 'label' => false));
        echo $this->form->submit('CREATE', array('class' => 'button')); 
    ?>
</fieldset>
<?php echo $this->Form->end(); ?>

尝试登录时,虽然我知道它不起作用,但我收到此错误

Authentication adapter Blowfish was not found.

1 个答案:

答案 0 :(得分:1)

beforesave似乎是正确的。我本质上使用相同的东西。这是我的代码。

我还在我的前置过滤器中设置了河豚,但在其他方面存在问题

$this->Auth->authorize = array('Controller');
$this->Auth->authenticate = array(
   'Blowfish' => array( 
      'userModel' => 'Account',
      'fields' => array('username' => 'act_username', 'password' => 'act_password')
   )
);

唯一的另一个区别是我的表单按钮有此提交

echo $this->Form->submit('Login', array('id' => 'submit', 'type' => 'submit'));

请注意,截至2.4版本,blowfish加密正在改变,bcrypt仅在2.3:http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#using-bcrypt-for-passwords