代码之家  ›  专栏  ›  技术社区  ›  TechFanDan

授权的公共URL不断重定向以进行身份验证,但失败了

  •  0
  • TechFanDan  · 技术社区  · 8 年前

    OurCustomAuth 当前正在返回预期值 ,正在到达适当的else,但 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检测到服务器正在重定向请求 这个地址永远不会完整。

    $this->Auth->login() /users/error 不应导致重定向,因为它被排除在身份验证之外。

    1 回复  |  直到 8 年前
        1
  •  1
  •   ndm    8 年前

    问题是,你在每次请求时都会运行登录代码,即在应用程序控制器中 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')) {
            // ...
        }
    }