traefik 的 forwardAuth 中间件可以用来保护浏览器页面(不是 api)吗?

Can traefik's forwardAuth middleware be used to secure a browser page (not an api)?

我需要使用存储在 cookie 或 url 参数中的令牌来保护网页。我能找到的所有使用 forwardAuth 中间件的示例似乎都是为了保护 API,因为在 API 请求中提供 headers 很容易。发送自定义 headers 不是浏览器的选项,所以我需要使用 cookie。

我想通过查询字符串 arg 传递身份验证令牌,例如 ?token=ABCDEFG,然后存储在 cookie 中以供将来请求使用。工作流程如下所示:

我已尝试使用 forwardAuth 进行试验,以了解如何做到这一点。 auth 端点读取授权 header,但我需要一些东西来读取请求中的 cookie 并将其转换为授权 header。

有什么方法可以用 Traefik 完成吗?

看起来答案是。本来我还以为traefik不会转发cookie,结果好像是转发cookie。

我最终在与 traefik 相同的主机上创建了一个“sidecar”auth 容器,这样 auth 请求会更快。

auth 函数如下所示 (node/express):

app.get('/auth', (req, res) => {
  logger.info('CHECKING AUTH');

  const url = new URL(`${req.headers['x-forwarded-proto']}://` +
                      `${req.headers['x-forwarded-host']}` +
                      `${req.headers['x-forwarded-uri']}`);

  const urlAuthToken = url.searchParams.get('token');

  if (urlAuthToken) {
    url.searchParams.delete('token');
    const domain = BASE_DOMAIN;
    const sameSite = false;
    const secure = url.protocol === 'https:';
    return res
        .cookie('auth-token', urlAuthToken, {domain, sameSite, secure})
        .redirect(url.toString());
  }

  // Simulate credentials check
  if (req.cookies['auth-token'] === 'my-little-secret') {
    return res.status(200).send();
  }

  return res.status(401).send('<h1>401: Unauthorized</h1>');
});