在我的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。我在IcalControllercontrollers目录中创建了一个目录,它具有一个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
});
}
};
Run Code Online (Sandbox Code Playgroud)
我的问题是,每当我尝试从客户端访问此控制器时,都会出现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,
};
Run Code Online (Sandbox Code Playgroud)
我的“已登录”策略文件是这样的:
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();
};
Run Code Online (Sandbox Code Playgroud)
我只是将该console.log放在此文件中。其他则与默认情况下从生成的一样sails new。
日志显示使用POST时,也不会被击中。(我在console.logs中看不到“ req.me =“..。)。但是使用GET时,会被击中。
似乎该路由不适用于POST请求。我想知道这是发帆js本身的错误还是我做错了什么。
至少有两种方法可以解决此问题。
可能您正在使用csrf。如果这样做,您的配置可能包括以下内容:
module.exports.security = { // With Sails <1.01 this probably is in another file
csrf: true
};
Run Code Online (Sandbox Code Playgroud)
并且(如果您使用的是Sails v1.01),则应执行以下路线:
'GET /csrfToken': { action: 'security/grant-csrf-token' },
Run Code Online (Sandbox Code Playgroud)
因此,要在前端获取数据,您只需:
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){});
});
}
Run Code Online (Sandbox Code Playgroud)
但是,如果您正在使用某些后台作业,Sails不会轻易为您提供csrf(可能有某种方法)。因此,您只需创建如下路线:
'post /get_some_records': {
action: 'home/get_some_records',
csrf: false
}
Run Code Online (Sandbox Code Playgroud)
小智 5
您可能使用 Rest 客户端进行测试(例如 Postman)。只需禁用 csrf 保护即可。在 /config/security.js 文件中
csrf: false
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1295 次 |
| 最近记录: |