Cakephp 2.0 and basic auth

I upgraded my application from CakePHP 1.3 to 2.0.4.

Previously, I could use the Security component to emulate basic HTTP authentication in only one controller.

I was doing 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 in / users / login. Did I miss something?

+2
source share
1 answer

You need to configure AuthComponent with your login action. You should check the Configure Authentication Handlers section of the Cake book.

Your setup will probably look something like 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'
      )
    )
  )
);

, , Cake loginUsers. , BasicAuthenticate getUser().

+1

All Articles