如何通过node.js授权windows个广告用户?
How to auth windows AD users by node.js?
我最近需要授权windows个AD用户。场景如下
- 网页在服务器 A 上运行(Vue + vue-router)
- Api 接口在服务器 B 上运行(node + express)
- 用户在网页上输入 AD 用户名和密码(服务器 A)
- 将用户名和密码传递到服务器 B 上的 api 接口以进行身份验证
- 服务器 B 通过 LDAP(windwos AD) 验证用户名和密码
- api 在服务器 B returns 对网页的反馈(服务器 A)
那么,是否可以在服务器 B 上实施任何解决方案以通过 LDAP 验证用户名和密码?
非常感谢!
我用过nodesspi https://github.com/abbr/nodesspi。
但它仅适用于 windows 环境。而且似乎你只能通过浏览器直接访问服务器B才能获得结果。未传递参数以在服务器 B.
上调用 api
反正对我来说是个学习的好场景
我找到了解决方案。参考:
Node JS LDAP Auth User
var ldap = require('ldapjs');
var client = ldap.createClient({
url: 'ldap://ldapserver:port/',
timeout: 5000,
connectTimeout: 10000
});
var opts = {
filter: '(&(cn=*))',
scope: 'sub',
// This attribute list is what broke your solution
attributes:['SamAccountName','dn']
};
console.log('--- going to try to connect user ---');
try {
client.bind(username, password, function (error) { //first need to bind
if(error){
console.log(error.message);
client.unbind(function(error) {if(error){console.log (error.message);} else{console.log('client disconnected');}});
} else {
console.log('connected');
client.search('ou=users, ou=compton, dc=batman, dc=com', opts, function(error, search) {
console.log('Searching.....');
search.on('searchEntry', function(entry) {
if(entry.object){
console.log('entry: %j ' + JSON.stringify(entry.object));
}
client.unbind(function(error) {if(error){console.log(error.message);} else{console.log('client disconnected');}});
});
search.on('error', function(error) {
console.error('error: ' + error.message);
client.unbind(function(error) {if(error){console.log(error.message);} else{console.log('client disconnected');}});
});
}
});
} catch(error){
console.log(error);
client.unbind(function(error) {if(error){console.log(error.message);} else{console.log('client disconnected');}});
}
请记住,如果出现 'error~~~: Size Limit Exceeded' 错误,请使用 paged 和 sizeLimit 参数。
var opts = {
filter: '(objectclass=commonobject)',
scope: 'sub',
paged: true,
sizeLimit: 200
};
我最近需要授权windows个AD用户。场景如下
- 网页在服务器 A 上运行(Vue + vue-router)
- Api 接口在服务器 B 上运行(node + express)
- 用户在网页上输入 AD 用户名和密码(服务器 A)
- 将用户名和密码传递到服务器 B 上的 api 接口以进行身份验证
- 服务器 B 通过 LDAP(windwos AD) 验证用户名和密码
- api 在服务器 B returns 对网页的反馈(服务器 A)
那么,是否可以在服务器 B 上实施任何解决方案以通过 LDAP 验证用户名和密码?
非常感谢!
我用过nodesspi https://github.com/abbr/nodesspi。
但它仅适用于 windows 环境。而且似乎你只能通过浏览器直接访问服务器B才能获得结果。未传递参数以在服务器 B.
上调用 api反正对我来说是个学习的好场景
我找到了解决方案。参考: Node JS LDAP Auth User
var ldap = require('ldapjs');
var client = ldap.createClient({
url: 'ldap://ldapserver:port/',
timeout: 5000,
connectTimeout: 10000
});
var opts = {
filter: '(&(cn=*))',
scope: 'sub',
// This attribute list is what broke your solution
attributes:['SamAccountName','dn']
};
console.log('--- going to try to connect user ---');
try {
client.bind(username, password, function (error) { //first need to bind
if(error){
console.log(error.message);
client.unbind(function(error) {if(error){console.log (error.message);} else{console.log('client disconnected');}});
} else {
console.log('connected');
client.search('ou=users, ou=compton, dc=batman, dc=com', opts, function(error, search) {
console.log('Searching.....');
search.on('searchEntry', function(entry) {
if(entry.object){
console.log('entry: %j ' + JSON.stringify(entry.object));
}
client.unbind(function(error) {if(error){console.log(error.message);} else{console.log('client disconnected');}});
});
search.on('error', function(error) {
console.error('error: ' + error.message);
client.unbind(function(error) {if(error){console.log(error.message);} else{console.log('client disconnected');}});
});
}
});
} catch(error){
console.log(error);
client.unbind(function(error) {if(error){console.log(error.message);} else{console.log('client disconnected');}});
}
请记住,如果出现 'error~~~: Size Limit Exceeded' 错误,请使用 paged 和 sizeLimit 参数。
var opts = {
filter: '(objectclass=commonobject)',
scope: 'sub',
paged: true,
sizeLimit: 200
};