我看到,每当我对CORS启用的Restify服务执行HTTP时,访问控制允许源标头默认设置为通配符。我宁愿它呼应我发送的起源,因为这是每个OWASP的最佳实践。有什么建议让我这么做吗?尝试了API文档中的默认头、格式化程序等,但没有成功。
我要做的是:
var server = restify.createServer({
name: 'People Data Service',
version: '1.0.6'
});
server.pre(wrapper(restify.pre.pause()));
// Cleans up sloppy paths
server.pre(wrapper(restify.pre.sanitizePath()));
server.use(wrapper(restify.acceptParser(server.acceptable)));
server.use(wrapper(restify.authorizationParser()));
server.use(wrapper(restify.queryParser()));
server.use(wrapper(restify.bodyParser()));
server.use(wrapper(restify.CORS()));
// server.use(wrapper(restify.fullResponse()));
// Needed this for OPTIONS preflight request: https://github.com/mcavage/node-restify/issues/284
function unknownMethodHandler(req, res) {
if (req.method.toUpperCase() === 'OPTIONS') {
console.log('Received an options method request from: ' + req.headers.origin);
var allowHeaders = ['Accept', 'Accept-Version', 'Content-Type', 'Api-Version', 'Origin', 'X-Requested-With', 'Authorization'];
if (res.methods.indexOf('OPTIONS') === -1) {
res.methods.push('OPTIONS');
}
res.header('Access-Control-Allow-Credentials', false);
res.header('Access-Control-Expose-Headers', true);
res.header('Access-Control-Allow-Headers', allowHeaders.join(', '));
res.header('Access-Control-Allow-Methods', res.methods.join(', '));
res.header('Access-Control-Allow-Origin', req.headers.origin);
res.header('Access-Control-Max-Age', 1209600);
return res.send(204);
}
else {
return res.send(new restify.MethodNotAllowedError());
}
}
server.on('MethodNotAllowed', wrapper(unknownMethodHandler));发布于 2013-10-02 19:19:31
通过对Restify中的cors.js文件进行简单修改,我找到了这样一种方法,如下所示:
Index: node_modules/restify/lib/plugins/cors.js
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- node_modules/restify/lib/plugins/cors.js (revision )
+++ node_modules/restify/lib/plugins/cors.js (revision )
@@ -102,6 +102,7 @@
res.setHeader(AC_ALLOW_ORIGIN, origin);
res.setHeader(AC_ALLOW_CREDS, 'true');
} else {
+ origin = req.headers['origin'];
res.setHeader(AC_ALLOW_ORIGIN, origin);
}这将将原点添加到响应头,即使凭据设置为false。此外,如果您想要白名单您的起源,您可以简单地添加到您的配置:
server.use(restify.CORS({'origins': ['http://localhost', 'https://domain.my.com', 'https://anotherDomain.my.com']}));发布于 2013-09-26 21:49:32
我在我的restify基础应用程序上这样做:
//setup cors
restify.CORS.ALLOW_HEADERS.push('accept');
restify.CORS.ALLOW_HEADERS.push('sid');
restify.CORS.ALLOW_HEADERS.push('lang');
restify.CORS.ALLOW_HEADERS.push('origin');
restify.CORS.ALLOW_HEADERS.push('withcredentials');
restify.CORS.ALLOW_HEADERS.push('x-requested-with');
server.use(restify.CORS());您需要使用restify.CORS.ALLOW_HEADERS.push方法首先将您想要的头推入restify,然后使用CORS中间件引导CORS函数。
发布于 2015-11-23 21:34:38
其实很简单。
你需要插入一个服务器,然后你才能设置它!
var server = restify.createServer( . );
到配置CORS遵循以下步骤:
1.您需要指定ALLOW_HEADERS
这是用于在到达CORS请求时设置的标题数组。
2.配置CORS-插件
server.use(restify.CORS({凭据:真}));
该选项‘凭证’将被restif-CORS-插件使用,以添加访问控制-允许-凭据和访问-控制-允许-原产地。
在客户端,在xhr传输上使用withCredentials = true很重要。
我希望这些答案能帮助你。
..。写代码&玩得开心!
https://stackoverflow.com/questions/18859052
复制相似问题