php - CakePHP - 无法使用 Blowfish Auth 登录用户

标签 php cakephp cakephp-2.4

我正在使用 CakePHP 构建网络应用程序。 我想使用 bcrypt/Blowfish 进行密码加密。 用户注册工作正常,包括使用 bcrypt 散列密码。 但不知何故我之后无法登录 - 应用程序显示用户名/密码错误但输入正确。

这是我关于授权的代码:

AppController 中的 Auth 组件设置:

public $components = array(
    'Session',
    'Auth' => array(
        'loginAction' => array(
            'controller' => 'users',
            'action' => 'login'
        ),
        'authenticate' => array(
            'Form' => array(
                'userModel' => 'User',
                'passwordHasher' => 'Blowfish'
            )
        )
    )
);

模型代码:

public $validate = array(
    'username' => array(
        'alphaNumeric' => array(
            'rule'      => 'alphaNumeric',
            'required'  => true,
            'message'   => 'Alphanumeric characters only'
        )
    ),
    'email'=> 'email',
    'password' => array(
        'lengthrule' => array(
            'rule'      => array('minLength', '8'),
            'message'   => 'Minimum 8 characters'
        )
    )
);

public function beforeSave($options = array()) {
    parent::beforeSave();
    $passwordHasher = new BlowfishPasswordHasher();
    $this->data['User']['password'] = $passwordHasher->hash(
        $this->data['User']['password']
    );
    if(empty($this->data[$this->alias]['token'])) {
        $this->data[$this->alias]['token'] = md5(
            $this->data[$this->alias]['username'] .
            $this->data[$this->alias]['email'] .
            $this->data[$this->alias]['created']
        );
    }

    return $this->data;
}

我的 Controller 中的登录操作:

public function login() {
    if ($this->request->is('post')) {

        if ($this->Auth->login()) {
            return $this->redirect(array('controller' => 'users', 'action' => 'edit'));
        }
        $this->Session->setFlash(__('Invalid username or password, try again'));
    }
}

最后是元素,我正在尝试从以下位置登录:

<div class="users form">
<?php echo $this->Session->flash('auth'); ?>
<?php echo $this->Form->create('User', array('url' => '/users/login')); ?>
    <fieldset>
        <legend>
            <?php echo __('Login :)'); ?>
        </legend>
        <?php echo $this->Form->input('username');
        echo $this->Form->input('password');
    ?>
    </fieldset>
<?php echo $this->Form->end(__('Login')); ?>
<small>no account yet?</small><br />
<?php echo $this->Html->link(__('Register'), '/users/register'); ?>
</div>

更新

我发现散列字符串随着每次登录尝试而改变 - 如何解决这个问题?盐可能是随机的吗?不幸的是我没有在文档中找到任何相关内容

更新2

关于 this post ,在这种情况下,更改哈希可能是正确的。 但我假设,CakePHP 在尝试登录时自行管理使用正确的盐,对吧?

更新3

按如下方式编辑 BlowfishPasswordHasher 类:

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

我现在确保 Auch 组件对每次检查都使用相同的散列。 但仍然 - 存储在我的数据库中的哈希与哈希器的检查方法生成的哈希不同。有什么想法吗?

最佳答案

我发现了我的错误。

除了我的用户注册,我还实现了电子邮件验证。 经验证,用户记录正在被修改,相应的对 User->save() 的调用导致密码在 beforeSave() 中再次被散列。

所以对我有用的是使用上面的 beforeSave() 代码,但如果 token 字段为空,则只再次散列密码字段,在我的情况下,这只能在注册时发生,而不是在验证时或之后发生。

关于php - CakePHP - 无法使用 Blowfish Auth 登录用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22126629/

相关文章:

php - CURL - 此请求需要 HTTP 身份验证

php - Laravel 使用 Sum 和 Groupby

php - Symfony 4 中的功能测试事件和订阅者

CakePHP 2.1 使用包含条件进行查找

cakephp - CakePHP 中的 "Automagic"输出字段?

cakephp - CakePHP Controller 中的 add 方法不起作用

mysql - 在CakePHP中,有没有更好的方法来使用AES_DECRYPT?

CakePHP/CakePHP 2.4 应用程序

php - 取消设置多个数组元素的更好方法

php - cakephp 缓存文件夹在 iis 上不可写