cakephp - CakePHP 4 CMS 身份验证教程重定向到登录不起作用(忽略子文件夹)
问题描述
我正在尝试根据官方 CMS 教程实施身份验证:https ://book.cakephp.org/4/en/tutorials-and-examples/cms/authentication.html#adding-login
但是这里实现的重定向:
public function getAuthenticationService(ServerRequestInterface $request): AuthenticationServiceInterface
{
$authenticationService = new AuthenticationService([
'unauthenticatedRedirect' => '/users/login',
'queryParam' => 'redirect',
]);
没有按预期工作。
我的安装位于 example.com/project1/ 之类的子文件夹中,正确的完整 url 将是 example.com/project1/users/login 但是当尝试访问 example.com/project1/ 时,重定向指向 example.com/users/登录。
我也试过改变
$authenticationService = new AuthenticationService([
'unauthenticatedRedirect' => '/users/login',
'queryParam' => 'redirect',
至
$authenticationService = new AuthenticationService([
'unauthenticatedRedirect' => [controller => 'users', 'action' => index],
'queryParam' => 'redirect',
但这会导致
parse_url() 期望参数 1 是字符串,给定数组
错误
我必须如何设置重定向或在哪里可以更改 CakePHP 4 中的“BASEURL”?
解决方案
我发现了问题。
我根据@ndm的链接更改了代码:
$authenticationService = new AuthenticationService([
'unauthenticatedRedirect' => \Cake\Routing\Router::url('/users/login'),
'queryParam' => 'redirect',
导致无限重定向,因为我忘记将此函数添加到 UsersController:
public function beforeFilter(\Cake\Event\EventInterface $event)
{
parent::beforeFilter($event);
// Configure the login action to not require authentication, preventing
// the infinite redirect loop issue
$this->Authentication->addUnauthenticatedActions(['login']);
}
推荐阅读
- django - Django QuerySet,根据每个数据的列/字段的最新条目等于特定值过滤数据
- python - HttpNtlmAuth 缺少参数
- python - 如何将龙卷风服务器集成到伏顿代理中
- javascript - 无法让 Javascript 使用 JSON/Flask 响应来重定向到新页面并更新其 DOM
- java - Spark-submit 正在重新使用以前的 jar
- python - 使用 Dash 和 Plotly 更新表值
- java - 检查类是否实现给定类名的接口
- python - 在 Python Fabric 中使用具有哈希作为主要提示符的 shell
- javascript - 将数组迭代中的字符串传递到 ajax 数据中。服务器端数据为空
- three.js - three.js 拾取对象缩放以适合对象