删除 API post 调用上的 csrf 保护 [英] Remove csrf protecteion on API post calls

查看:31
本文介绍了删除 API post 调用上的 csrf 保护的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的 Express 3.0 应用程序中删除 csrf,因为我不需要它.我使用 oauth 来验证客户端.使用 express.csrf() 时是否是将 API url 列入白名单的中间件?

I would like to remove csrf from my Express 3.0 application as i don't need it there. I use oauth to validate clients. Is the a middleware to whitelist API urls when using express.csrf()?

推荐答案

您可以通过两种方式做到这一点.

You can do that in two ways.

1.) 创建一个自己的小型中间件,允许白名单 url 模式不被 csrf 之类的阻止;

1.) Create a small middleware of your own to allow white list url patterns not to be blocked by csrf like;

var express = require("express");
var expressCsrf = express.csrf();
var app = express.createServer();

var customCsrf = function (req, res, next) {
    // I assume exact match, but you can use regex match here
  var csrfEnabled = true;
  var whiteList = new Array("/pattern1/param1","/pattern2/param2","/pattern3/param3");
  if (whiteList.indexOf(req.path) != -1) {
    csrfEnabled = false;
  }

  if (csrfEnabled) {
    expressCsrf(req, res, next);
  } else {
    next();
  }
}

app.use(customCsrf);
app.listen(3000);

2.) 在您要启用的控制器上使用 csrf 中间件.例如,您想在配置文件保存控制器上使用 csrf 检查;

2.) Use csrf middleware on your controllers you want to enable. For example, you want to use csrf check on profile save controller;

app.post("/profile/save", express.csrf(), function(req, res, next) {
    // put your code here
});

这篇关于删除 API post 调用上的 csrf 保护的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆