在前面的两篇文章中,我详细的介绍了使用ldap与window AD服务集成,实现ToB项目中的身份认证集成方案,包括技术方案介绍、环境配置:
ToB项目身份认证AD集成(一):基于目录的用户管理、LDAP和Active Directory简述
ToB项目身份认证AD集成(二):一分钟搞定window server 2003部署AD域服务并支持ssl加密(多图保姆教程+证书脚本)
在本文中,我将详细介绍如何利用 ldapjs
库使之一个 Node.js 服务类 LdapService
,该类实现了与 之前搭建的Windows AD 交互,包括用户搜索、身份验证、密码修改等功能。
也算是AD集成系列的完结吧,后续可能出其它客户端的对接,但目前工作核心在AI那块儿,大概率也不会继续了
一、实现方案和LdapService类概述
LdapService
类的核心是通过 LDAP(轻量级目录访问协议)与 AD 进行交互,提供用户搜索、认证、密码修改、重置等功能。下图是该类的基本结构,后续将一步步的介绍如何实现各个方法。
class LdapService {
client: Promise<ldap.Client>;
private config: MustProperty<LdapServiceConfig>;
constructor(config: LdapServiceConfig) {
this.config = {
...defaultConfig,
...config,
};
this.client = this.init();
}
async findUsers(
filter = this.config.userSearchFilter,
attributes: string[] = ["sAMAccountName", "userPrincipalName", "memberOf"]
) {
}
// 关闭连接
async close() {
(await this.client).destroy();
}
async findUser() {
}
// 修改用户密码的方法
async changePassword(
user: LdapUserSimInfo,
newPassword: string,
oldPassword: string
) {
}
// 用户认证的方法 - 检查密码是否正确
async checkPassword(user: LdapUserSimInfo, password: string) {
}
/*重置密码 */
async resetPassword(user: LdapUserSimInfo, resetPassword: string) {
}
private async init() {
const conf = this.config;
const client = ldap.createClien