Cakephp 3身份验证插件,登录URL不匹配 [英] Cakephp 3 Authentication plugin, login URL did not match

查看:95
本文介绍了Cakephp 3身份验证插件,登录URL不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用CakePHP 3.8的身份验证插件,但是遇到了文档中没有的问题.

I want to use the Authentication plugin for CakePHP 3.8 and I'm having problems that are not in documentation.

按照入门指南( https://book.cakephp.org/authentication/1/en/index.html )我有一个问题.

After follow Getting Started (https://book.cakephp.org/authentication/1/en/index.html) I have one question.

最初指定了$ fields来更改真实数据库中的用户名和密码关系,与Auth组件相同,并且登录URL是si加载登录表单的位置.

Originally $fields were specified to change username and password relation in real database, same that Auth component, and login URL is where login form si loaded.

首先,在入门或文档的任何部分都没有提及登录视图(窗体),因此,就像旧的Auth Component一样,我将其创建到Users/login.ctp

First, in getting started or any part of documentation doesn't says about a login view (form), so, like old Auth Component I created this to 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中的代码包括以下内容(及其各自的用法和实现):

My code in Application.php includes this (with its respective uses and implements):

    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之后,我得到了:

But when I try to login, I have this error, after var_dump in login.ctp I get this:

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'行,则登录正常.

If I comment 'loginUrl' => '/users/login' line, then login works fine.

其他说明: -我测试了哈希密码和textplane密码,结果相同. -我在beforeFilter中添加了$this->Authentication->allowUnauthenticated(['view', 'index', 'login', 'add']);来访问登录名. -这是一个干净的cakephp 3.8安装,仅数据库对于测试是相同的. -我仅在蛋糕控制台中添加了Crud.

Additional notes: - I've tested with hashed and textplane passwords, same results. - I've added $this->Authentication->allowUnauthenticated(['view', 'index', 'login', 'add']); in beforeFilter to access login. - It's a clean cakephp 3.8 install, only database is the same for tests. - I've added Crud only with cake console.

我想了解更多有关loginURL的信息,我应该在UsersController中包括一些用法吗?是什么导致此错误?

I would like to learn more about that loginURL, I should include some uses in UsersController? What causes this error?

谢谢

推荐答案

错误消息目前有点误导,因为它没有向您显示可能的基本目录,这是您遇到的实际问题.我已经提出了 对此问题的修复 进入下一个版本.

The error messages is currently a little misleading, as it doesn't show you the possible base directory, which is the actual issue that you are experiencing. I've proposed a fix for that, which may make into the next release.

当您的应用程序位于子目录中时,您需要确保将登录URL配置考虑在内,即通过传递包含基本目录的URL,您可以手动执行以下操作:

When your application lives in a subdirectory, you need to make sure that your login URL configuration takes that into account, that is by either passing the URL including the base directory, which you could do either manually:

'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,而不必使用路由器:

Another option would be to use the routes based URL checker, which can be configured via the form authenticators urlChecker option, then you can define the login URL using URL arrays, without having to use the router:

'urlChecker' => 'Authentication.CakeRouter',
'loginUrl' => [
    'plugin' => null,
    'prefix' => null,
    'controller' => 'Users',
    'action' => 'login'
]

另请参阅:

这篇关于Cakephp 3身份验证插件,登录URL不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆