首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Cakephp 3 unauthorizedRedirect不工作

Cakephp 3 unauthorizedRedirect不工作
EN

Stack Overflow用户
提问于 2019-09-11 06:18:17
回答 1查看 451关注 0票数 1

我正在做一个页面,试图为AppController中的auth组件设置AppController,但是没有工作,它什么也不做。

我试过把它装在假的上面,结果什么都没有用。

这是应用程序控制器

代码语言:javascript
复制
public function initialize()
{
    parent::initialize();

    $this->loadComponent('RequestHandler');
    $this->loadComponent('Flash');
    $this->loadComponent('Auth', [
        'loginRedirect' => [
            'controller' => 'Pages',
            'action' => 'display'
        ],
        'authError' => 'Seems like you have to use some kind of magic word.',
        'logoutRedirect' => [
            'controller' => 'Pages',
            'action' => 'display',
            'home'
        ],
        'unauthorizedRedirect' => [
            'controller' => 'Users',
            'action' => 'unauthorized'
        ],
    ]);

    //use model companies in all controllers
    $tableCategories = $this->loadModel('Categories');

    $categories = $tableCategories->find()
        ->contain([]);

    $this->set(compact('categories'));
}

public function beforeFilter(Event $event)
{
    $this->set('current_user', $this->Auth->user());
}

}

我是UsersController

代码语言:javascript
复制
class UsersController extends AppController

{ var $breadcrump = 'Usuarios';

代码语言:javascript
复制
public function beforeFilter(Event $event)
{
    parent::beforeFilter($event);
    $this->Auth->allow(['login', 'unauthorized']);
}

public function login()
{
    $this->viewBuilder()->layout('login');
    if ($this->request->is('post')) {
        $user = $this->Auth->identify();
        if ($user) {
            $this->Auth->setUser($user);
            return $this->redirect(['controller' => 'pages', 'action' => 'display']);
        }
        $this->Flash->error(__('Invalid username or password, try again'));
    }
}

public function logout()
{
    return $this->redirect($this->Auth->logout());
}

public function unauthorized()
{
    var_dump();
    $this->autoRender = false;

    $message = false;

    echo json_encode($message);exit;
}

它只重定向到登录页面。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-11 07:47:35

来自文档

unauthorizedRedirect控制处理未经授权的访问。默认情况下,未经授权的用户被重定向到引用者URL或loginAction或‘/’。如果设置为false,则抛出ForbiddenException异常,而不是重定向。

unauthorizedRedirect选项仅适用于经过身份验证的用户。如果经过身份验证的用户试图转到他们未被授权访问的URL,他们将被重定向回引用程序。通过指定unauthorizedRedirect,您现在将用户重定向到指定的URL,而不是引用者。

如果要在错误的登录尝试中重定向用户,则必须在登录方法中手动进行重定向。

希望能消除任何疑虑。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57883162

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档