【发布时间】:2014-08-05 19:17:18
【问题描述】:
我已经在使用护照对用户进行身份验证。我将 kibana 3 添加到 assets 文件夹中,并希望用户只有在经过身份验证后才能访问它。我该怎么做?
【问题讨论】:
标签: static sails.js assets passport.js
我已经在使用护照对用户进行身份验证。我将 kibana 3 添加到 assets 文件夹中,并希望用户只有在经过身份验证后才能访问它。我该怎么做?
【问题讨论】:
标签: static sails.js assets passport.js
assets 文件夹用于存放公开可用的文件,例如您的图片和 Javascript。如果你想保护这些文件,你可以覆盖 Sails 中的默认 www 中间件,它会激活 Express 静态处理程序来服务这些文件(请参阅 this answer 中覆盖默认中间件的详细信息),或者保存你想要的文件在不同的位置进行保护并使用控制器操作为它们提供服务(可能是更合理的选择)。
因此,您可以将文件保存在 protected_files 中,并将这样的路由添加到 config/routes.js:
'/protected/:file': 'ProtectedFileController.download'
然后在 controllers/ProtectedFileController:
var fs = require('fs');
var path = require('path');
module.exports = {
download: function(req, res) {
// Get the URL of the file to download
var file = req.param('file');
// Get the file path of the file on disk
var filePath = path.resolve(sails.config.appPath, "protected_files", file);
// Should check that it exists here, but for demo purposes, assume it does
// and just pipe a read stream to the response.
fs.createReadStream(filePath).pipe(res);
}
};
然后使用与需要身份验证的任何其他区域一样的策略来保护该控制器/操作。
【讨论】:
fs.createReadStream(filePath).pipe(res);,您还可以使用standard express method 来下载文件。(在sails 中仅适用于HTTP 传输协议)