Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Cakephp 3.x进行LDAP身份验证_Php_Authentication_Ldap_Cakephp 3.0 - Fatal编程技术网

使用Cakephp 3.x进行LDAP身份验证

使用Cakephp 3.x进行LDAP身份验证,php,authentication,ldap,cakephp-3.0,Php,Authentication,Ldap,Cakephp 3.0,我是Cakephp新手,现在正在我的应用程序中实现LDAP身份验证。官方网站上的“书签”教程中有很多东西是自动工作的,所以它没有给我足够的关于如何实现某些特定身份验证的信息。我已经检查了这篇文章,并尝试以这种方式实现我的身份验证,但仍然存在一些理解问题 在我的数据库中有一个表“Students”,它的主键是属性“id”。 我的AppController如下所示: public function initialize() { parent::initialize(); $thi

我是Cakephp新手,现在正在我的应用程序中实现LDAP身份验证。官方网站上的“书签”教程中有很多东西是自动工作的,所以它没有给我足够的关于如何实现某些特定身份验证的信息。我已经检查了这篇文章,并尝试以这种方式实现我的身份验证,但仍然存在一些理解问题

在我的数据库中有一个表“Students”,它的主键是属性“id”。 我的AppController如下所示:

 public function initialize()
{
    parent::initialize();

    $this->loadComponent('Flash');
    $this->loadComponent('Auth', ['authenticate' =>
        ['Form' =>
            ['fields' =>
                ['username' => 'email',
                    'password' => 'password']
            ]
        ], 'loginAction' => [
        'controller' => 'Students',
        'action' => 'login'
    ]
    ]);

    $this->Auth->config('authenticate', ['Ldap']);
}

public function isAuthorized()
{
    if($this->Auth->user('departmentnumber') == "***") { return true; }
}
LdapAuthenticate类类似于我在上面提到的帖子:

namespace App\Auth;

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

class LdapAuthenticate extends BaseAuthenticate {

protected $_host = '***' ;

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 = "cn=Users,dc=***";
    $dn = "cn=$username, " . $basedn;
    $ldapbind = @ldap_bind($ds, $dn, $password);
    if (!$ldapbind) {
        return false ;
    }

    $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) ;
    return $user ;
}
}
当我打开任何页面时,我都会成功登录到login.ctp页面。在输入凭据并单击“登录”后,出现错误“SQLSTATE[42S02]:找不到基表或视图:1146表“***\u db.users”不存在”。 因此,我认为我犯了一些错误,但没有足够的理解来找到哪里-不知道为什么它试图在我的数据库中找到一个不存在的“users”表


感谢所有提前帮助我提出想法的人

因为配置不正确。不要在身份验证配置中使用
表单
,而是使用
Ldap

 $this->loadComponent('Auth', ['authenticate' =>
        ['Ldap' =>
            ['fields' =>
                ['username' => 'email',
                    'password' => 'password']
            ]
        ], 'loginAction' => [
        'controller' => 'Students',
        'action' => 'login'
    ]
    ]);
 $this->loadComponent('Auth', ['authenticate' =>
        ['Ldap' =>
            ['fields' =>
                ['username' => 'email',
                    'password' => 'password']
            ]
        ], 'loginAction' => [
        'controller' => 'Students',
        'action' => 'login'
    ]
    ]);