0

I want to migrate my cakephp2 project to cakephp3. I must retain the user's information. How to make them have the same way of generating a password? Here's how I generate passwords in cakephp2.

App::uses('AuthComponent', 'Controller/Component');
....
public function beforeSave($options = array()) {
      $this->data['User']['password'] = AuthComponent::password(
      $this->data['User']['password']
    );
    return true;
}

This is the way cakephp3 document generates passwords:

namespace App\Model\Entity;

use Cake\Auth\DefaultPasswordHasher;
use Cake\ORM\Entity;

/**
 * User Entity.
 */
class User extends Entity
{

    /**
     * Fields that can be mass assigned using newEntity() or patchEntity().
     *
     * @var array
     */
    protected $_accessible = [
        'email' => true,
        'password' => true,
        'bookmarks' => true,
    ];

    protected function _setPassword($value)
    {
        $hasher = new DefaultPasswordHasher();
        return $hasher->hash($value);
    }

}

They are not the same plaintext generate the same ciphertext. So, I can not retain cakephp2 user information. Could you tell me how to set up a successful migration project?

4

2 回答 2

1

请参阅Migration Guide,它解释了您需要做的所有事情,特别是它在 Auth Component 部分下提到了这一点:

Default 现在是身份验证类使用的默认密码哈希。它专门使用 bcrypt 散列算法。如果您想继续使用 2.x 中使用的 SHA1 哈希,请在您的身份验证器配置中使用 'passwordHasher' => 'Weak'。

请参阅:http ://book.cakephp.org/3.0/en/appendices/3-0-migration-guide.html#authcomponent了解更多信息。

于 2015-06-18T05:32:45.810 回答
1

来自CakePHP 3 迁移指南

  • Default 现在是身份验证类使用的默认密码哈希。它专门使用 bcrypt 散列算法。如果您想继续使用 2.x 中使用的 SHA1 哈希,请在您的身份验证器配置中使用 'passwordHasher' => 'Weak'。
  • 添加了一个新的 FallbackPasswordHasher,以帮助用户将旧密码从一种算法迁移到另一种算法。查看 AuthComponent 的文档以获取更多信息。

阅读AuthComponent 文档显示了一个与此类似的示例:

$this->loadComponent('Auth', [
    'authenticate' => [
        'Form' => [
            'passwordHasher' => [
                'className' => 'Fallback',
                'hashers' => ['Default', 'Weak']
            ]
        ]
    ]
]);

当用户登录 AuthComponent 将使用Fallback密码哈希类时,它将首先尝试Default哈希方法(在上面的代码中使用),然后是Weak哈希。

该文档还继续向您展示如何在登录时更新用户密码以使用更安全的Default哈希。

于 2015-06-18T05:49:12.607 回答