auth0+passport.js 超过 1 个实例的重定向过多

auth0+passport.js too many redirects with more than 1 instance

基础设施:

云: aws beanstalk 为容器代理服务器开启 nginx 应用程序负载均衡器 - 仅限 https,默认进程 (https) 私有子网中的 2+ 个实例 启用端到端加密如下 https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/configuring-https-endtoend.html https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/https-singleinstance-docker.html

实例上的自签名证书 实例 运行 docker

在本地,我们有一个 3 容器来模拟基础设施,

1 nginx: 443 作为负载均衡器和 https 反向代理 2 个应用程序容器:分别为 3000:3000、3001:3001 所以,还没有端到端加密

软件: 作者 护照 (https://github.com/auth0/passport-auth0) 表示 反应 cookie 会话包

const sessionConfig = {
  name: 'sessionId',
  secret: uid.sync(18),
  secure: true,
  httpOnly: true,
  secureProxy: true,
  maxAge: 1800 * 1000
};

工作流程: 打开网站,点击登录link,然后重定向到auth0登录页面,输入username/passport后,我们点击提交。

当我们有超过 1 个实例 运行 时,我们遇到 "redirect too many times"。如果我在 aws 中打开目标组的粘性会话,问题就会消失。

我们在尝试本地 docker 环境时看到了同样的情况。

在此代码中,

 router.get('/callback', (req, res, next) => {
    authenticate('auth0', (authErr, user) => {
      if (authErr) {
        console.error(`Error authenticating user: ${authErr}`);
        return next(authErr);
      }
      if (!user) {
        console.info(`No user data, redirecting to login page`);
        return res.redirect('/login');
      }

逻辑总是命中 - if (!user),我们不确定为什么在多实例、负载平衡器设置中会发生这种情况。

更新:

抱歉我是新手,

我想知道我是否可以使用 cookie-session 而不是 express-session,因为 JWT 应该不会在服务器中存储信息。

我问是因为看了一些passport和Auth0的教程,里面也只提到了expression-session。

Auth0 使用的是 JWT,我可以使用 cookie-session 吗?如果是这样,我做错了什么?

PS。 这是我的会话配置:

const sessionConfig = {
  name: 'sessionId',
  domain: 'example.com',
  secret: uid.sync(18),
  secure: true,
  httpOnly: true,
  maxAge: 1800 * 1000
};

请指教和帮助。 谢谢! 周杰伦

问题已解决。

因为secret是随机的,所以服务器之间没有共享固定的secret。