Cakephp 2.0 and basic auth

折月煮酒 提交于 2019-12-14 01:15:20

问题


I've upgraded my app from CakePHP 1.3 to 2.0.4.

Previously, I was able to use the Security component to emulate Basic HTTP authentication only in one controller.

I used to do something like this:

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

Now SecurityComponent no longer handles Basic and Digest Authentication and I need to do something like this:

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

But when I use this on my ApiController it redirects to my login form at /users/login. Am I missing something?


回答1:


You need to configure the AuthComponent with your login action. You should check out the section on Configuring Authentication handlers in the Cake book.

Your setup will probably look something similar to this:

public $components = array(
  'Auth'=> array(
    'loginAction' => array(
      'controller' => 'api',
      'action'     => 'login'
    ),
    'loginRedirect' => array(
      'controller' => 'api',
      'action'     => 'logged_on'
    ),
    'authenticate' => array(
      'Basic' => array(
        'realm' => 'api'
      )
    )
  )
);

Also, it should be noted that Cake no longer supports defining users using the loginUsers property. You would probably have to extend the BasicAuthenticate class and override it's getUser() method.



来源:https://stackoverflow.com/questions/8435482/cakephp-2-0-and-basic-auth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!