【问题标题】:Cannot request from React. Always showing session-token error无法从 React 请求。总是显示会话令牌错误
【发布时间】:2021-11-03 11:16:37
【问题描述】:

我有 2 台服务器 { React:3000 和 Node:5000 } 我在后端使用 express-session, 但是每当我使用正确的 Session-Token 从 React( Axios ) 请求时, 它总是显示 Bad-Session-Token。 谁能帮我解决这个问题?

会议会议:

    const mongoDBstoreSess = new MongoDBStore({
        uri: "mongodb://localhost:27017/SomeDb",
        collection: "mySessions"
    });
    app.use(
        session({
          name: "SomeCookie",
          secret: "SuperSecret",
          resave: true,
          saveUninitialized: false,
          store: mongoDBstoreSess,
          cookie: {
            maxAge: 7.2e+6,
            // sameSite: false,
            secure: false,
            httpOnly: false
          }
        })
    );
    app.use(csrf());
    app.use(function(err, req, res, next) {
      // console.log(require('ip').address());
      if (err.code !== 'EBADCSRFTOKEN') return next(err);
      res.type("application/json").status(403).send({msg: "Session error.", res: {}});
      console.log("Bad Session!");
    });

React Axios 请求对象:

{
      method: "post",
      url: "",
      baseURL: site.reqUrl,
      headers: {"CSRF-Token":"lkIpPT8A-ts2YcBc_YggWDYwe5f74k8jTWHM"},
      // xsrfHeaderName: "CSRF-Token",
      data: {},
}

【问题讨论】:

    标签: node.js reactjs express mongoose express-session


    【解决方案1】:

    令牌必须从登录或其他方法获取令牌来检索令牌。 从服务器检索到令牌后,您必须将其存储在 storage / cookie 中。 然后在axios请求对象中使用token。

    【讨论】:

      猜你喜欢
      • 2014-06-11
      • 2018-11-01
      • 2016-06-22
      • 1970-01-01
      • 1970-01-01
      • 2016-06-19
      • 1970-01-01
      • 1970-01-01
      • 2014-09-01
      相关资源
      最近更新 更多