【问题标题】:Cakephp 2.0 and basic authCakephp 2.0 和基本身份验证
【发布时间】:2012-01-16 03:42:46
【问题描述】:

我已将我的应用程序从 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();

现在 SecurityComponent 不再处理基本和摘要身份验证,我需要做这样的事情:

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

但是当我在我的 ApiController 上使用它时,它会重定向到我在 /users/login 的登录表单。我错过了什么吗?

【问题讨论】:

    标签: php cakephp cakephp-2.0


    【解决方案1】:

    您需要使用登录操作配置 AuthComponent。您应该查看 Cake book 中关于 Configuring Authentication handlers 的部分。

    您的设置可能类似于以下内容:

    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() 方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-06
      • 2021-02-10
      • 1970-01-01
      • 2011-01-28
      • 2013-11-10
      • 2013-01-13
      相关资源
      最近更新 更多