CakePHP2.0和基本身份验证

CakePHP2.0和基本身份验证,php,cakephp,cakephp-2.0,Php,Cakephp,Cakephp 2.0,我已经将我的应用程序从CakePHP 1.3升级到2.0.4 以前,我只能在一个控制器中使用安全组件模拟基本HTTP身份验证 我曾经做过这样的事情: $this->Auth->allow(array('*')); $this->Security->loginOptions = array('type'=>'basic','realm'=>'api'); $this->Security->loginUsers = array("api"=>"1

我已经将我的应用程序从CakePHP 1.3升级到2.0.4

以前,我只能在一个控制器中使用安全组件模拟基本HTTP身份验证

我曾经做过这样的事情:

$this->Auth->allow(array('*'));
$this->Security->loginOptions = array('type'=>'basic','realm'=>'api');
$this->Security->loginUsers = array("api"=>"123");
$this->Security->requireLogin();
public $components = array(
    'Auth' => array(
        'authenticate' => array('Basic')
    )
);
现在SecurityComponent不再处理基本身份验证和摘要身份验证,我需要这样做:

$this->Auth->allow(array('*'));
$this->Security->loginOptions = array('type'=>'basic','realm'=>'api');
$this->Security->loginUsers = array("api"=>"123");
$this->Security->requireLogin();
public $components = array(
    'Auth' => array(
        'authenticate' => array('Basic')
    )
);

但当我在ApicController上使用它时,它会重定向到我的登录表单/users/login。我缺少什么吗?

您需要使用登录操作配置AuthComponent。你应该看看蛋糕册上的那一节

您的设置可能与此类似:

public $components = array(
  'Auth'=> array(
    'loginAction' => array(
      'controller' => 'api',
      'action'     => 'login'
    ),
    'loginRedirect' => array(
      'controller' => 'api',
      'action'     => 'logged_on'
    ),
    'authenticate' => array(
      'Basic' => array(
        'realm' => 'api'
      )
    )
  )
);
另外,应该注意的是,Cake不再支持使用loginUsers属性定义用户。您可能需要扩展BasicAuthenticate类并重写它的getUser()方法