Adldap2-openLdap-无法联系LDAP服务器

时间:2019-08-16 20:39:05

标签: laravel openldap adldap

我正在本地计算机上运行四个docker容器:

ldap:
   container_name: openldap
   image: osixia/openldap:latest
  app:
    build:
    image: digitalocean.com/php
    container_name: app
  webserver:
    image: nginx:alpine
    container_name: webserver
  db:
    image: mysql:5.7.22
    container_name: db
  phpmyadmin:
    image: phpmyadmin/phpmyadmin

我现在想做的是从laravel控制器向openLdap发出ldap请求。程序LDAPAdmin允许我使用以下设置连接到openLdap容器:

    Host: localhost:389
    Base: dc=myworld,dc=com
    Username: cn=admin,dc=myworld,dc=com
    Password: PassWord
    Version: 3
    Simple Authentication

我的控制器如下:

use Adldap\Adldap;

        $LDAPconfig = [
            'default' => [
                'hosts'            => ['openldap', 'localhost'],
                'base_dn'          => env('ADLDAP_BASEDN', "dc=myworld,dc=com"),
                'username'         => env('ADLDAP_ADMIN_USERNAME', "admin"),
                'password'         => env('ADLDAP_ADMIN_PASSWORD', "PassWord"),

                // Optional Configuration Options
                'schema' => \Adldap\Schemas\OpenLDAP::class,
                'account_prefix' => '',
                'account_suffix' => '',
                'port'             => 389,
                'follow_referrals' => false,
                'use_ssl'          => false,
                'use_tls'          => false,
                'version'          => 3,
                'timeout'          => 5,
            ]
        ];

        $ad = new Adldap();

        $config = new \Adldap\Configuration\DomainConfiguration($LDAPconfig['default']);

        $provider = new Adldap();
        $provider->addProvider($config);

        try {
            $provider->connect();
        } catch (\Exception $e) {
            dd($e);
        }

如果我只返回$ provider,它看起来很有希望:

Adldap {#268 ▼
  #default: "default"
  #providers: array:1 [▼
    "default" => Provider {#276 ▼
      #connection: Ldap {#277 ▼
        #name: "default"
        #host: "ldap://openldap:389 ldap://localhost:389"
        #connection: ldap link resource @338
        #bound: false
        #useSSL: false
        #useTLS: false
      }
      #configuration: DomainConfiguration {#267 ▼
        #options: array:14 [▼
          "hosts" => array:2 [▶]
          "timeout" => 5
          "version" => 3
          "port" => 389
          "schema" => "Adldap\Schemas\OpenLDAP"
          "base_dn" => "dc=myworld,dc=com"
          "username" => "admin"
          "password" => "PassWord"
          "account_prefix" => ""
          "account_suffix" => ""
          "use_ssl" => false
          "use_tls" => false
          "follow_referrals" => false
          "custom_options" => []
        ]
      }
      #schema: OpenLDAP {#278}
      #guard: null
    }
  ]
  #listen: array:3 [▼
    0 => "Adldap\Auth\Events\*"
    1 => "Adldap\Query\Events\*"
    2 => "Adldap\Models\Events\*"
  ]
}

但是当我尝试访问我的网站时,出现异常“无法联系LDAP服务器”,并且我不确定它是否正在尝试连接到容器,或者为什么请求被拒绝。

BindException {#281 ▼
  #detailedError: DetailedError {#282 ▶}
  #message: "Can't contact LDAP server"
  #code: -1
  #file: "/var/www/vendor/adldap2/adldap2/src/Auth/Guard.php"
  #line: 109
  -previous: Exception {#280 ▶}
  trace: {▶}

有人暗示吗?

谢谢

斯蒂芬


更新

从Guard.php中的函数bind返回的第109行

public function bind($username = null, $password = null){
    $this->fireBindingEvent($username, $password);

    try {
        if (@$this->connection->bind($username, $password) === true) {
            $this->fireBoundEvent($username, $password);
        } else {
            throw new Exception($this->connection->getLastError(), $this->connection->errNo());
        }
    } catch (Throwable $e) {
        $this->fireFailedEvent($username, $password);

        throw (new BindException($e->getMessage(), $e->getCode(), $e))
            ->setDetailedError($this->connection->getDetailedError());
    }
}

1 个答案:

答案 0 :(得分:0)

好的,我几个小时后解决了错误。

问题是我不知道要同时做两件事。第一件事是laravel,第二件事是docker。事实证明,我的laravel代码运行正常。该问题与我的容器的网络基础架构有关。因为我使用的是两个不同的docker-compose.yml文件,所以即使我在两个文件中都将其称为网络接口,该容器也无法彼此通信:

networks:
  app-network:
    driver: bridge

但是,此刻我将所有内容合并到一个docker-compose.yml文件中,直到我弄清楚如何使它们在同一网络中运行。

谢谢

斯蒂芬


更新

我的撰写文件,版本3.5:

主文件:

#Docker Networks
networks:
    app-network:
        driver: bridge
        name: app-network

第二个文件:

#Docker Networks
networks:
  app-networkk:
    external:
        name: app-network