【问题标题】:Sails 1.0 POST route gives 403 errorSails 1.0 POST 路线给出 403 错误
【发布时间】:2018-05-01 20:35:03
【问题描述】:

在我的 routes.js 文件中,我定义了这样的路由:

'PUT /api/v1/entrance/login': { action: 'entrance/login' }, 'POST /api/v1/entrance/signup': { action: 'entrance/signup' }, 'POST /api/v1/entrance/send-password-recovery-email': { action: 'entrance/send-password-recovery-email' }, 'POST /api/v1/entrance/update-password-and-login': { action: 'entrance/update-password-and-login' }, 'POST /api/v1/deliver-contact-form-message': { action: 'deliver-contact-form-message' }, 'POST /api/v1/getEventsForICalUrl': 'IcalController.getEvents',

我刚刚使用了默认生成的代码并添加了getEventsForIcalUrl 的最后一条路由。 我在控制器目录中创建了一个IcalController,它有一个动作getEvents,它简单地呈现一个像这样的json:

module.exports = {


  /**
   * `IcalController.getEvents()`
   */
  getEvents: async function (req, res) {
    console.log("hit here");
    let events = [];
    for (var i = 0; i < 20; i++) {
      events.push({foo: "bar" + i});
    }
    return res.json({
      events: events
    });
  }

};

我的问题是,每当我尝试从客户端访问此控制器时,都会出现 403 禁止错误。 当我将路由从 POST 更改为 GET 时,它按预期工作(我正在使用来自客户端的正确 GET/POST 请求作为路由)。

不确定是什么问题。 我还检查了日志。当我使用 GET 时,它的打印“点击这里”。 在我的策略文件中看起来像这样(因为它是生成的。我没有更改它):

module.exports.policies = {

  '*': 'is-logged-in',

  // Bypass the `is-logged-in` policy for:
  'entrance/*': true,
  'account/logout': true,
  'view-homepage-or-redirect': true,
  'deliver-contact-form-message': true,

};

我的“已登录”策略文件是这样的:

module.exports = async function (req, res, proceed) {

  // If `req.me` is set, then we know that this request originated
  // from a logged-in user.  So we can safely proceed to the next policy--
  // or, if this is the last policy, the relevant action.
  // > For more about where `req.me` comes from, check out this app's
  // > custom hook (`api/hooks/custom/index.js`).
  console.log("req.me=" + req.me);
  if (req.me) {
    return proceed();
  }

  //--•
  // Otherwise, this request did not come from a logged-in user.
  return res.unauthorized();

};

我只是将 console.log 放在这个文件中。其他的则默认从sails new生成。

日志显示,使用 POST,这个也没有被命中。(我在 console.logs 中没有看到“req.me =”..)但是使用 GET 时这个被命中。

该路由似乎不适用于 POST 请求。我想知道它是sails js本身的错误还是我做错了什么。

【问题讨论】:

  • 你试过浏览器的GET请求和邮递员的POST吗?
  • 我从我的sails js 前端(使用axios 库)尝试了POST。还检查了 chrome @HamzaFatmi 中的网络流量
  • 我认为当您执行POST 时,您可能没有将cookie 与请求一起发送,因为从浏览器执行GET 时,它会自动为您完成。
  • 如何通过提交表单来提出post请求?
  • @Logan_B 你不应该:我在那个页面上两次强调这个想法。

标签: sails.js


【解决方案1】:

至少有两种方法可以解决这个问题。
可能您正在使用csrf。如果你这样做了,你的配置可能包括这个:

module.exports.security = { // With Sails <1.01 this probably is in another file
    csrf: true
};

而且(如果您使用的是sails v1.01),您应该使用这条路线:

'GET /csrfToken': { action: 'security/grant-csrf-token' },

因此,要在前端获取数据,您只需:

function get_some_records(object_with_data) {
  $.get("/csrfToken", function (data, jwres) {
    if (jwres != 'success') { return false; }
    msg = {
      some_data: object_with_data,
      _csrf: data._csrf
    };
    $.post("get_some_records", msg, function(data, status){});
  });
}

但是如果你正在使用一些后台作业,Sails 不会轻易给你 csrf(可能有某种方式)。因此,您只需创建这样的路线:

 'post /get_some_records': {
     action: 'home/get_some_records',
     csrf: false
  }

【讨论】:

  • 我添加了这个,但我仍然被禁止,但是,这在邮递员中确实有效
  • 您是否必须在客户端执行某些操作才能使其正常工作?邮递员可以工作,但是当从我的 Angular 6 客户端时,我仍然会收到 403 错误,即使正文中有 csrftoken
【解决方案2】:

您可能使用 Rest 客户端进行测试(如 Postman)。只需禁用 csrf 保护。在 /config/security.js 文件中

csrf: false

【讨论】:

    猜你喜欢
    • 2014-09-18
    • 2014-09-29
    • 1970-01-01
    • 1970-01-01
    • 2015-06-15
    • 2016-02-05
    • 2011-12-01
    • 1970-01-01
    • 2023-03-26
    相关资源
    最近更新 更多