static - 如何验证对 sails.js 中 Assets 文件夹的访问权限

标签 static sails.js assets passport.js

我已经在使用 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/

相关文章:

C编译器优化(gcc) : Automatically Inline a non-static function vs static function

C# "Enum"序列化 - 反序列化为静态实例

android - 从 Assets 中检索图像时出错

node.js - 在链接的 Docker 容器中的 Mongo 数据库中播种数据

java - 在 Android 测试项目中加载 Assets

html - HTML窗口小部件中未显示本地 flutter Assets 图像

PHP5静态关键字及用法

java - 什么是 "static"以及在哪里使用它?

node.js - sails.js 服务中的水线错误实现

node.js - 如何设计具有微服务架构的 sails.js 项目?