我正在做一个页面,试图为AppController中的auth组件设置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());
}}
我是UsersController
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;
}它只重定向到登录页面。
发布于 2019-09-11 07:47:35
来自文档
unauthorizedRedirect控制处理未经授权的访问。默认情况下,未经授权的用户被重定向到引用者URL或loginAction或‘/’。如果设置为false,则抛出ForbiddenException异常,而不是重定向。
unauthorizedRedirect选项仅适用于经过身份验证的用户。如果经过身份验证的用户试图转到他们未被授权访问的URL,他们将被重定向回引用程序。通过指定unauthorizedRedirect,您现在将用户重定向到指定的URL,而不是引用者。
如果要在错误的登录尝试中重定向用户,则必须在登录方法中手动进行重定向。
希望能消除任何疑虑。
https://stackoverflow.com/questions/57883162
复制相似问题