【问题标题】:How to protect a file directory and only allow authenticated users to access the files?如何保护文件目录并只允许经过身份验证的用户访问文件?
【发布时间】:2012-12-25 01:04:07
【问题描述】:

如何限制文件夹,只有登录我的 Meteor 应用程序的人才能下载文件?

我研究了多种方法,但主要问题是我无法访问(我得到null.):

Meteor.user() or this.userId()

我试过了:

__meteor_bootstrap__.app
    .use(connect.query())
    .use(function(req, res, next) {
        Fiber(function () {  

          // USER HERE?

        }).run();
    });

__meteor_bootstrap__.app.stack.unshift({

    route: "/protected/secret_document.doc", // only users can download this

    handle: function(req, res) { Fiber(function() {

        // CHECK USER HERE ?

        // IF NOT LOGGED IN:
        res.writeHead(403, {'Content-Type': 'text/html'});
        var content = '<html><body>403 Forbidden</body></html>';
        res.end(content, 'utf-8');
    }).run() }
});

【问题讨论】:

    标签: meteor


    【解决方案1】:

    您可以try storing the files in mongodb,这意味着它们将被连接到您的收集系统中,并且可以在客户端和服务器上进行查询。然后,只需将相关数据发布到特定用户的客户端,或者使用 Meteor.methods 以这种方式公开信息。

    示例:

    假设文件存储在 MongoDB 中,我们先将它们发布到客户端:

    Meteor.publish("files", function(folder) {
      if (!this.userId) return;
      // the userHasAccessToFolder method checks whether
      // this user is allowed to see files in this folder
      if (userHasAccessToFolder(this.userId, folder))
        // if so, return the files for that folder
        // (filter the results however you need to)
        return Files.find({folder: folder});
    });
    

    然后在客户端,我们自动订阅发布的频道,这样每当它发生变化时,它就会被刷新:

    Meteor.startup(function() {
      Meteor.autosubscribe(function() {
        // send the current folder to the server, 
        // which will return the files in the folder
        // only if the current user is allowed to see it
        Meteor.subscribe("files", Session.get("currentFolder"));
      });
    });
    

    注意。我没有测试过上面的代码,所以认为它是伪代码,但它应该为你指明解决这个问题的大方向。困难的部分是将文件存储在mongodb中!

    【讨论】:

    • 如何在不渲染模板的其余部分的情况下将我的文件发布为 XML 或 JSON?
    • 什么意思?如果您的文件在 mongo 中,您可以使用集合 API 从数据库中检索它(如果您允许访问)。例如。 Files.find({filename: "myfile.jpg"});
    • 也许你可以发布一个例子?问题是我无法控制访问。
    • 花了一点时间,但我发布了一个例子!
    • 邮件列表和问题跟踪讨论中有一些迹象表明,这是 Meteor 的发展方向,并且可能成为解决此类问题的框架推荐方法。
    【解决方案2】:

    我会更关心为什么Meteor.user() 不起作用。

    几个问题:

    • 您在使用流星 0.5.0 吗?
    • 您是否已将accounts-base 添加到您的流星项目中?
    • 您是否使用过流星的登录系统之一(accounts-passwordaccounts-facebook 等)? (可选 - accounts-ui 以便于使用?)
    • 您还有自动发布功能吗?或者您是否正确设置了发布/订阅?

    Meteor.user() 应该是当前用户,Meteor.users 应该是所有以前登录用户的 Meteor 集合。

    【讨论】:

    • 您好,谢谢您的回答。 Auth 在应用程序内部工作得非常好。但是当我直接请求其中一个文件时,auth 无法检查用户是否登录。从我尝试过的事情来看,在大多数情况下,我得到“”Meteor.userId 只能在方法调用中调用。在发布函数中使用 this.userId。”” 当然,我尝试了多种方式的“.userId”。
    • 如果您的主应用程序的身份验证工作正常,您可以尝试使用Meteor Router 之类的东西 - 您可以检查 url 参数以查看文件是否存在,并检查登录用户是否具有正确的权限,将标头设置为正确的内容类型,然后读取并传递文件(就像普通的 node.js http 服务器一样)
    • 说实话,这不是一个很好的解决方案——它不能很好地处理大文件,而且感觉像是一种解决方法。我将为此提出功能请求,如果失败,请为其编写一个模块。 (我宁愿它是内置的——感觉它应该是核心流星服务器的一部分)
    • 谢谢 stef,如果您不介意的话,能否在此处链接功能请求?
    猜你喜欢
    • 2017-04-23
    • 2019-01-03
    • 1970-01-01
    • 2019-11-18
    • 1970-01-01
    • 2020-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-09
    相关资源
    最近更新 更多