为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

莫回无 2023-07-06 17:18:50
我一直在尝试使用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”中的错误)。
查看完整描述

1 回答

?
隔江千里

TA贡献1906条经验 获得超10个赞

这可能是由 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)


查看完整回答
反对 回复 2023-07-06
  • 1 回答
  • 0 关注
  • 101 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信