如何在sails.js中验证对资产文件夹的访问权限

use*_*321 2 static assets sails.js passport.js

我已经使用护照来验证用户身份.我将kibana 3添加到assets文件夹,并希望用户只有在经过身份验证后才能访问它.我该怎么做呢 ?

sgr*_*454 8

资产的文件夹用于公开发表的文件,如图像和Javascript角.如果要保护这些文件,可以覆盖wwwSails中的默认中间件,这会激活Express静态处理程序以提供这些文件(请参阅本答复中有关覆盖默认中间件的详细信息),或者保存要保护的文件.不同的位置并使用控制器动作来为它们服务(可能是更合理的选择).

因此,您可以将文件保存在protected_files中,并将这样的路由添加到config/routes.js:

'/protected/:file': 'ProtectedFileController.download'
Run Code Online (Sandbox Code Playgroud)

然后在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);

    }


};
Run Code Online (Sandbox Code Playgroud)

然后使用类似于您需要身份验证的任何其他区域的策略来保护该控制器/操作.