【问题标题】:Internal Error with AuthComponent Logout in CakePHPCakePHP 中 AuthComponent 注销的内部错误
【发布时间】:2014-10-11 14:10:51
【问题描述】:

我在 Cakephp(版本 = 2.5.3)中实现身份验证时遇到问题。 登录功能工作正常,但每当我单击注销时,error.log 中都会生成以下异常

0 /opt/lampp/htdocs/v1/lib/Cake/Controller/Component/Auth/BaseAuthorize.php(72): ControllerAuthorize->controller(Object(UsersController))
1 /opt/lampp/htdocs/v1/lib/Cake/Controller/Component/AuthComponent.php(500): BaseAuthorize->__construct(Object(ComponentCollection), Array)
2 /opt/lampp/htdocs/v1/lib/Cake/Controller/Component/AuthComponent.php(462): AuthComponent->constructAuthorize()
3 /opt/lampp/htdocs/v1/lib/Cake/Controller/Component/AuthComponent.php(309): AuthComponent->isAuthorized(Array)
4 [internal function]: AuthComponent->startup(Object(UsersController))
5 /opt/lampp/htdocs/v1/lib/Cake/Utility/ObjectCollection.php(128): call_user_func_array(Array, Array)
6 [internal function]: ObjectCollection->trigger(Object(CakeEvent))
7 /opt/lampp/htdocs/v1/lib/Cake/Event/CakeEventManager.php(242): call_user_func(Array, Object(CakeEvent))
8 /opt/lampp/htdocs/v1/lib/Cake/Controller/Controller.php(675): CakeEventManager->dispatch(Object(CakeEvent))
9 /opt/lampp/htdocs/v1/lib/Cake/Routing/Dispatcher.php(187): Controller->startupProcess()
10 /opt/lampp/htdocs/v1/lib/Cake/Routing/Dispatcher.php(165): Dispatcher->_invoke(Object(UsersController), Object(CakeRequest))
11 /opt/lampp/htdocs/v1/app/webroot/index.php(108): Dispatcher->dispatch(Object(CakeRequest), Object(CakeResponse))
12 {main}

以下是登录和注销功能编码的不同文件

AppController.php

class AppController extends Controller {

public $components = array(
    'Session',
    'Auth' => array('authenticate' => array('Form' => array('passwordHasher' => 'Blowfish', 'userModel' => 'User','fields' => array('username' => 'user_name', 'password' => 'password'))),
                    'loginRedirect' => array('controller' => 'talks','action' => 'index'),
                    'logoutRedirect' => array('controller' => 'pages','action' => 'display','home'),
                    'loginAction' => array('controller' => 'users', 'action' => 'login' ),
                    'authorize' => array('Controller')
    )
);

public function beforeFilter() {
    $this->Auth->allow('index', 'view');
}

}

UsersController.php

App::uses('AppController', 'Controller');

class UsersController extends AppController {

//put your code here

public function beforeFilter() {
    parent::beforeFilter();
    $this->Auth->allow('add', 'login');
}

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

public function logout() {
    return $this->redirect($this->Auth->logout());
}

}

以下代码用于登录和注销链接的显示

<?php if(!$this->Session->read('Auth.User')){ ?>
LOGIN FORM GENERATION CODE
            <?php }else{
            echo $this->Html->link(
                        'LOGOUT', array(
                                       'controller' => 'users',
                                       'action' => 'logout',
                                       'full_base' => true)
                                  );
         } ?>

我也没有太多关于如何调试 Cakephp 应用程序的想法。因此,如果有人至少能指出我正确的调试方向也会非常有帮助。

在此先感谢 :)

【问题讨论】:

    标签: php cakephp authentication


    【解决方案1】:

    经过深思熟虑并浏览了 Caakephp API 文档后,我发现我没有覆盖 UsersController 中的 isAuthorized() 方法。

    我在我的控制器中添加了以下内容:

    public function isAuthorized() {
        return true;
    }
    

    我总是返回 true,因为我没有任何组。我只有一种类型的用户。

    【讨论】:

      猜你喜欢
      • 2016-11-03
      • 2014-02-07
      • 2013-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-19
      相关资源
      最近更新 更多