Cakephp 从 1.3 升级到 2 身份验证失败


cakephp upgrade from 1.3 to 2 authentication failure

我实际上已经想通了这一点,但是在与另一位开发人员进行头脑风暴之前,我找不到任何关于此的内容 - 跟踪核心代码以弄清楚发生了什么。

问题很简单 - 从 CakePHP v1.3 升级到 v2.5.9 后,登录(身份验证(不起作用。但是没有错误消息告诉您为什么它不起作用。

如 2.0 迁移指南中所述:

AuthComponent在2.0中完全重构,这样做是为了帮助减少开发人员的困惑和挫败感。此外,AuthComponent变得更加灵活和可扩展。您可以在身份验证指南中找到更多信息。

提到的身份验证指南很好地解释了您应该如何让它适用于新安装,但没有说明迁移需要做什么。

进一步的问题是,没有错误可以告诉您发生了什么。

我从身份验证指南中复制了UsersController.php -> login方法的代码 识别用户:

public function login() {
    if ($this->request->is('post')) {
        // Important: Use login() without arguments! See warning below.
        if ($this->Auth->login()) {
            return $this->redirect($this->Auth->redirectUrl());
            // Prior to 2.3 use
            // `return $this->redirect($this->Auth->redirect());`
        }
        $this->Session->setFlash(
            __('Username or password is incorrect'),
            'default',
            array(),
            'auth'
        );
    }
}

在我的AppController.php中,我有以下内容:

public $components = array(
    'Session', 'P28n', 'Store', 'SiteStore', 'UserAccessLevel', 'Auth'
);

然后在AppController.php -> beforeFilter

$this->Auth->authorize = array('Controller');
$this->Auth->loginError = __('Login failed, invalid username or password. Please try again.');
$this->Auth->authError = __('Please log-in.');
$this->Auth->allow('login', 'logout');

我唯一可以肯定的是,$this->Auth->login()返回的是假的。但问题可能是任何事情。

问题是密码的哈希。一旦你知道答案,就很容易。

我已经添加了身份验证指南中建议的简单密码哈希组件:

public $components = array(
    'Auth' => array(
        'authenticate' => array(
            'Form' => array(
                'passwordHasher' => array(
                    'className' => 'Simple',
                    'hashType' => 'sha256'
                )
            )
        )
    )
);

但这仍然失败了,但我无法确认密码哈希肯定是原因。将代码跟踪到密码肯定失败BaseAuthenticate::_findUser才能确认。

在这一点上,我试探了一下,CakePHP的密码哈希可以与Simple passwordHasher相匹配。

CakePHP 1.3 中的密码使用 sha1 保存,切换'hashType' => 'sha1'解决了这个问题:

public $components = array(
    'Auth' => array(
        'authenticate' => array(
            'Form' => array(
                'passwordHasher' => array(
                    'className' => 'Simple',
                    'hashType' => 'sha1'
                )
            )
        )
    )
);