我将应用程序从CakePHP 2移动到CakePHP 3. Cake3有一个新的哈希算法。我希望现有用户能够使用旧密码登录应用程序,然后将这些密码更新为新算法。
不幸的是,我无法获得正确的哈希以匹配数据库中的内容。
for wrapper in body.find_all('div', {"class":"wrapper"}):
print wrapper.text
找不到用户,如果我调试$ oldhash,我得到的字符串与该用户的密码字段中存储的字符串不同。
我做错了什么?
答案 0 :(得分:4)
CakePHP提供了一种将用户密码从一种算法迁移到另一种算法的简洁方法,这是通过FallbackPasswordHasher类实现的。假设您要从使用sha1密码哈希的CakePHP 2.x迁移您的应用程序,您可以按如下方式配置AuthComponent:
您必须创建Custom Password Hasher课程src/Auth/
。自定义密码看起来像这样:
namespace App\Auth;
use Cake\Auth\AbstractPasswordHasher;
class LegacyPasswordHasher extends AbstractPasswordHasher {
public function hash($password)
{
return sha1($password);
}
public function check($password, $hashedPassword)
{
return sha1($password) === $hashedPassword;
} }
然后将passwordhasher
中的authenticate
添加为fallback
,如下所示:
'authenticate' => [
'Form' => [
'passwordHasher' => [
'className' => 'Fallback',
'hashers' => [
'Default',
'Legacy'
]
]
]
]
出现在哈希键中的第一个名称表示哪个类是首选类,但如果检查不成功,它将回退到列表中的其他类。
legacy
是Custom Password Hasher。
要将用户密码更新为新哈希,您只需将此代码添加到登录过程中:
if ($this->Auth->authenticationProvider()->needsPasswordRehash()) {
$user = $this->Users->get($this->Auth->user('id'));
$user->password = $this->request->data('password');
$this->Users->save($user);
}
答案 1 :(得分:0)
我有一个使用Blowfish的CakePHP 2应用程序。这就是我如何使它与CakePHP 3配合使用:
$this->loadComponent('Auth', [
'authenticate' => [
'Form' => [
// ...
'fields' => [
'username' => 'email',
'password' => 'pass', // make sure you match this with your corresponding login.ctp input name
],
// ...
'passwordHasher' => [
'className' => 'Fallback',
'hashers' => [
'Default' => ['hashType' => PASSWORD_BCRYPT],
]
],
// ...
]
],
希望它可以帮助搜索此问题的人