【问题标题】:How protect a RestFul Api in Nodejs?如何保护 Nodejs 中的 RestFul Api?
【发布时间】:2013-09-13 10:40:22
【问题描述】:

现在我使用https://github.com/baugarten/node-restful 来帮助我在 API 中工作,问题是?

我在 Express 框架中工作,有没有办法保护从其他站点到我的“GET”请求。

我使用 express 的 CSRF,但只能通过带有 FORbidden 403 消息的 POST、PUT、DELETE 方法工作给我一个包含所有帖子的数组。

  app.use(express.csrf());
  app.use(function(req, res, next){
      res.locals.token = req.session._csrf;
      next();
  });
  app.use(app.router);

你有什么建议?还有其他模块可以更好地工作 Api 吗?如何保护 nodejs 中的 Api?拥有者需要学习的最佳实践是什么?

感谢您的帮助。

【问题讨论】:

    标签: javascript node.js mongodb mongoose event-driven


    【解决方案1】:

    尝试专门为此设计的 Express 中间件。例如:

    var express = require('express');
    var app = express();
    
    // simple middle ware to protect your URI
    app.use(function(req, res, next){
      if (req.method == 'GET') {
         if (!req.locale.token) { res.send(403); } // custom to fit your policy
         else if (req.protocol !== "https") {res.redirect("your-secure-url");}
         else { next(); }
      }
    });
    

    【讨论】:

    • 好 我还发现一个出于安全问题的 api 将在 HTTPS 协议下运行,您知道任何允许我执行这种情况的模块吗?谢谢
    猜你喜欢
    • 2012-12-15
    • 1970-01-01
    • 1970-01-01
    • 2012-11-25
    • 2018-09-13
    • 1970-01-01
    • 1970-01-01
    • 2020-08-13
    • 2020-10-24
    相关资源
    最近更新 更多