我想使用CakePHP 3.8的身份验证插件,但是遇到了文档中没有的问题。
按照入门指南(https://book.cakephp.org/authentication/1/en/index.html)之后,我有一个问题。
最初指定了$ fields来更改真实数据库中的用户名和密码关系,与Auth组件相同,而登录URL是si加载登录表单的位置。
首先,在入门或文档的任何部分都没有提及登录视图(窗体),因此,就像旧的Auth Component一样,我将其创建到Users / login.ctp
<div class="users form">
<?= $this->Flash->render('auth') ?>
<?= $this->Form->create() ?>
<fieldset>
<legend><?= __('Please enter your email and password') ?></legend>
<?= $this->Form->input('email') ?>
<?= $this->Form->input('password') ?>
</fieldset>
<?= $this->Form->button(__('Login')); ?>
<?= $this->Form->end() ?>
</div>
我在Application.php中的代码包括以下内容(及其各自的用法和实现):
public function getAuthenticationService(ServerRequestInterface $request, ResponseInterface $response)
{
$service = new AuthenticationService();
$fields = [
'username' => 'email',
'password' => 'password'
];
// Load identifiers
$service->loadIdentifier('Authentication.Password', compact('fields'));
// Load the authenticators, you want session first
$service->loadAuthenticator('Authentication.Session');
$service->loadAuthenticator('Authentication.Form', [
'fields' => $fields,
'loginUrl' => '/users/login'
]);
return $service;
}
但是当我尝试登录时,出现此错误,在login.ctp中的var_dump之后,我得到了:
object(Authentication\Authenticator\Result)[124]
protected '_status' => string 'FAILURE_OTHER' (length=13)
protected '_data' => null
protected '_errors' =>
array (size=1)
0 => string 'Login URL `http://localhost/users/login` did not match `/users/login`.' (length=70)
如果我在'loginUrl' => '/users/login'
行中添加注释,则登录正常。
其他说明:
-我测试了哈希密码和textplane密码,结果相同。
-我在beforeFilter中添加了$this->Authentication->allowUnauthenticated(['view', 'index', 'login', 'add']);
来访问登录名。
-这是一个干净的cakephp 3.8安装,仅数据库对于测试是相同的。
-我仅在蛋糕控制台中添加了Crud。
我想了解更多有关loginURL的信息,我应该在UsersController中包括一些用法吗?是什么导致此错误?
谢谢
答案 0 :(得分:1)
错误消息目前有点误导,因为它没有向您显示可能的基本目录,这是您遇到的实际问题。我已经提出了a fix for that,可以在下一个版本中使用。
当应用程序位于子目录中时,您需要确保将登录URL配置考虑在内,即通过传递包含基本目录的URL,您可以手动执行以下操作:
'loginUrl' => '/myapp/users/login'
或使用路由器:
'loginUrl' => \Cake\Routing\Router::url('/users/login')
'loginUrl' => \Cake\Routing\Router::url([
'plugin' => null,
'prefix' => null,
'controller' => 'Users',
'action' => 'login'
])
另一种选择是使用基于路由的URL检查器,该检查器可以通过表单身份验证器urlChecker
选项进行配置,然后您可以使用URL数组定义登录URL,而不必使用路由器:>
'urlChecker' => 'Authentication.CakeRouter',
'loginUrl' => [
'plugin' => null,
'prefix' => null,
'controller' => 'Users',
'action' => 'login'
]
另请参阅: