Node.js和Express会话管理 - 后退按钮问题

20

我在我的Express应用程序中有一个受限区域'/dashboard'。我使用一个非常小的函数来限制访问:

app.get('/dashboard', loadUser, function(req, res){
  res.render('dashboard', {
    username: req.session.username
  });
});

function loadUser(req, res, next){
  if (req.session.auth) {
    next();
  } else {
    res.redirect('/login');
  }
};

问题在于,当我调用"logout"函数注销用户时,该用户的所有会话将被终止,而不仅仅是当前活动的会话。
app.get('/logout', function(req, res){
  if (req.session) {
    req.session.auth = null;
    res.clearCookie('auth');
    req.session.destroy(function() {});
  }
  res.redirect('/login');
});

当会话被杀死后,但是当我在浏览器中点击"返回"按钮时,从浏览器缓存中得到了受限制的页面。这意味着没有对'/dashboard'执行GET操作,也没有用户登录验证。我尝试在元标记(Jade模板)中使用no-cache,但仍然不起作用。
meta(http-equiv='Cache-Control', content='no-store, no-cache, must-revalidate')
meta(http-equiv='Pragma', content='no-cache')
meta(http-equiv='Expires', content='-1')

有人吗?


7个回答

64
很遗憾,Josh的答案对我没有用。但是在搜索后,我找到了这个问题:如何处理缓存和浏览器后退按钮?,并将那里的答案用于此node.js/express问题。您只需要更改以下行即可。
res.header('Cache-Control', 'no-cache');

为了

res.header('Cache-Control', 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0');

现在,每次我使用浏览器的返回按钮时,页面都会重新加载而不是被缓存。 * express v4.x 更新 *
// caching disabled for every route
server.use(function(req, res, next) {
  res.set('Cache-Control', 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0');
  next();
});

// otherwise put the res.set() call into the route-handler you want

2
我遇到了类似的问题。你在哪里添加那行代码? - Scott
4
请尝试这个代码:app.use(function(req, res, next) { res.header('Cache-Control', 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0'); next(); }); 它会设置缓存控制头信息,确保不缓存任何响应,并在每次请求时重新验证。 - sidonaldson
@pkyeck,是的,我尝试过res.set,但不幸的是,我认为这个问题没有任何解决方案。看起来完全取决于浏览器。 - nikjohn
@nikjohn 这个完美地运行了,希望你也能让它为你工作,感谢 Phil 的答案。 - Thabo
这仅适用于GET端点。有没有想法让它也适用于POST端点? - Shivang Gupta
显示剩余2条评论

8
app.get('/dashboard', loadUser, function(req, res){
  res.header('Cache-Control', 'no-cache');
  res.header('Expires', 'Fri, 31 Dec 1998 12:00:00 GMT');

  res.render('dashboard', {
    username: req.session.username
  });
});

4

我使用的是Express ^4.16.3版本,根据@pkyeck所说的,以下方法对我有效。

我按照以下方式将其添加到我的路由中,它正常工作:

routes
.use(function(req, res, next) {
res.set('Cache-Control', 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0');
        next();
          })
          .get('/login', function(req, res, next){
              .....
})

1

简单的解决方案是在清除会话后..再次重定向到相同的路由..例如: 路由/sessionedpage 有会话变量..点击注销按钮后通过 req.session.destroy(function() {}); 清除会话变量,之后你尝试重定向到主页...而不是重定向到主页..重定向到/sessionedpage (同一路由)..为/sessionedpage编写if条件,如果(!res.sessions),则重定向到'/home'


1
你需要在使用Express后端和PUG前端的NodeJS应用程序中,在所有路由之前简单地添加以下中间件,以解决此后退按钮问题。这是最基本的解决方案:

enter image description here


为什么我不应该上传代码/数据/错误的图片? 链接 - undefined

0
var forwardAuthenticated = (req, res, next) => {
    res.set('Cache-Control', 'no-cache, private, no-store, must-revalidate, 
    post-check=0, pre-check=0');
    const token = req.cookies.jwt;
    if (!token) {
        return next()
    }
    res.redirect('/listProducts');  
}

0
为避免从缓存加载页面,请在app.js中使用以下代码,
注意:在使用路由器之前使用它。
app.use(function(req, res, next) { 
  res.header('Cache-Control', 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0');
   next();
 });

目前您的回答不太清晰,请进行[编辑]以添加更多细节,帮助其他人了解它如何回答问题。您可以在帮助中心找到有关编写良好答案的更多信息。 - Community

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接