Wil*_*ill 2 security cakephp cakephp-3.0
我正在将一个应用程序从 CakePHP 2 迁移到 CakePHP 3。Cake3 有一个新的哈希算法。我希望现有用户能够使用旧密码登录应用程序,然后将这些密码更新为新算法。
不幸的是,我无法获得与数据库中的内容相匹配的正确哈希值。
$person = $this->Auth->identify();
if(!$person){ # maybe they have old sha1 password?
$oldhash = Security::hash($this->request->data['password'],
'sha1', "oldsalt");
$person = $this->People->find()->where(['password' => $oldhash])->where(['email' =>$this->request->data['email'] ])->first();
if($person){
$person->password = Security::hash($this->request->data['password']);
$this->People->save($person);
}
}
Run Code Online (Sandbox Code Playgroud)
找不到用户,如果我调试 $oldhash ,我会得到一个与该用户的密码字段中存储的字符串不同的字符串。
我究竟做错了什么?
根据文档:
\n\n\n\n\nCakePHP 提供了一种干净的方法将用户\xe2\x80\x99 密码从一种算法迁移到另一种算法,这是通过 FallbackPasswordHasher 类实现的。假设您要从使用 sha1 密码哈希的 CakePHP 2.x 迁移应用程序,您可以按如下方式配置 AuthComponent:
\n
您必须创建一个自定义密码哈希器类src/Auth/。自定义密码哈希器看起来像这样:
namespace App\\Auth;\n\nuse Cake\\Auth\\AbstractPasswordHasher;\n\nclass LegacyPasswordHasher extends AbstractPasswordHasher {\n\n public function hash($password)\n {\n return sha1($password);\n }\n\n public function check($password, $hashedPassword)\n {\n return sha1($password) === $hashedPassword;\n } }\nRun Code Online (Sandbox Code Playgroud)\n\n然后将其添加到passwordhasherin中authenticate,如下fallback所示:
\'authenticate\' => [\n \'Form\' => [\n \'passwordHasher\' => [\n \'className\' => \'Fallback\',\n \'hashers\' => [\n \'Default\',\n \'Legacy\'\n ]\n ]\n ]\n ]\nRun Code Online (Sandbox Code Playgroud)\n\n\n\n\n散列键中出现的第一个名称指示哪个类是首选类,但如果检查不成功,它将回退到列表中的其他类。
\n
legacy是自定义密码哈希器。
要将用户的密码更新为新的哈希值,您只需将此代码添加到您的登录过程中:
\n\nif ($this->Auth->authenticationProvider()->needsPasswordRehash()) {\n $user = $this->Users->get($this->Auth->user(\'id\'));\n $user->password = $this->request->data(\'password\');\n $this->Users->save($user);\n}\nRun Code Online (Sandbox Code Playgroud)\n\n| 归档时间: |
|
| 查看次数: |
817 次 |
| 最近记录: |