当前位置: 首页 > news >正文

全国广告设计合肥seo排名公司

全国广告设计,合肥seo排名公司,政府网站建设基本要求,ppt超链接到网站怎么做在前面的两篇文章中,我详细的介绍了使用ldap与window AD服务集成,实现ToB项目中的身份认证集成方案,包括技术方案介绍、环境配置: ToB项目身份认证AD集成(一):基于目录的用户管理、LDAP和Active…

在前面的两篇文章中,我详细的介绍了使用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.createClient({url: conf.url,tlsOptions: {minVersion: "TLSv1.2",rejectUnauthorized: false,},});await promisify(client.bind).call(client, conf.adminDN, conf.adminPassword);return client; // 返回绑定后的客户端}private mergeSearchEntryObjectAttrs(entry: ldap.SearchEntryObject) {}private doSearch(client: ldap.Client, opts: ldap.SearchOptions) {}private encodePassword(password) {}private safeDn(dn: string) {}
}

二、中文字段的特殊patch

ldap.js对于数据的字段进行了escape操作,会导致中文输入被转化成\xxx的形式,无论是接收的数据还是发送的请求,这时候会导致cn包含中文会出现错。需要用如下方法进行patch,通过在出现问题的rdn上配置unescaped参数控制是否对字符串进行escape(如果不知道啥是escape,参见十六进制转义escape介绍

const oldString = ldap.RDN.prototype.toString;
ldap.RDN.prototype.toString = function () {return oldString.call(this, { unescaped: this.unescaped });
};

加了这个补丁后,就可以控制rdn的转义情况了。

三、用户搜索功能

findUsers() 方法用于在 AD 中搜索用户,返回用户的基本信息。

async findUsers(filter = this.config.userSearchFilter,attributes: string[] = ["sAMAccountName", "userPrincipalName", "memberOf"]
): Promise<LdapUserSimInfo[]> {await this.bindAsAdmin();const opts = {filter, scope: "sub", attributes: Array.from(new Set(["distinguishedName", "cn"].concat(attributes))),};const searchResult = await this.doSearch(await this.client, opts);return searchResult.map((user) => {return this.mergeSearchEntryObjectAttrs(user) as LdapUserSimInfo;});
}
  • filter 是用于搜索的 LDAP 过滤器,默认为查找所有用户的 (objectClass=user) 过滤器。
  • attributes 参数允许指定返回哪些用户属性,默认返回 sAMAccountNameuserPrincipalNamememberOf 等属性。
  • 该方法调用了 doSearch() 进行搜索,并通过 mergeSearchEntryObjectAttrs() 整理和转换 AD 返回的用户数据。

doSearch() 方法是实际进行 LDAP 搜索的地方:

private doSearch(client: ldap.Client, opts: ldap.SearchOptions) {return new Promise<ldap.SearchEntryObject[]>((resolve, reject) => {const entries = [] as ldap.SearchEntryObject[];client.search(this.config.userSearchBase, opts, (err, res) => {if (err) {return reject(err);}res.on("searchEntry", (entry) => {entries.push(entry.pojo);});res.on("end", (result) => {if (result?.status !== 0) {return reject(new Error(`Non-zero status from LDAP search: ${result?.status}`));}resolve(entries);});res.on("error", (err) => {reject(err);});});});
}
  • client.search()ldapjs 提供的一个方法,用于执行搜索操作。搜索结果通过事件 searchEntry 逐条返回,最终在 end 事件时完成。

四、用户认证功能

checkPassword() 方法用于用户身份验证,检查用户输入的密码是否正确。

async checkPassword(user: LdapUserSimInfo, password: string) {const userDN = user.objectName;const client = await this.client;await promisify(client.bind).call(client, userDN, password);
}
  • 通过 LDAP 的 bind() 方法,可以尝试使用用户的 DN 和密码进行绑定。如果绑定成功,表示密码正确;否则,会抛出错误,表示认证失败。

五、密码修改功能

changePassword() 方法允许用户修改自己的密码。

async changePassword(user: LdapUserSimInfo, newPassword: string, oldPassword: string) {await this.bindAsAdmin();const userDN = this.safeDn(user.objectName);const changes = [new ldap.Change({operation: "delete",modification: new ldap.Attribute({type: "unicodePwd",values: [this.encodePassword(oldPassword)],}),}),new ldap.Change({operation: "add",modification: new ldap.Attribute({type: "unicodePwd",values: [this.encodePassword(newPassword)],}),}),];const client = await this.client;await promisify(client.modify).call(client, userDN, changes);
}
  • 在修改密码时,LDAP 需要先删除旧密码,再添加新密码。这里使用 ldap.Change 创建修改操作,通过 client.modify() 方法应用到 AD。

六、密码重置功能

resetPassword() 方法允许管理员重置用户的密码:

async resetPassword(user: LdapUserSimInfo, resetPassword: string) {await this.bindAsAdmin();const client = await this.client;const userDN = this.safeDn(user.objectName);const changes = new ldap.Change({operation: "replace",modification: new ldap.Attribute({type: "unicodePwd",values: [this.encodePassword(resetPassword)],}),});await promisify(client.modify).call(client, userDN, changes);
}
  • 与修改密码不同,重置密码直接使用 replace 操作,替换用户的现有密码。

七、结语

通过对 LdapService 类的逐步解析,相信你已经学会了如何利用 ldapjs 库与 Windows AD 进行交互。在实际使用中,还可以根据业务需求对这个类进行扩展,从而满足大规模企业系统中的用户管理需求。

另外这个中文的问题,暂时还只能是如此打补丁,期待社区修复可能不会那么及时

http://www.hrbkazy.com/news/13713.html

相关文章:

  • 如何在一个空间做2个网站外贸营销网站
  • 公司团队建设百度seo新站优化
  • 网站建设推广新业务网络营销成功案例介绍
  • 网站企业制作网站优化流程
  • 重庆手机网站建设互联网推广怎么找渠道
  • 大学网站建设评比考核办法郑州seo优化服务
  • b2c模式的电子商务网站东莞网络推广平台
  • 广州建设网站的公司简介郑州竞价托管公司哪家好
  • 利用淘宝做网站卖货到国外谷歌商店下载
  • 国外最炫酷网站如何设计与制作网页
  • 电脑怎么做网站网络推广方案范文
  • 安徽专业做网站的公司网页设计模板免费网站
  • 全国知名网站建设网站后台管理系统
  • 自己的网站怎么做搜索引擎网上电商怎么做
  • 淄博网站建设优化运营熊掌号百度知道在线
  • 海尔网站建设水平新手电商运营从哪开始学
  • 企业网站管理系统如何使用说明如何制作一个自己的网页
  • 网站模板 整站源码下载浙江搜索引擎优化
  • 上海建设人才网官网领硕网站seo优化
  • 成都网站建设哪里好郑州网站优化seo
  • 英文网站制作注意点真正免费建站
  • 网站建设应急处置方案微信推广广告在哪里做
  • 重庆网站设计新媒体营销成功案例
  • 湖南城乡建设厅官方网站精准营销
  • 伪静态网站配置常用的网络营销方法
  • 在线股票交易网站开发网站推广软件免费版大全
  • 合肥知名建站公司网络营销模式有哪些
  • 那个网站有用director做的片头百度关键词排名怎么做
  • 营业执照挂靠地址费用广东seo网站推广
  • 怎么在小程序里开店铺厦门seo俱乐部