【问题标题】:Blowfish authentication in cakephp 2.4.2cakephp 2.4.2 中的 Blowfish 身份验证
【发布时间】:2014-07-24 04:22:52
【问题描述】:

谁能帮助我使用河豚进行 cakephp 2.4.2 身份验证,我是 cakephp 身份验证的新手,所以我用谷歌搜索了它,但没有找到任何解决我的问题的方法。

这是我的代码

对于应用控制器

public $components = array(
    'Session',
    'RequestHandler',
    'Auth' => array(
        'authenticate' => array(
            'Form' => array(
                'passwordHasher' => 'Blowfish'
            )
        )
    )
);

对于模型

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

对于控制器

                if ($this->Auth->login()) {
                    $this->redirect(array('controller' => 'admins', 'action' => 'dashboard', 'builder' => true));
                } else {
                    $this->Session->write('flash', array('You Have entered wrong username or password.', 'failure'));
                    $this->redirect(array('controller' => 'users', 'action' => 'login', 'builder' => true));
                }

【问题讨论】:

  • 无法验证用户。
  • Security::hash 已弃用,如果可能,请使用密码哈希类代替文档。

标签: cakephp cakephp-2.4 blowfish


【解决方案1】:

对于河豚,您需要提供盐,即bcrypt

来自文档 http://book.cakephp.org/2.0/en/core-utility-libraries/security.html#Security::hash

// Create a hash using bcrypt
Security::setHash('blowfish');
$salt = Security::hash(Configure::read('Security.salt'));

// $salt is a previously generated bcrypt salt.
$passwordHash = Security::hash($password, 'blowfish', $salt);       

我建议为每个用户/密码使用单独的盐,在这种情况下,不要使用安全盐来创建 bcrypt 盐,而是使用一些随机字符串,然后将盐与密码哈希一起保存在数据库中。

在用户登录期间使用此场景

  • 1) 从数据库中获取盐和密码哈希
  • 2) 使用salt和用户在登录时提供的明文密码生成密码哈希
  • 3) 将新生成的密码哈希与从数据库中获取的密码进行比较。
  • 4) 如果匹配登录用户,否则显示错误

对于登录使用 if ($this->Auth->login($userData)) {$userData 应该是数组

array ('username' => 'the_username', 'password' => 'the_password');

身份验证:

$userData = $this->User->findByEmail('myEmail@gmail.com', array('username', 'password', 'salt'));

$passwordHash = Security::hash($userPlainTextPassword, 'blowfish', $userData['User']['salt']);

if ($passwordHash == $userData['User']['password']) {
  if (  $this->Auth->login($userData['User'])) {
     // ok
  } else {
    // smth wrong
  }
} else {
  // wrong username or password
}

顺便说一句,为了比较你最好使用标准时间比较的哈希值,在这里阅读更多

https://crackstation.net/hashing-security.htm#properhashing

【讨论】:

  • 感谢 rply,试一试
  • 我可以使哈希正确,但问题在于用户身份验证。
  • 您是否执行了列出的步骤,您能否确认 db 中的密码散列等于返回的散列提供的纯文本密码的散列?如果是这样,你将array ('username' => 'the_username', 'password' => 'the_password'); 数组提供给$this->Auth->login 函数应该没问题,请再检查一次,也许你错过了什么?如果不起作用,请编辑您的答案并发布您的代码登录部分,以便我提供进一步的帮助
  • 你能告诉我如何在身份验证时检查散列密码值
【解决方案2】:

在你的 AppController 中试试这个:

$this->Auth->authenticate = array(
        AuthComponent::ALL => array(
            'userModel' => 'User',
            'fields' => array(
                'username' => 'email',
                'password' => 'password'
            ),
            'scope' => $user_scope,
        ), 'Form'=> array(
                'passwordHasher' => 'Blowfish'
            )
    );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-11
    • 2011-07-31
    • 1970-01-01
    • 1970-01-01
    • 2014-05-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多