0

我在本地机器上运行四个 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: {▶}

有人可能有提示吗?

谢谢

斯蒂芬


更新

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

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());
    }
}
4

1 回答 1

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
于 2019-08-18T12:21:17.160 回答