Cakephp 4.x CMS教程认证重定向问题
Cakephp 4.x CMS tutorial authentication redirect problem
我刚刚重新创建了 CMS 教程。当未登录并尝试编辑文章时,我被重定向到登录页面。成功在这里。
登录后,我被重定向到一个错误页面。为什么?重定向回文章出错了。见下文
Link 重定向到:http://localhost/test_cms/test_cms/articles/edit/first-post
Link 它应该重定向到 http://localhost/test_cms/articles/edit/first-post
我根据 CMS 教程处理了每个文件,但似乎无法弄清楚这里出了什么问题。
登录代码如下:
public function login()
{
$this->Authorization->skipAuthorization();
$this->request->allowMethod(['get', 'post']);
$result = $this->Authentication->getResult();
// regardless of POST or GET, redirect if user is logged in
if ($result->isValid()) {
// redirect to /articles after login success
$redirect = $this->request->getQuery('redirect', [
'controller' => 'Articles',
'action' => 'index',
]);
return $this->redirect($redirect);
}
// display error if user submitted and authentication failed
if ($this->request->is('post') && !$result->isValid()) {
$this->Flash->error(__('Invalid username or password'));
}
}
看起来您在代码中的某处设置了额外的 test_cms
,或者用于未授权操作的 URL 可能会在您的 redirect
查询参数中附加额外的 test_cms
。您可以通过在 if
条件中打印 $redirect
变量来调试它。
您可能还想添加一些屏幕截图、错误消息、堆栈跟踪以及您的问题,这可以帮助人们理解问题以获得更准确的答案。
我也遇到了这个问题,并找到了解决方法。在我的 UsersController 登录方法中,在 return $this->redirect($redirect);
之前,我添加了这段代码来去除多余的应用程序名称:
if (!is_array($redirect)) {
if (strncmp($redirect, '/myapp', 6) === 0) {
$redirect = substr($redirect, 6);
}
}
同样,这是一种解决方法。
我刚刚重新创建了 CMS 教程。当未登录并尝试编辑文章时,我被重定向到登录页面。成功在这里。 登录后,我被重定向到一个错误页面。为什么?重定向回文章出错了。见下文
Link 重定向到:http://localhost/test_cms/test_cms/articles/edit/first-post
Link 它应该重定向到 http://localhost/test_cms/articles/edit/first-post
我根据 CMS 教程处理了每个文件,但似乎无法弄清楚这里出了什么问题。
登录代码如下:
public function login()
{
$this->Authorization->skipAuthorization();
$this->request->allowMethod(['get', 'post']);
$result = $this->Authentication->getResult();
// regardless of POST or GET, redirect if user is logged in
if ($result->isValid()) {
// redirect to /articles after login success
$redirect = $this->request->getQuery('redirect', [
'controller' => 'Articles',
'action' => 'index',
]);
return $this->redirect($redirect);
}
// display error if user submitted and authentication failed
if ($this->request->is('post') && !$result->isValid()) {
$this->Flash->error(__('Invalid username or password'));
}
}
看起来您在代码中的某处设置了额外的 test_cms
,或者用于未授权操作的 URL 可能会在您的 redirect
查询参数中附加额外的 test_cms
。您可以通过在 if
条件中打印 $redirect
变量来调试它。
您可能还想添加一些屏幕截图、错误消息、堆栈跟踪以及您的问题,这可以帮助人们理解问题以获得更准确的答案。
我也遇到了这个问题,并找到了解决方法。在我的 UsersController 登录方法中,在 return $this->redirect($redirect);
之前,我添加了这段代码来去除多余的应用程序名称:
if (!is_array($redirect)) {
if (strncmp($redirect, '/myapp', 6) === 0) {
$redirect = substr($redirect, 6);
}
}
同样,这是一种解决方法。