【问题标题】:LDAP Authentication with Cakephp 3.x使用 Cakephp 3.x 的 LDAP 身份验证
【发布时间】:2015-11-15 16:16:26
【问题描述】:

我是 Cakephp 的新手,现在正在我的应用程序中实现 LDAP 身份验证。官方网站上的“书签”教程中的很多东西都是自动工作的,所以它没有给我足够的信息来告诉我如何实现一些特定的身份验证。我已经查看了这篇CakePHP 3 Ldap authentication issue and clarification 的帖子并尝试以这种方式实现我的身份验证,但仍然存在一些理解问题。

在我的数据库中,我有一个表“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'不存在”。 所以我认为我做错了,但没有足够的理解来找到哪里 - 不知道为什么它试图在我的数据库中找到一个不存在的“用户”表。

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

【问题讨论】:

    标签: php authentication ldap cakephp-3.0


    【解决方案1】:

    因为配置不正确。不要在 Auth 配置中使用 Form,而是使用 Ldap

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-24
      • 1970-01-01
      • 2015-07-12
      • 1970-01-01
      • 2015-12-04
      • 1970-01-01
      相关资源
      最近更新 更多