【问题标题】:CakePHP 3 Ldap authentication issue and clarificationCakePHP 3 Ldap 认证问题及说明
【发布时间】:2015-09-15 23:57:57
【问题描述】:

我正在将 LDAP 身份验证集成到我的项目中。我按照 CakePHP 官方网站上的教程指导如何在应用程序 src 路径中创建自定义对象并在 AuthController 中使用这些自定义对象。

所以我在 src 中创建了一个名为 Auth 的文件夹,文件名为 LdapAuthorize.php。路径看起来像这样 src/Auth/LdapAuthorize.php

这是我的 LdapAuthorize.php 代码:

namespace App\Auth;

use Cake\Auth\BaseAuthorize;
use Cake\Network\Request;

class LdapAuthorize extends BaseAuthorize {
    public function authorize($user, Request $request) {
        if ($user == 'username') { // where username is logged on ldap user on a computer.
            return true;
        }
    }
}

我还调用了 AppController.php 文件中的对象。这是我的代码:

public function initialize()
{
    parent::initialize();
    $this->loadComponent('Flash');
    $this->loadComponent('Auth', [
        'loginRedirect' => [
            'controller' => 'Customers',
            'action' => 'index'
        ],
        'logoutRedirect' => [
            'controller' => 'Pages',
            'action' => 'display',
            'home'
        ]
    ]);      
    $this->Auth->config('authenticate', [
        'Ldap'
    ]);
}

所以当我访问网址http://localhost/AppPath/Dashboard/index 时,我得到Authentication adapter "Ldap" was not found.

由于这是我第一次使用 CakePHP,我无法在网上找到那么多有助于解决任何问题的解决方案。

为 LdapAuthenticate.php 添加附加代码:

namespace App\Auth;

use Cake\Auth\BaseAuthenticate;
use Cake\Network\Request;
use Cake\Network\Response;

class OpenidAuthenticate extends BaseAuthenticate
{
    public function authenticate(Request $request, Response $response)
    {
        $users = ["john", "ray"];
        return $users;
    }
}

【问题讨论】:

    标签: php cakephp authentication cakephp-3.0


    【解决方案1】:

    你需要的是custom authentication adapter,你的LdapAuthorize是custom authorize adapter

    // in src/Auth/LdapAuthenticate.php
    
    namespace App\Auth;
    
    use Cake\Auth\BaseAuthenticate;
    use Cake\Network\Request;
    use Cake\Network\Response;
    
    class LdapAuthenticate extends BaseAuthenticate {
    
        protected $_host = 'your_ldap_server' ;
    
        public function authenticate(Request $request, Response $response) {
            $username = $request->data['username'] ;
            $password = $request->data['password'] ;
            $ds = @ldap_connect($this->_host) ;
            if (!$ds) {
                throw \Cake\Error\FatalErrorException ('Unable to connect to LDAP host.') ;
            }
            $basedn = "your ldap query... "
            $dn = "uid=$username, ".$basedn;
            $ldapbind = @ldap_bind($ds, $dn, $password);
            if (!$ldapbind) {
                return false ;
            }
            // Do whatever you want with your LDAP connection... 
            $entry = ldap_first_entry ($ldapbind) ;
            $attrs = ldap_get_attributes ($ldapbind, $entry) ;
            $user  = [] ;
            // Loop
            for ($i = 0 ; $i < $attrs["count"] ; $i++) {
                $user[$attrs[$i]] = ldap_values ($ldapbind, $entry, $attrs[$i])[0] ;
            }
            // Then close it and return the authenticated user
            ldap_unbind ($ldapbind) ;
            ldap_close ($ldapbind);
            return $user ;
        }
    
    }
    

    【讨论】:

    • 我明白了。我还在我的项目中添加了自定义身份验证适配器,因为我创建了一个包含要返回的用户列表的数组。我仍然得到Authentication adapter "Ldap" was not found. 所以我也尝试了$this-&gt;Auth-&gt;config('authenticate', ['App\Auth\Ldap' ]); 不知道我还缺少什么。
    • @Ray 您是否使用我的答案中的代码创建了文件src/Auth/LdapAuthenticate.php?我不知道App\Auth\Ldap 是否适用于authenticate 选项,您应该尝试使用简单的Ldap。我不明白“我创建了一个包含要返回的用户列表的数组”是什么意思?如果您的项目中有其他内容,请将其添加到您的问题中并附上详细信息。
    • 是的,我确实使用您上面的代码在src/Auth/LdapAuthenticate.php 中创建了文件。我将其包括在我之前发布的问题中。在 LdapAuthenticate 中,我创建了一个数组,其中包含允许登录的用户,因此在我开始编写 LDAP 相关的 PHP 代码以连接到 Activie 目录之前,我可以让初始组件正常工作。我还需要对路由进行任何更改吗?
    • @Ray 您将您的班级称为OpenidAuthenticate 而不是LdapAuthenticate,如果它不是复制/粘贴错误,这可能是导致您的错误的原因。另请注意,authenticate 应该只返回一个用户,而不是用户列表(是的,文档在这一点上有点误导......)。
    • 非常感谢您阐明 LdapAuthenticate 适配器路径。官方文档让我有点困惑,而且我很难理解如何在每个页面中附加 LDAP 身份验证。我可以打开新问题并将其设为已解决。
    【解决方案2】:

    在创建上面建议的自定义身份验证适配器后,我仍然遇到同样的错误。

    我解决了它的变化
    namespace App\Auth;

    namespace Cake\Auth;
    

    在 LdapAuthenticate.php 中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-18
      • 2017-06-26
      • 2016-08-17
      • 2014-07-16
      • 1970-01-01
      • 1970-01-01
      • 2011-02-02
      相关资源
      最近更新 更多