节点JSExpress身份验证中间件无法正常运行

节点JSExpress身份验证中间件无法正常运行

本文介绍了节点JSExpress身份验证中间件无法正常运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在尝试通过向 app.js 中要求它并以此方式使用"它的方式,在对服务器的每个请求上运行函数 isUserAuthenticated :.use(authenticate.isUserAuthenticated).

I am attempting to run the function isUserAuthenticated on every request to the server by requiring it in app.js and 'using' it as so: app.use(authenticate.isUserAuthenticated).

我有一个/authenticate 回调路由,该路由由我们的SAML身份提供程序发布到,其中包含验证用户和会话所需的信息.这是我的 authenticate.js 文件中的内容:

I have an /authenticate callback route that is being POSTED to by our SAML Identity Provider which contains the information required to validate the user and the session. This is what is inside my authenticate.js file:

module.exports = router;
module.exports.isUserAuthenticated = function(req, res, next) {
    console.log(req.cookies.subject);
  if (req.cookies.subject) {
      console.log(req.cookies.subject)
    return next();
    } res.redirect("LINK TO IDP FOR VERIFICATION, callback func. is then ran to get value of user and session");
}

如前所述,此身份验证功能是必需的,并在 app.js 中使用: authenticate = require('./routes/authenticate')app.use(authenticate.isUserAuthenticated).

As referenced, this authentication function is being required and used in app.js: authenticate = require('./routes/authenticate'), and app.use(authenticate.isUserAuthenticated).

问题:无论 if 语句的什么变体,以验证请求中是否存在主题cookie,都不会触发身份验证检查并重定向到IDP身份验证路由的路由也没有被重定向.上面代码中的 console.log 检查返回:

The problem: No matter what variation of the if statement to verify if the subject cookie is present in the request, the authentication check is not being fired and the redirect to the IDP authentication route is not being redirected too. The console.log checks in the code above are returning:

未定义,以及 {} .

身份验证仅在一条路由上进行: router.use('/',isUserAuthenticated,function(req,res,next){... ,但是我试图在全球范围内使用此功能,因此我不必在每个路由上手动合并此中间件.

Authentication was working on a single route when I was using the isUserAuthenticated function manually like this: router.use('/', isUserAuthenticated, function(req, res, next) {..., but I am trying to use this function globally so I don't have to manually incorporate this middleware on each route.

任何建议/建议将不胜感激.谢谢你.

Any advice/suggestions would be greatly appreciated. Thank you.

推荐答案

如注释中所建议,

您可以将 isUserAuthenticated 函数移至app.js.看起来像这样

you can move the isUserAuthenticated function to app.js. It'd look something like this

app.use(function(req, res, next) {
  if (req.cookies.subject) {
    next();
    }
else
    res.redirect("LINK TO IDP FOR VERIFICATION, callback func. is then ran to get value of user and session");

})

这将处理所有请求,然后再将它们转发到路由.

This will process all the requests before they are forwarded to the routes later.

这篇关于节点JSExpress身份验证中间件无法正常运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 16:29