簡體   English   中英

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()返回false。 但是問題可能出在任何地方。

問題是密碼散列。 一旦知道答案就很容易。

我盡可能地添加了Authentication guide中建議的Simple password hashing組件:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM