【问题标题】:CakePHP simple authentication tutorial is not workingCakePHP 简单认证教程不起作用
【发布时间】:2013-10-11 15:19:03
【问题描述】:

我是 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;
    }
}

【问题讨论】:

  • 你能发布你的用户模型的代码吗?
  • @Andrew,当然。我已经编辑了我的问题。
  • 尝试调试 $this->Auth->login() 方法。加密密码应与数据库中的密码相同。

标签: cakephp authentication controller cakephp-2.4


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

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

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

    public $components = array('Auth');

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

    【讨论】:

    • 根据食谱教程,$components = array('Auth'); 已经在AppController 中定义。
    • 您需要 1) 确保实际的 AppController 文件包含该组件。 2)上面的原始帖子显示您正在用户模型中加载身份验证组件,这是不正确的。组件对应的是控制器,而不是模型。
    • cookbook 教程本身显示 Auth 组件同时加载到用户模型和 AppController 中。不管怎样,我把它从模型中删除了。
    【解决方案3】:

    我认为,您错过了一些东西,请确保您已按照 CakePHP 2.x http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html 的相同教程进行操作。

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

    【讨论】:

      【解决方案4】:

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

      'authenticate' => array(
         'Form' => array(
             'passwordHasher' => 'Blowfish'
         )
      )
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多