我正在使用sails.js实现一个nodejs应用程序。我希望我的用户只能通过https进行通信。为此,我需要以我的方式配置我的服务器,以便在每个响应中添加一个标题“严格传输-安全性”,“最大年龄=31536000”,告诉浏览器只与HSTS通信。现在我如何修改我将从sails发送的每个响应头,我搜索了文档,但没有找到任何帮助。
发布于 2014-02-05 14:39:42
策略仅应用于在config/policies.js中显式分配它们的控制器。
不要使用策略,而是尝试在config/express.js中直接添加一个快递中间件(如果该文件还不存在,则创建该文件)。该中间件应用于所有控制器。格式如下:
// config/express.js
"use strict";
exports.express = {
customMiddleware: function (app) {
app.use(function hsts(req, res, next) {
res.setHeader("Strict-Transport-Security", "max-age=31536000");
next();
});
}
}如果您有多个要使用的快递自定义中间件,我的建议是将每个中间件功能保存在自己的文件中。我将提供一个示例,使用您的中间件以及接受某些选项的附加中间件。
// config/express.js
"use strict";
var hsts = require('../lib/middleware/hsts');
var staticguard = require('../lib/middleware/staticguard');
exports.express = {
customMiddleware: function (app) {
// ordering of middleware matters!
app.use(hsts);
app.use(staticguard(/^\/protected\/.*$/));
}
}
// lib/middleware/hsts.js
"use strict";
module.exports = function hsts(req, res, next) {
res.setHeader("Strict-Transport-Security", "max-age=31536000");
next();
}
// lib/middleware/staticguard.js
"use strict";
module.exports = function (regex) {
return function (req, res, next) {
if (!regex.test(req.url)) {
return next();
}
res.end('you are not allowed!');
}
};如果您试图让多个文件在“express.customMiddleware”命名空间上导出一个函数,我相信只有最后加载的文件的middleWare才能工作。不过我还没试过呢。
发布于 2014-02-05 02:32:33
您应该能够使用风帆政策。有了这个,您应该能够创建一个策略来更改要发送回的标头。
// policies/hsts.js
module.exports = function hsts(req, res, next) {
res.setHeader("Strict-Transport-Security", "max-age=31536000");
};https://stackoverflow.com/questions/21566912
复制相似问题