登录 URL /my_app/ 与 /my_app/users/login 不匹配

问题描述 投票:0回答:1

我得到了

Login URL /my_app/ did not match /my_app/users/login
,尽管我已经实现了此处找到的修复(即使用 Router 类):

我正在使用身份验证插件版本

2.20.2

Application.php

public function getAuthenticationService(ServerRequestInterface $request): AuthenticationServiceInterface
{
    $login_url = \Cake\Routing\Router::url('/users/login');
    //dd($login_url) outputs /my_app/users/login

    $service = new AuthenticationService([
        'unauthenticatedRedirect' => $login_url,
        'queryParam' => 'redirect',
    ]);

    //
    $service->loadAuthenticator('Authentication.Session');
    $service->loadAuthenticator('Authentication.Environment', [
        'loginUrl' => $login_url,
        'fields' => [
            // Choose which environment variables exposed by your
            // authentication provider are used to authenticate
            // in your application.
            'HTTP_SAML_EMAIL',
        ],
    ]);

    //
    $service->loadIdentifier('Authentication.Token', [
        'tokenField' => 'email',
        'dataField' => 'HTTP_SAML_EMAIL',
        'resolver' => [
            'className' => 'Authentication.Orm',
            'userModel' => 'Users',
            'finder' => 'authenticatedUser',    //check Users table for custom finder
        ],
    ]);
    return $service;
}

编辑#1

Users.login

public function login()
{
    /*
    FAILURE_CREDENTIALS_MISSING - no headers
    FAILURE_IDENTITY_NOT_FOUND - user not found, must be added
    SUCCESS - all good
    */
    
    //get auth status
    $status = $this->Authentication->getResult()->getStatus();

    if($status == 'SUCCESS')    //user found, must update
    {
        //...

        $target = $this->Authentication->getLoginRedirect() ?? \Cake\Routing\Router::url(['controller'=>'Users', 'action'=>'workspace']);
        //debug("a:". $target);
        $this->Flash->success(__('You have successfully logged in'));
        return $this->redirect($target);
    }
    elseif($status == 'FAILURE_IDENTITY_NOT_FOUND') //user not found, must add
    {
        //...

        $target = $this->Authentication->getLoginRedirect() ?? \Cake\Routing\Router::url('/users/workspace');
        //debug("b:". $target);
        $this->Flash->success(__('You have successfully logged in'));
        return $this->redirect($target);
    }
    elseif($status == 'FAILURE_CREDENTIALS_MISSING')
    {
        throw new \Cake\Core\Exception\CakeException(__('Authentication error, missing headers. Please contact system administrators.'), 403);
    }
    else
    {
        //THIS IS WHERE WE'RE AT NOW
        debug($this->Authentication->getResult());
        debug($this->request);

        throw new \Cake\Core\Exception\CakeException(__('Uncaught exception during authentication. Please contact system administrators.'), 500);
    }
}

编辑#2

网络踪迹:

  1. /my_app
    (301)
  2. /my_app/
    (200)(额外斜线?)
  3. /my_app/
    (500)(预期,
    $this->Authentication->getResult()->getStatus()
    为 FAILURE_OTHER)

我应该在那里看到

/my_app/users/login
吗?注意使用环境标头作为身份验证机制。

php authentication cakephp cakephp-4.x
1个回答
0
投票

问题是我的路线。

$routes->scope('/',

    $builder->connect('/', ['controller' => 'Users', 'action' => 'login'], ['name' => 'app']);

    $builder->fallbacks();
});

我需要确保基本路由不会反映相同的登录 URL(即

Users.login
)。

© www.soinside.com 2019 - 2024. All rights reserved.