我已经在使用 Passport 对用户进行身份验证。我将 kibana 3 添加到 Assets 文件夹中,并希望用户只有在经过身份验证后才能访问它。我该怎么做?
最佳答案
assets 文件夹用于存放公开可用的文件,例如您的图像和 Javascript。如果您想保护这些文件,您可以覆盖 Sails 中的默认 www
中间件,它会激活 Express 静态处理程序来为这些文件提供服务(请参阅 this answer 中有关覆盖默认中间件的详细信息),或者将要保护的文件保存在不同的位置并使用 Controller 操作来为它们提供服务(可能是更合理的选择)。
因此,您可以将文件保存在 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);
}
};
然后像保护任何其他需要身份验证的区域一样,使用策略保护该 Controller /操作。
关于static - 如何验证对 sails.js 中 Assets 文件夹的访问权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24235653/