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

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

问题描述

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

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

原来$fields是用来改变真实数据库中的用户名和密码关系的,和Auth组件一样,登录URL是登录表单si加载的地方.

首先,在入门或文档的任何部分都没有说明登录视图(表单),因此,像旧的 Auth 组件一样,我将其创建为 Users/login.ctp

<?= $this->Flash->render('auth') ?><?= $this->Form->create() ?><字段集><legend><?= __('请输入您的电子邮件和密码') ?></legend><?= $this->Form->input('email') ?><?= $this->Form->input('password') ?></fieldset><?= $this->Form->button(__('Login'));?><?= $this->Form->end() ?>

我在 Application.php 中的代码包括这个(及其各自的用途和实现):

 public function getAuthenticationService(ServerRequestInterface $request, ResponseInterface $response){$service = new AuthenticationService();$字段 = ['用户名' =>'电子邮件','密码' =>'密码'];//加载标识符$service->loadIdentifier('Authentication.Password', compact('fields'));//加载验证器,首先需要会话$service->loadAuthenticator('Authentication.Session');$service->loadAuthenticator('Authentication.Form', ['字段' =>$字段,'登录网址' =>'/用户/登录']);返回 $service;}

但是当我尝试登录时,出现此错误,在 login.ctp 中的 var_dump 之后我得到了这个:

object(AuthenticationAuthenticatorResult)[124]受保护的 '_status' =>字符串FAILURE_OTHER"(长度=13)受保护的 '_data' =>空值受保护的 '_errors' =>数组(大小=1)0 =>字符串登录 URL `http://localhost/users/login` 与 `/users/login` 不匹配."(长度=70)

如果我评论 'loginUrl' =>'/users/login' 行,然后登录工作正常.

补充说明:- 我已经用散列和文本平面密码进行了测试,结果相同.- 我在 beforeFilter 中添加了 $this->Authentication->allowUnauthenticated(['view', 'index', 'login', 'add']); 以访问登录.- 这是一个干净的 cakephp 3.8 安装,只有数据库是相同的测试.- 我只在蛋糕控制台中添加了 Crud.

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

谢谢

解决方案

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

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

'loginUrl' =>'/我的应用程序/用户/登录'

或使用路由器:

'loginUrl' =>CakeRoutingRouter::url('/users/login')

'loginUrl' =>CakeRoutingRouter::url(['插件' =>空值,'前缀' =>空值,'控制器' =>'用户','动作' =>'登录'])

另一种选择是使用基于路由的 URL 检查器,可以通过表单验证器 urlChecker 选项进行配置,然后您可以使用 URL 数组定义登录 URL,而不必使用路由器:

'urlChecker' =>'Authentication.CakeRouter','登录网址' =>['插件' =>空值,'前缀' =>空值,'控制器' =>'用户','动作' =>'登录']

另见:

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

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

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.

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>

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;
    }

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

object(AuthenticationAuthenticatorResult)[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)

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

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.

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

Thank you

解决方案

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.

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'

or by using the router:

'loginUrl' => CakeRoutingRouter::url('/users/login')

'loginUrl' => CakeRoutingRouter::url([
    'plugin' => null,
    'prefix' => null,
    'controller' => 'Users',
    'action' => 'login'
])

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'
]

See also:

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

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