gpt4 book ai didi

php - 在 CakePHP 2.3 中使用 bcrypt

转载 作者:行者123 更新时间:2023-12-02 05:15:39 25 4
gpt4 key购买 nike

我注意到在 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']);
}

它成功地在数据库中为用户帐户保存了一个 bcrypt 哈希。但是,我不确定我打算如何登录用户。我的用户 Controller 具有以下登录操作:

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.');
}
}
}

但每次都显示“无效的用户名或密码”,我确定这是正确的电子邮件/密码。我认为这是因为 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')
)
);

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;
}

关于php - 在 CakePHP 2.3 中使用 bcrypt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14694071/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com