3

我是 CakePHP 的新手。我正在尝试使用 CakePHP 2.4.1进行简单身份验证的 Cake 食谱教程。
尽管我输入了正确的用户名和密码,但我总是收到“无效的用户名或密码,请重试”。

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

根据AuthComponent API,如果参数 toAuth->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

4 回答 4

1

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

于 2014-04-27T23:12:50.773 回答
0

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

'authenticate' => array(
   'Form' => array(
       'passwordHasher' => 'Blowfish'
   )
)
于 2014-12-29T07:24:13.110 回答
0

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

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

于 2013-10-12T01:39:41.377 回答
0

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

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

并将其添加到 AppController。还将它添加到应用程序控制器:

public $components = array('Auth');

如果您想使用非默认设置,您可能需要一些额外的配置。

于 2013-10-12T00:05:20.693 回答