sails.js - Sails 1.0 POST 路由出现 403 错误

标签 sails.js

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

'PUT/api/v1/entrance/login': { action: '入口/登录' }, '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 的最后一条路由。 我在controllers目录中创建了一个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
    });
  }

};

我的问题是,每当我尝试从客户端访问此 Controller 时,都会出现 403 禁止错误。 当我将路由从 POST 更改为 GET 时,它按预期工作(ofc 我正在使用来自客户端的正确 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 本身的错误还是我做错了什么。

最佳答案

至少有两种方法可以解决这个问题。
可能你正在使用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
  }

关于sails.js - Sails 1.0 POST 路由出现 403 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50123460/

相关文章:

sails.js - sails 中的水或水是否支持结合?

node.js - 如何给集合命名(当我们映射多对多关系时自动生成)?

console - 在其他端口上运行 sails 控制台,然后 sails 抬起

iphone - Ionic 和 sails.js(后端)测试

kubernetes - Grunt watch 在 Kubernetes 中抛出同步 :dev not found error with Sails. js

node.js - sails .js : How to return access token after user registration using sails-generate-auth?

javascript - 风 sails 能否填充超过一层深度的关联?

node.js - npm 错误!无法读取未定义的属性 'pause' --Bluemix

javascript - 通过路由 sails.js 重定向请求

sails.js - 如何通过 Waterline 中的验证更新散列密码?