繁体   English   中英

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

[英]LDAP Authentication with Cakephp 3.x

我是Cakephp的新手,现在正在我的应用程序中从事LDAP身份验证的实现。 官方网站上“书签”教程中的许多内容会自动运行,因此它没有为我提供有关如何实施某些特定身份验证的足够信息。 我已经检查过这篇CakePHP 3 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 ;
}
}

在StudentsController中,我实现了登录和注销功能,就像“书签管理器”教程中那样:

public function login(){
    if ($this->request->is('post')){
        $user = $this->Auth->identify();
        if ($user) {
            $this->Auth->setUser($user);
            return $this->redirect($this->Auth->redirectUrl());
        }

        // user is not identified
        $this->Flash->error('Your username or password is not correct');
    }
}

public function logout(){
    $this->Flash->success('You are now logged out');
    return $this->redirect($this->Auth->logout());
}

打开任何页面时,我都成功登录了我的login.ctp页面。 输入凭据并单击“登录”后,出现错误“ SQLSTATE [42S02]:找不到基表或视图:1146表'*** _ db.users'不存在”。 因此,我认为我做错了事,但对找到位置的了解不足-不知道为什么它会尝试在我的数据库中查找不存在的“用户”表。

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

由于配置错误。 使用Ldap代替在Auth配置中使用Form

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM