CakePHP简单身份验证教程不起作用

时间:2013-10-11 15:19:03

标签: cakephp authentication controller cakephp-2.4

我是CakePHP的新手。我正在尝试使用CakePHP 2.4.1 the Cake cookbook tutorial of simple authentication 我总是收到“无效的用户名或密码,请再试一次”,尽管我输入了正确的用户名和密码。

public function login() {
    if ($this->request->is('post')) {
        if ($this->Auth->login()) {
            return $this->redirect($this->Auth->redirect());
        }
        $this->Session->setFlash(__('Invalid username or password, try again'));
    }
}

根据the AuthComponent API,如果Auth->login()的参数为空或未指定,则该请求将用于标识用户。 调试查询输出显示

SELECT `User`.`id`, `User`.`username`, `User`.`password`, `User`.`role`, `User`.`created`, `User`.`modified` 
FROM `cake_dvdcatalog`.`users` AS `User` WHERE `User`.`username` = 'admin' LIMIT 1

这是我的用户模型:

// app/Model/User.php
App::uses('AuthComponent', 'Controller/Component');

class User extends AppModel {

    public $validate = array(
        'username' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'A username is required'
            )
        ),
        'password' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'A password is required'
            )
        ),
        'role' => array(
            'valid' => array(
                'rule' => array('inList', array('admin', 'author')),
                'message' => 'Please enter a valid role',
                'allowEmpty' => false
            )
        )
    );

    public function beforeSave($options = array()) {
        if (isset($this->data[$this->alias]['password'])) {
            $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
        }
        return true;
    }
}

4 个答案:

答案 0 :(得分:1)

您必须检查加密密码, Cakephp默认情况下使用SHA1,因此使用带有SHA1密码的Cakephp创建用户,在用户模型中添加 beforesave 方法来加密密码

答案 1 :(得分:0)

auth组件进入控制器而不是模型。尝试从模型中删除它:

App::uses('AuthComponent', 'Controller/Component');

并将其添加到AppController。同时将其添加到app控制器:

public $components = array('Auth');

如果要使用非默认设置,可能还需要一些其他配置。

答案 2 :(得分:0)

我想,你错过了一些东西,请确保你已经遵循了CakePHP 2.x http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html的相同教程。

完成所有配置后,请在http://localhost/application_name/users/add上创建用户,并尝试使用此类用户。它对我有用。

答案 3 :(得分:0)

检查&AppController.php'中的$ components数组中是否有以下项目。文件:

'authenticate' => array(
   'Form' => array(
       'passwordHasher' => 'Blowfish'
   )
)
相关问题