我在本地机器上运行四个 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());
}
}