CakePHP认证插件登录URL不匹配求助(已尝试Router类修复)
问题:Login URL不匹配错误排查(Authentication Plugin 2.20.2)
遇到错误提示:Login URL /my_app/ did not match /my_app/users/login,已按照Stack Overflow相关方案(使用Router类)尝试修复,但问题仍存在。当前使用的Authentication Plugin版本为2.20.2。
Application.php中getAuthenticationService方法代码
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:网络追踪信息
/my_app(301)/my_app/(200)(额外的斜杠?)/my_app/(500)(预期内,$this->Authentication->getResult()->getStatus()为FAILURE_OTHER)
疑问:我是否应该在网络追踪中看到/my_app/users/login?请注意我使用环境标头作为认证机制。
内容的提问来源于stack exchange,提问作者TechFanDan
相关产品推荐
相关产品推荐

