Node.JS/Passport-CI-OIDC - 错误:发送后无法设置标头。

我一直在尝试使用passport-ci-oidc 和node.js 执行一些身份验证。在之前的变体中,我使用了passport-idaas-openidconnect,一切对我来说都很好。现在我收到以下错误:


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 (/Users/LSBTUser1/ibm/iERPedia/node_modules/express/lib/response.js:718:10)

    at app.use (/Users/LSBTUser1/ibm/iERPedia/app.js:219:7)

    at Layer.handle [as handle_request] (/Users/LSBTUser1/ibm/iERPedia/node_modules/express/lib/router/layer.js:95:5)

    at trim_prefix (/Users/LSBTUser1/ibm/iERPedia/node_modules/express/lib/router/index.js:312:13)

    at /Users/LSBTUser1/ibm/iERPedia/node_modules/express/lib/router/index.js:280:7

    at Function.process_params (/Users/LSBTUser1/ibm/iERPedia/node_modules/express/lib/router/index.js:330:12)

    at next (/Users/LSBTUser1/ibm/iERPedia/node_modules/express/lib/router/index.js:271:10)

    at urlencodedParser (/Users/LSBTUser1/ibm/iERPedia/node_modules/body-parser/lib/types/urlencoded.js:91:7)

    at Layer.handle [as handle_request] (/Users/LSBTUser1/ibm/iERPedia/node_modules/express/lib/router/layer.js:95:5)

即使我将删除错误消息中提到的行 - 我也会收到相同的错误,但不会提及我的代码的任何位置(仅“/node_modules/serve-static/node_modules/send/index.js”中的错误)。


莫回无
浏览 133回答 1
1回答

隔江千里

这可能是由 doAuth 中的重定向引起的。如果中间件没有渲染任何内容,您只应该调用 next() 。这应该修复:function doAuth(req, res, next) {  if (req.originalUrl.indexOf('/auth/sso/callback') === 0 || req.originalUrl === '/login') {        return next();  }  if (!req.isAuthenticated()) {    callback_url = req.originalUrl;    return res.redirect('/login');  }  return next();}还有两件事:而不是使用 app.use('*', (request, response, next) => { doAuth(request, response, next); });你可以简单地使用app.use('*', doAuth);并在文件开头设置 cors 和通用标头(否则某些路由将没有 cors)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript