简体   繁体   English

Cakephp 2 ACL ERR_TOO_MANY_REDIRECTS

[英]Cakephp 2 ACL ERR_TOO_MANY_REDIRECTS

I am trying to implement the ACL tutorial found here: http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html 我正在尝试实施此处的ACL教程: http : //book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html

I followed all the instructions, however when I try to go to [my_site]/users/add ERR_TOO_MANY_REDIRECTS error. 我遵循了所有说明,但是当我尝试转到[my_site] / users / add ERR_TOO_MANY_REDIRECTS错误时。

I found this on the Cakephp website: 我在Cakephp网站上找到了这个:

This happens when for example we have an element that receives data from a method of a controller, and the implementation of this method requires conducting the login would create an infinite loop that eventually will cause the browser to decline redirects 例如,当我们有一个元素从控制器的方法接收数据时,就会发生这种情况,并且该方法的实现要求进行登录会创建一个无限循环,最终将导致浏览器拒绝重定向

And they suggest this as the fix: 他们建议将此作为解决方法:

function  beforeFilter ()  { 
 $ this -> Auth -> allow ( 'CONTROLLER_NAME' ); 
}

Which doesn't seem to work. 这似乎不起作用。

If I change the AppController from this: 如果我从这里更改AppController:

public function beforeFilter() {
    $this->Auth->allow('index', 'view', 'login', 'add');
}

to: 至:

public function beforeFilter() {
    $this->Auth->allow('*');
}

I dont get the error anymore but get redirected to [my_site]/users/login 我不再收到错误,但重定向到[my_site] / users / login

Any suggestions as to what I am doing wrong that I can't view the User-Add page? 关于我做错了什么的任何建议,导致我无法查看“用户添加”页面? TIA! TIA!

UserController: UserController的:

public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('add');
    }

Login function (UsersController): 登录功能(UsersController):

Public function login() {
    if ($this->request->is('post')) {
        if ($this->Auth->login()) {
            $this->redirect($this->Auth->redirect());
        } else {
            $this->Session->setFlash(__('Invalid username or password, try again'));
        }
    }
}

Auth Component Loader: 身份验证组件加载程序:

public $components = array(
        'Session',
        'RequestHandler',
        'Auth' => array(
            'loginRedirect' => array('controller' => 'projects', 'action' => 'index'),
            'logoutRedirect' => array('controller' => 'pages', 'action' => 'display', 'home')
        )
    );

The error you are getting has nothing to do with ACL, but with the Auth component denying access to your UsersController add and login functions, to which it is trying to redirect the user. 您收到的错误与ACL无关,但与Auth组件拒绝访问UsersController add和login函数有关,后者试图将用户重定向到该错误。 Make sure the add and login functions are public, by adding this line in your UsersController (rather than your AppController ): 通过将以下行添加到UsersController (而不是AppController )中,确保addlogin函数是公共的:

public function beforeFilter() {
    $this->Auth->allow(array('add', 'login'));
}

The loop you are now encountering is because the add and login functions are not public and therefor the loop looks like: add -> Unauthorized -> login -> Unauthorized -> login ... and so on. 您现在遇到的循环是因为添加和登录功能不是公开的,因此循环看起来像: add -> Unauthorized -> login -> Unauthorized -> login ...等。

Please change your beforeFilter() 's Auth->allow('_CONTROLLER_NAME') to: 请将您的beforeFilter()Auth->allow('_CONTROLLER_NAME')更改为:

public function beforeFilter(){
    $this->Auth->allow();
}

Hope that works for you! 希望对您有用!

I finally managed to solve the problem with help Brian: 我终于在布莱恩的帮助下解决了这个问题:

Do you have an requestAction() code? 您是否有一个requestAction()代码? If so, try adding this in 如果是这样,请尝试在其中添加

AppController::beforeFilter() 

if (isset($this->params['requested'])) $this->Auth->allow($this->action); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM