【问题标题】:Express session not saving/persisting over https using azure app services使用 azure 应用服务的快速会话未通过 https 保存/保留
【发布时间】:2021-02-25 02:41:17
【问题描述】:

我已经查看了很多关于此的问题,并尝试了所有“解决方案”组合来解决我的问题,但似乎无法解决这个问题。

我目前有一个客户端 React 应用程序托管在 azure 上。我们简称为https://clientside.net

我还有一个服务器端节点 js 应用程序托管在 azure 上,我们称之为https://serverside.net

我似乎无法在验证用户时保存会话变量。顺便说一句,这在 localhost 上运行良好。

例如在客户端,我们使用 axios 发出请求,如下所示:

const headers = {
    withCredentials: true,
    headers: { auth: process.env.REACT_APP_SECRET },
  };
  
axios.get(`${process.env.REACT_APP_SERVER}/get/auth`, headers).then((response) => console.log("blah blah blah")); 

在服务器端,这是设置快速会话的方式...

app.use(
  cors({
    origin: ["https://clientside.net"],
    methods: ["GET", "POST"],
    credentials: true,
  })
);

app.set("trust proxy", 1);
app.use(
  session({
    name: "sid",
    saveUninitialized: false,
    resave: false,
    secret: "shhh",
    cookie: {
      domain: ".clientside.net",
      maxAge: 1000 * 60 * 60 * 8,
      secure: true,
      httpOnly: false,
    },
  })
);

在服务器端的身份验证路由中,我们像这样保存会话...

req.session.username = req.body.username;
req.session.password = req.body.password;
req.session.save(() =>
    res
      .status(202)
      .json({ 
      authenticated: true, username:        req.session.username})
);

在刷新或尝试访问任何其他路由时,找不到 req.session.username 和 req.session.password。我的会话配置有问题吗?或者我可能错过了什么?我感谢任何和所有的帮助!谢谢大家

【问题讨论】:

  • 看起来好像我的 sessionID 在每次请求时都在变化

标签: express session https axios session-variables


【解决方案1】:

我已经解决了问题。

使用 azure app services 部署应用程序意味着您将使用反向代理。

在我的例子中是 nodeiis 网络服务代理。

客户端向 iis 服务发出请求,然后将请求路由到节点服务器。

我做了很多更改,但让我启动并运行的一个是将 cors 源和会话域切换为服务器端域/ip,就像这样......

app.use(
  cors({
    origin: ["https://serverside.net"],
    methods: ["GET", "POST"],
    credentials: true,
  })
);

app.set("trust proxy", 1);
app.use(
  session({
    name: "sid",
    saveUninitialized: false,
    resave: false,
    secret: "shhh",
    cookie: {
      domain: ".serverside.net",
      maxAge: 1000 * 60 * 60 * 8,
      secure: true,
      httpOnly: false,
    },
  })
);

现在会话变量可以在登录时成功保存。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-07
    • 1970-01-01
    • 2018-05-19
    • 2019-01-24
    相关资源
    最近更新 更多