首页 文章

如何在CakePHP 2.0中再次使用身份验证?

提问于
浏览
0

将功能齐全的Cake 1.3应用程序迁移到最近发布的2.0版本后,身份验证已停止运行 .

我根据更新的2.0手册改变了 AuthComponent 的调用和 login 动作的结构,但无济于事 . 奇怪的是用户实际上是由 $this->Auth->login() 验证的,因为它到达登录功能的一部分,其中用户被重定向到由 $this->Auth->redirect() 设置的URL . 然而,在重定向之后, $this->Auth->user() 返回空(以及 AuthComponent::user() )并且用户未通过Auth组件登录 .

Cake在此过程中不会抛出任何错误,'auth'的flash消息仍为空 .

用户存储在一个包含id,username,email,password和timestamp列的简单数据库表中 . 密码是哈希的,我已经使用新的Cake 2.0方法添加了一些用户 .

这是AppController.php的代码:

<?php
  class AppController extends Controller {
    public $helpers = array('Session', 'Html', 'Time', 'Form', 'Text');
    public $components = array('Session', 'RequestHandler', 'Auth');    

    public function beforeFilter() {
        $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
        $this->Auth->loginRedirect = array('controller' => 'maps', 'action' => 'index');
        $this->Auth->logoutRedirect = array('controller' => 'maps', 'action' => 'index');
    }
  }
?>

UserController.php:

<?php
  class UsersController extends AppController {
    public $name = 'Users';

    function beforeFilter() {
      parent::beforeFilter();
    }

    function login() {
      if ($this->request->is('post')) {
        if ($this->Auth->login()) {
          return $this->redirect($this->Auth->redirect());
        }
      }
    }

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

User.php模型 . 我解决了这个问题后暂时禁用了表单验证:

<?php
  class User extends AppModel {
    public $name = 'User';
  }
?>

登录视图:

<?php
    echo $this->Form->create('User');
    echo $this->Form->input('username', array('label' => 'Username', 'before' => '<p class="input" id="username">', 'after' => '</p>', 'between' => '
', 'div' => false)); echo $this->Form->input('password', array('label' => 'Password', 'before' => '<p class="input" id="password">', 'after' => '</p>', 'between' => '
', 'div' => false)); echo $this->Form->end('Login'); ?>

我还尝试在 AppController 中的 $components 变量中设置一些Auth功能,这些功能也不起作用:

public $components = array(
    'Auth'=> array(
      'loginAction' => array(
        'controller' => 'users',
        'action' => 'login',
      ),
      'loginRedirect' => array(
        'controller' => 'maps',
        'action' => 'index',
      ),
      'authenticate' => array(
        'Form' => array(
          'fields' => array('username', 'password')
        )
      )
    )
  );

是什么原因引起了这个问题?路由可能吗?我已经评论了所有路线,除了:

require CAKE . 'Config' . DS . 'routes.php';

UPDATE: 在UsersController的login方法中添加一些调试语句后,我现在知道 $this->Auth->user() 实际上是在调用 $this->Auth->login() 之后填充了正确的用户 . 然而,在重定向到另一个控制器之后,登录会话完全丢失 . 所以我仍然不会在这里出错 .

UPDATE 2 我已经重新启动了迁移过程,方法是使用我的1.3应用程序并在其上运行迁移控制台脚本,就像我上次一样 .

这次我注意到脚本由于与自定义组件相关的两个错误而停止 . 组件类现在应该扩展Component,而不是1.3 default:Object .

在修复这些组件错误之后,我再次运行了迁移脚本(我在第一次迁移尝试期间忽略了这一点)并实现了新的AuthCompenent调用 . 到目前为止,一切似乎都正常 . 不确定现在有什么不同,第一次出了什么问题,因为Cake没有输出任何错误消息 .

UPDATE 3 它's getting weirder. I thought I solved it, but after transferring my code to another development machine Auth suddenly stops working. It'正在我的主要设置上工作,但在另一个测试时,它会在相同的情况下再次失败 . 我've cleared the cache to be sure, but it still isn'工作 . Cake不会生成任何错误输出 .

UPDATE 4 这似乎是我机器上的会话问题 . 我've just set the Session to be stored in a cookie and suddenly Auth starts working again. Not sure why the default Session isn' t工作,我不知道在那种情况下从哪里开始调试 .

只有cookie会话似乎有效,定义数据库会话与常规会话具有相同的结果; Auth停止工作 .

2 回答

  • 1

    尝试在/Config/core.php中启用use_trans_sid:

    Configure::write('Session', array(
        //'defaults' => 'php'
        'defaults' => 'cake',
        'cookie' => 'CAKEPHP2',
        'ini' => array('session.use_trans_sid' => true)
    ));
    
  • 0

    您是否还尝试配置身份验证处理程序?

    public $components = array(
        'Auth'=> array(
            'authenticate' => array('Form')
        )
    );
    

相关问题