【问题标题】:CakePHP 2.x custom "Authentication adapter "LdapAuthorize" was not found未找到 CakePHP 2.x 自定义“身份验证适配器”LdapAuthorize
【发布时间】:2026-02-15 23:50:02
【问题描述】:

我正在使用 CakePHP 构建一个应用程序并尝试合并一个自定义身份验证对象,但它似乎无法找到它。尝试登录时出现以下错误:“未找到身份验证适配器“LdapAuthorize””。我已经使用我的代码创建了文件 app/Controller/Component/Auth/LdapAuthorize.php 以进行身份​​验证。在“AppController.php”的顶部附近我有

App::uses('LdapAuthroize', 'Controller/Component/Auth/LdapAuthorize');

在我的 AppController 类中

public $components = array(
        'Session',
        'Auth' => array(
            'loginRedirect'  => array('controller' => 'pendings', 'action' => 'index'),
            'logoutRedirect' => array('controller' => 'users', 'action' => 'login'),
            'authorize'      => array('Controller'),
            'authenticate'   => array('LdapAuthorize')
        )
    );

然后在我的 UsersController.php 中我有以下登录功能。

        public function login() {       

        if($this->request->is('post')) {
            if($this->Auth->login()) { 
                                // My Login stuff...
                            }
                    else
                        $this->redirect(array('controller'=>'someController', 'action'=>'someAction'));         
        }
    }

如果有人知道为什么它似乎无法加载我的自定义身份验证对象,那真是太棒了。谢谢!

【问题讨论】:

  • 你找到解决办法了吗?
  • @ElliotVargas 您是否有解决方案,因为上述方法不起作用并且我遇到了同样的问题?
  • @DeAn 我刚刚加了一个answer,看看对你有没有用。
  • @ElliotVargas 谢谢,我几周前就想通了,这符合你的建议。

标签: cakephp authentication


【解决方案1】:

我将自定义身份验证类放在 Controller/Component/Auth 中。比如我的类的名字是CustomUserAuthenticate,文件的路径是,

Controller/Component/Auth/CustomUserAuthenticate.php.

然后在我的 AppController 中,我将以下内容添加到 authenticate 数组中,

class AppController extends Controller {      
    public $components = array(
        'Auth' => array(
            /** Any other configuration like redirects can go here */
            'authenticate' => array(
                'CustomUser'
            )
        )
    );
}

authenticate 数组中的字符串必须与类的名称匹配,Authenticate 字除外。

我的 CustomUserAuthenticate 类扩展了 CakePHP 的 Controller/Component/Auth/BaseAuthenticate 并覆盖了 authenticate 方法。 CakePHP 的文档指出这不是required。我没试过。

【讨论】:

    【解决方案2】:

    我认为您的App::uses() 是错误的,因此它找不到课程。您当前的代码:

    App::uses('LdapAuthroize', 'Controller/Component/Auth/LdapAuthorize');
    

    正在尝试查找 Controller/Component/Auth/LdapAuthorize/LdapAuthroize.php

    第一个参数是类名(你有一个错字),第二个是包含类的目录的路径,你不需要再次添加类名。

    试试这个:

    App::uses('LdapAuthorize', 'Controller/Component/Auth');
    

    【讨论】:

    • 感谢您的回复!我不敢相信我错过了那个错字,但这是第 500 次写出该行。不幸的是,我似乎仍然遇到同样的错误。还有其他建议吗?
    • @JonHaller 您是否也将第二个参数更改为Controller/Component/Auth