2014-03-29 78 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版本。