【问题标题】:CakePHP : 2.4 Auth component not workingCakePHP:2.4 Auth 组件不起作用
【发布时间】:2014-12-19 10:42:46
【问题描述】:

我有一个表名 admin_users,我想在这里使用 auth 组件。所以我在appcontroller中写了下面的代码

public $components = array('Session','RequestHandler','Paginator'=>array('limit'=>4),'Auth'=>array(
        'loginAction'=>array(
        'controller'=>'adminusers',
        'action'=>'login'
        ),
        'loginRedirect' => array('controller' => 'adminusers','action' => 'index'),
        'logoutRedirect' => array('controller' => 'adminusers','action' => 'index'),
        'authError'=>'You can not access this page!!',
        )); 

对于我写的设置用户

public function beforeFilter() {
    //$this->Auth->allow();
    $this->set('logged_in', $this->Auth->loggedIn());
    $this->set('current_user',$this->Auth->user());
     parent::beforeFilter();
        $this->Paginator->settings = array(
          'limit'=>4
    );


}

我在 adminusers 控制器中做了一个方法调用 login()

    public function login() {
    $this->layout = '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'));
    }
}

对于我在 AdminUser 模型中编写的哈希密码

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

这是 login.ctp

              <?php echo $this->Form->create('AdminUser'); ?>
              <?php 
                    echo $this->Form->input('username',array(
                        'label' => false,
                        'placeholder'=>'UserName'
                        ));
                    echo $this->Form->input('password',array(
                        'label' => false,
                        'placeholder'=>'Password'
                    ));
                ?>

使用allow()方法后,我创建了一个用户,这里的哈希密码工作正常。但问题是当我尝试登录时,它给了我“无效的用户名或密码,再试一次”。

【问题讨论】:

  • 你检查了它生成的查询吗?并检查哈希密码
  • 您的控制器名称似乎不常规:adminusers 也应该是 admin_users。

标签: cakephp cakephp-2.4


【解决方案1】:

在您的 login.ctp 中创建一个 AdminUser。 Auth 默认使用模型“用户”。 您需要在 Auth-Component 中定义自己的模型。

 public $components = array('Session','RequestHandler','Paginator'=>array('limit'=>4),'Auth'=>array(
    'loginAction'=>array(
    'controller'=>'adminusers',
    'action'=>'login'
    ),
    'loginRedirect' => array('controller' => 'adminusers','action' => 'index'),
    'logoutRedirect' => array('controller' => 'adminusers','action' => 'index'),
    'authError'=>'You can not access this page!!',
    'authenticate' => array(
        'Form' => array(
            'userModel' => 'AdminUser',
            'passwordHasher' => 'Blowfish'
        )
    )
)); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 2019-09-12
    • 2016-02-16
    相关资源
    最近更新 更多