任何人都可以使用blowfish帮助我使用cakephp 2.4.2 auth,我是cakephp auth的新手,所以我用谷歌搜索了它,但没有找到解决我问题的方法。
这是我的代码
对于App Controller
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));
}
答案 0 :(得分:2)
对于河豚,你需要提供一种盐,即bcrypt
盐
来自docs 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);
我建议为每个用户/密码使用单独的salt,在这种情况下,不要使用安全盐来创建bcrypt salt,使用一些随机字符串,然后将salt与密码哈希一起保存在数据库中。
在用户登录期间使用此方案
登录使用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
}
btw,为了比较哈希,你最好使用标准时间比较,在这里阅读更多
答案 1 :(得分:1)
在AppController中试试这个:
$this->Auth->authenticate = array(
AuthComponent::ALL => array(
'userModel' => 'User',
'fields' => array(
'username' => 'email',
'password' => 'password'
),
'scope' => $user_scope,
), 'Form'=> array(
'passwordHasher' => 'Blowfish'
)
);