【问题标题】:Cakephp bcrypt saving empty fieldCakephp bcrypt 保存空字段
【发布时间】:2013-08-05 18:34:45
【问题描述】:

我正在尝试为我的 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。

我的应用控制器:

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.

【问题讨论】:

  • 您可以发布或查看您设置的表单吗?可能是某个字段的名称错误
  • 好主意,刚刚编辑过 OP

标签: cakephp bcrypt


【解决方案1】:

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

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

$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 开始,河豚加密正在发生变化,bcrypt 仅在 2.3 中:http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#using-bcrypt-for-passwords

【讨论】:

  • 感谢您的回答,但它并没有真正起到多大作用。仍然在数据库中返回空白密码
  • 你用的是什么版本?
  • 2.2.9... 我刚刚意识到 bcrypt 直到 2.3 才实现,对吧?该死的。我刚刚下载了顶部的版本,假设它是最新版本。是否可以在不丢失进度的情况下更新我的版本?编辑:修复了所有这些。感谢您指出这一点:)
猜你喜欢
  • 1970-01-01
  • 2012-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-29
  • 1970-01-01
相关资源
最近更新 更多