在这种情况下,
OurCustomAuth
当前返回预期值false,正在达到适当的其他值,但users/error
路径不断重定向,即使它已公开并且不需要任何身份验证。
我已经设置了新操作:
C:\wamp\myapp\app>Console\cake AclExtras.AclExtras aco_update
Welcome to CakePHP v2.4.9 Console
---------------------------------------------------------------
App : app
Path: C:\wamp\myapp\app\
---------------------------------------------------------------
Created Aco node: controllers/Users/error
Aco Update Complete
在
UsersController
中,我添加了要公开的操作:
public function beforeFilter() {
parent::beforeFilter ();
$this->Auth->allow ('logout', 'error');
}
在
AppController
中,Auth
配置:
public $components = array(
'Acl',
'Cookie',
'DebugKit.Toolbar', 'Session',
'Auth' => array(
'authenticate' => array('OurCustomAuth'),
'loginAction' => array('controller' => 'users', 'action' => 'view'),
'authError' => 'Did you really think you are allowed to see that?',
'authorize' => array('Actions' => array('actionPath' => 'controllers'))
)
);
...
public function beforeFilter() {
...
//Auto logging users in if they are not logged in
if (!AuthComponent::user('id')) {
if ($this->Auth->login()) {
//stuff here
} else {
$this->Session->setFlash(__('We could not authenticate you ...'));
return $this->redirect(array('controller' => 'Users', 'action' => 'error'));
}
}
...
}
我在 Firefox 中遇到的错误:
页面未正确重定向
Firefox 检测到服务器正在重定向请求 这个地址以一种永远不会完成的方式。
更新#1
$this->Auth->login()
本质上是抓取请求标头,在这种情况下是故意错误的,这似乎重定向到适当的链接。但是,/users/error
不应导致重定向,因为它被排除在身份验证之外。
问题是您在每个请求上运行登录代码,即在应用程序控制器
beforeFilter()
方法中。因此,当该代码因您未登录而将您重定向到 /users/error
时,该代码将针对该控制器/操作再次运行,并一次又一次地重定向您...
如果您需要为每个请求运行此代码,那么您必须手动检查允许的操作,即通过
$this->Auth->allow()
允许的操作,并仅在当前操作不允许的情况下运行您的代码。检查AuthComponent::_isAllowed()
的代码,只需很少的修改即可轻松使用:
$action = strtolower($this->request->params['action']);
if (!in_array($action, array_map('strtolower', $this->Auth->allowedActions))) {
//Auto logging users in if they are not logged in
if (!AuthComponent::user('id')) {
// ...
}
}