2014-03-29 81 views
1

我試圖首次使用cakephp,我寫的代碼不允許我登錄任何用戶。用戶創建工作正常,因爲我是能夠查詢我的數據庫並輸出用戶名和散列密碼。我不確定我是否在某處丟失了一行代碼,或者我可能沒有將數據庫中的密碼字段設置得足夠大。我已經擺弄我的數據庫變量,但改變大小和類型都似乎沒有幫助。CakePHP不允許以任何身份驗證登錄

的AppController:

class AppController extends Controller 
{ 
public $helpers = array('Html'); 

public $components = array(
    'Session', 
    'Auth' => array(
     'loginRedirect' => array(
      'controller' => 'login', 
      'action' => 'index' 
     ), 
     'logoutRedirect' => array(
      'controller' => 'home', 
      'action' => 'index', 'home' 
     ), 
     'Auth' => array('authorize' => 'Controller'), 
     'authenticate' => array(
      'Form' => array('passwordHasher' => 'Blowfish') 
     ) 
    ) 
); 

public function beforeFilter() 
{ 
    $this->Auth->allow('index', 'view'); 
    $this->set('loggedin', $this->Auth->loggedIn()); 
} 

public function isAuthorized($user) 
{ 
    if(($this->Auth->user('id')) === $user['user_id']) 
    { 
     return true; 
    } 
    else 
    { 
     $this->Session->setFlash(__('Please log in to view this page')); 
     return false; 
    } 
} 

}

我UsersController:

class UsersController extends AppController 
{ 
public $helpers = array('HTML', 'Form'); 

public function beforeFilter() 
{ 
    parent::beforeFilter(); 
    $this->Auth->allow('add'); 
} 

public function index() 
{ 
    $this->set('users', $this->User->find('all')); 
} 

public function add() 
{ 
    if($this->request->is('post')) 
    { 
     $this->User->create(); 

     if($this->User->save($this->data)) 
     { 
      $this->Session->setFlash(__('Created new user')); 
      return $this->redirect(array('controller' => 'users', 'action' => 'login')); 
     } 
     $this->Session->setFlash(__('Could not create new user. Please try again.')); 
    } 
} 

public function login() 
{ 
    //$this->set('allUsers', $this->User->query("SELECT `User`.`user_id`, `User`.`username`, 
    //`User`.`password` FROM `ead44db`.`users` AS `User` WHERE 1 = 1")); 

    if($this->request->is('post')) 
    { 
     if($this->Auth->login()) 
     { 
      return $this->redirect($this->Auth->redirect()); 
     } 
     $this->Session->setFlash(__('Incorrect user name or password, please try again.')); 
    } 
} 

public function logout() 
{ 
    return $this->redirect($this->Auth->logout()); 
} 

}

我的用戶模型:

<?php 

App::uses('BlowfishPasswordHasher', 'Controller/Component/Auth'); 

class User extends AppModel 
{ 
public $validate = array('username' => array('required' => array('rule' => 'notEmpty')), 
    'password' => array('required' => array('rule' => 'notEmpty'))); 

/*public $hasMany = array('Review' => array('className' => 'Review', 'foreignKey' => 'reviewer_id'), 
    'Message' => array('className' => 'Message', 'foreignKey' => 'user_to_id'), 
    'Message' => array('className' => 'Message', 'foreignKey' => 'user_from_id'), 
    'Comment' => array('className' => 'Comment', 'foreignKey' => 'commenter_id'));*/ 

public $hasMany = array('Review', 'Message', 'Comment'); 

public function beforeSave($options = array()) 
{ 
    if(isset($this->data['User']['password'])) 
    { 
     $passwordHasher = new BlowfishPasswordHasher(); 
     $this->data['User']['password'] = $passwordHasher->hash($this->data['User']['password']); 
    } 
    return true; 
} 
} 

?> 

並可爲用戶我的數據庫表:

CREATE TABLE users (
user_id int AUTO_INCREMENT PRIMARY KEY, 
username varchar(50) UNIQUE, 
password TEXT NOT NULL 
); 
+0

我目前有完全相同的問題。我開始認爲這是一個錯誤。如果我手動比較哈希,我可以看到我輸入了正確的密碼,但登錄不起作用。 – IchBinKeinBaum

回答

0
public $components = array(             
         'Auth' => array(
          'authenticate' => array(         
           'Form' => array(
            'passwordHasher' => 'Blowfish' 
           ) 
          ), 
          'loginRedirect' => array('controller' => 'login', 'action' => 'index'), 
          'logoutRedirect' => array('controller' => 'home', 'action' => 'index', 'home'), 
          'authorize' => array('Controller') 
         ), 
         'Session' 
        ); 
1

似乎有在BlowfishPasswordHash.php

錯誤讀取

public function check($password, $hashedPassword) { 
    return $hashedPassword === Security::hash($password, 'blowfish', $hashedPassword); 
} 

它傳遞的散列版本來自用戶的密碼在數據庫中記錄爲「鹽」。這是沒有意義的。

應改爲

public function check($password, $hashedPassword) { 
    return $hashedPassword === Security::hash($password, 'blowfish', false); 
} 

這與生成的密碼哈希開始與「散列」的方法是一致的。

該錯誤似乎僅限於2.5.x Cake版本。