Jam*_*son 2 php cakephp bcrypt
我注意到在CakePHP的3.2版本中,他们使用bcrypt添加了对散列的支持.我想利用这个,但我似乎无法找到如何正确使用它.
在我的User模型beforeSave()方法我这样做:
if(isset($this->data[$this->alias]['password'])) {
$this->data[$this->alias]['password'] = Security::hash($this->data[$this->alias]['password'], 'blowfish');
unset($this->data['User']['passwd']);
}
Run Code Online (Sandbox Code Playgroud)
它成功地在数据库中为用户帐户保存了bcrypt哈希.但是,我不确定我是如何登录用户的.我的用户控制器具有以下登录操作:
public function login() {
if($this->request->is('post')) {
if($this->Auth->login()) {
$this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash('Invalid username or password, try again.');
}
}
}
Run Code Online (Sandbox Code Playgroud)
但它每次都说"用户名或密码无效",我确定这是正确的电子邮件/密码.我认为这是因为AuthComponent不知道它应该使用bcrypt,但我不确定.
有什么建议?
好吧,我设法解决了.这是相关的代码:
在AppController.php:
public $components = array(
'Session',
'Auth' => array(
'authenticate' => array(
'Blowfish' => array(
'fields' => array('username' => 'email')
)
),
'loginRedirect' => array('controller' => 'pages', 'action' => 'home'),
'logoutRedirect' => array('controller' => 'pages', 'action' => 'home')
)
);
Run Code Online (Sandbox Code Playgroud)
在User.php:
public function beforeSave($options = array()) {
if(isset($this->data[$this->alias]['password'])) {
$this->data[$this->alias]['password'] = Security::hash($this->data[$this->alias]['password'], 'blowfish');
unset($this->data['User']['passwd']);
}
return true;
}
Run Code Online (Sandbox Code Playgroud)