node 项目连接 ldap

ldap 简单介绍

ldap 是一种树形数据库,一般用于存储公司员工相关信息.
WX20190930-112724@2x

使用 ldap 模块查询

用到 ldap 模块,使用 npm 安装:
npm i ldapjs -S

使用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
const ldap = require("ldapjs");
const ldapConfig = { url: "ldap://10.20.21.24" };

const opts = {
// filter: "", //查询条件过滤器,查找uid=kxh的用户节点
// scope: "", //查询范围
timeLimit: 5 //查询超时
};

module.exports = {
ldapAuth: userName => {
return new Promise((resolve, reject) => {
const client = ldap.createClient(ldapConfig.config);
//连接 ldap 的账户名&密码
client.bind("账户名", "密码", (err1, res1) => {
client.search(`cn=${userName},ou=employee,dc=zrtg,dc=com`, opts, (err, res2) => {
res2.on("searchEntry", entry => {
user = entry.object;
resolve(user);
});
res2.on("searchReference", referral => {
console.log("referral: " + referral.uris.join());
});
res2.on("error", err => {
client.unbind();
reject(err);
});
res2.on("end", result => {
client.unbind();
});
});
});
});
}
};
thank u !