CakePhp错误的Auth重定向

时间:2014-10-14 14:50:40

标签: cakephp authentication

我刚开始学习Auth组件,我遇到了重定向问题。我的本地应用程序的路径是:localhost / school但是当一个登录用户试图访问一个URL时,他不允许该站点重定向到localhost / school / school并且它显示“所请求的地址'/ school / school /'在这服务器上没找到”。发生这种情况时,我不希望重定向,只是在同一页面中显示“您不被允许”或者可能重定向到特定的错误页面,我该怎么办?我没有登录或注销重定向的问题,只有我之前说的。这是我的App Controller:

public $ components = array(         '的ACL',         'Auth'=>阵列(             'authorize'=>阵列(                 '行动'=>数组('actionPath'=>'控制器')             )         )         “会话”     );     public $ helpers = array('Html','Form','Session');

public function beforeFilter() {
    //Configure AuthComponent

    $this->Auth->loginAction = array(
        'controller' => 'users',
        'action' => 'login'
    );
    $this->Auth->logoutRedirect = array(
        'controller' => 'users',
        'action' => 'login'
    );

    $this->set('current_user',$this->Auth->User());
    $this->Auth->authError = "You're not allowed.";
}

4 个答案:

答案 0 :(得分:0)

如果您不允许某人访问某个页面,那么您希望控制器在请求时执行什么操作?

例如,您可以使用以下命令设置重定向:

$this->redirect(array(
'controller'=>'users', 
'action' => 'login'));`

您可以使用Session::setFlash();

显示消息

答案 1 :(得分:0)

当您没有此操作的权限时,

localhost / projectName / projectName是重定向。我有同样的问题。我在'Actions' => array('actionPath' => 'controllers') )中评论$components。之后我通过执行以下代码设置aros_acos

$group = $this->User->Group->read(null,'1');
$this->Acl->allow($group, 'controllers/Users/controlPanel');

之后我取消注释代码,并在操作'controlPanel'并且错误消失:)我不知道如何更改此重定向,但如果我在aros_acos中有记录,一切正常。

答案 2 :(得分:0)

我有同样的问题,我解决了。

在AppController中尝试此代码

    public function beforeFilter() {

    //Configure AuthComponent

// note just these two lines
    $this->Auth->unauthorizedRedirect=FALSE ;
    $this->Auth->authError="Access Denied";


    $this->Auth->loginAction = array(
    'controller' => 'users',
    'action' => 'login'
    );
    $this->Auth->logoutRedirect = array(
    'controller' => 'users',
    'action' => 'login'
    );
    $this->Auth->loginRedirect = array(
    'controller' => 'posts',
    'action' => 'add'
    );

    $this->Auth->allow('display');
    //$this->Auth->allow();




    }

答案 3 :(得分:0)

class AppController extends Controller {

// added the debug toolkit
// sessions support
// authorization for login and logut redirect
public $components = array(
    'Session','Flash',
    'Auth' => array(
        'loginRedirect' => array('controller' => 'users', 'action' => 'index'),
        'logoutRedirect' => array('controller' => 'users', 'action' => 'login'),
        'authError' => 'You must be logged in to view this page.',
        'loginError' => 'Invalid Username or Password entered, please try again.'

    ));

// only allow the login controllers only
public function beforeFilter() {
    $this->Auth->allow('login');
}

public function isAuthorized($user) {
    // Here is where we should verify the role and give access based on role

    return true;
}

}

在您的控制器中它应该是这样的:

class UsersController extends AppController {

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



public function login() {

    //if already logged-in, redirect
    if($this->Session->check('Auth.User')){
        $this->redirect(array('action' => 'index'));        
    }

    // if we get the post information, try to authenticate
    if ($this->request->is('post')) {
        if ($this->Auth->login()) {
            $this->Flash->set(__('Welcome, '. $this->Auth->user('username')));
            $this->redirect($this->Auth->redirectUrl());
        } else {
            $this->Flash->set(__('Invalid username or password'));
        }
    } 
}