我正在尝试通过LDAP登录到我自己的下层(sharedlatex)社区服务器。与Active Directory的连接有效。但是在成功通过身份验证后,我得到以下错误:
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (_http_outgoing.js:470:11)
at ServerResponse.header (/var/www/sharelatex/web/node_modules/express/lib/response.js:718:10)
at ServerResponse.send (/var/www/sharelatex/web/node_modules/express/lib/response.js:163:12)
at ServerResponse.json (/var/www/sharelatex/web/node_modules/express/lib/response.js:249:15)
at /var/www/sharelatex/web/app/src/Features/Authentication/AuthenticationController.js:133:15
at tryCatcher (/var/www/sharelatex/web/node_modules/standard-as-callback/built/utils.js:11:23)
at promise.then (/var/www/sharelatex/web/node_modules/standard-as-callback/built/index.js:19:49)
at process._tickCallback (internal/process/next_tick.js:68:7)
即使凭据正确,这也会导致错误的用户名/密码消息。
对于登录过程,我使用以下代码:
ldapAuth(query, passwd, onSuccess, callback, adminMail, userObj) {
const client = ldap.createClient({
url: process.env.LDAP_SERVER
});
const bindDn = process.env.LDAP_BIND_DN
const bindPassword = process.env.LDAP_BIND_PW
client.bind(bindDn, bindPassword, function (err) {
if (err == null) {
const opts = {
filter: '&(sAMAccountName=' + query.email.split('@')[0] + ')',
scope: 'sub',
attributes: ['dn']
};
client.search('ou=Users,ou=xxxxxx,dc=xxxxx,dc=xx', opts, function (err, res) {
if (err) {
return callback(null, null)
}
res.on('searchEntry', function (entry) {
userDn = entry.objectName
client.bind(userDn, passwd, function (err) {
if (err == null) {
console.log("ldap positive")
onSuccess(query, adminMail, userObj, callback)
client.unbind()
return null
} else {
console.log("ldap negative")
client.unbind()
return callback(null, null)
}
})
})
res.on('error', err => {
console.error('error: ' + err.message);
client.unbind()
return callback(null, null)
});
res.on('end', result => {
//if nothing written (user not found)
if(result.connection._writableState.writing == false){
client.unbind()
return callback(null, null)
}
});
});
} else {
return callback(null, null)
}
})
}
在网上搜索了一会儿之后,我发现问题是由两次发送标头或类似原因引起的。如果我注释掉以下几行,则登录按预期进行。但是,当在Active Directory中找不到该用户时,登录挂起,而不是输入错误的用户名/密码。
res.on('end', result => {
//if nothing written (user not found)
if(result.connection._writableState.writing == false){
client.unbind()
return callback(null, null)
}
});
我试图修改代码很多,以使其正常运行,但没有成功。对于不熟悉node.js的人,我不难发现错误。但是我是个流血的菜鸟。 ^^
有人可以帮我吗?
谢谢!