authentication - Cakephp 3 身份验证插件,登录 URL 不匹配

标签 authentication cakephp plugins cakephp-3.x cakephp-3.8

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

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

最初 $fields 被指定用于更改真实数据库中的用户名和密码关系,与 Auth 组件相同,登录 URL 是加载登录表单的地方。

首先,在入门或文档的任何部分都没有提到登录 View (表单),所以,就像旧的 Auth 组件一样,我将它创建到 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' 行,那么登录工作正常。

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

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

谢谢

最佳答案

错误消息目前有点误导,因为它没有向您显示可能的基本目录,而这正是您遇到的实际问题。我提议了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'
]

另见:

关于authentication - Cakephp 3 身份验证插件,登录 URL 不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59065494/

相关文章:

django - Django + nginx + uwsgi 无法登录

java - 在 Java Web 应用程序中获取 Windows 登录详细信息

php - 加入查询仅从一个表 cakephp 3 检索数据

cakephp - 使用多个 $this->loadModel ('Model' ) 还是使用数组一个?

php - 图片未替换为 css

asp.net-mvc - 您如何测试 ASP MVC 应用程序的基于用户的安全性?

Java登录MySQL数据库

javascript - 转移站点后未加载 CSS 和 JS

java - 云服务的 SecurityManager "sandbox"

java - 有没有办法让 jadclipse 与 Eclipse 3.4 一起工作?