Cakephp 3 未经授权重定向不起作用



我正在做一个页面,试图为 AppController 中的身份验证组件设置未经授权的重定向,但不起作用,它什么也不做。

我试过把它放在假的上,但没有任何效果

这是应用程序控制器

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());
}

}

这是用户控制器

class UsersController extends AppController

{ var $breadcrump = 'Usuarios';

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;
}

它只会重定向到登录页面

来自文档

unauthorizedRedirect控制对未授权访问的处理。默认情况下,未经授权的用户被重定向到引荐来源网址或登录操作或"/"。如果设置为 false,则会引发禁止异常异常,而不是重定向。

unauthorizedRedirect选项仅适用于经过身份验证的用户。如果经过身份验证的用户尝试转到他们无权访问的 URL,他们将被重定向回引荐来源网址。通过指定unauthorizedRedirect,您现在将用户重定向到指定的 URL,而不是引用。

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

希望能消除任何疑虑。

最新更新