【问题标题】:how to protect a public dynamic folder in nodejs如何保护nodejs中的公共动态文件夹
【发布时间】:2012-08-08 07:18:03
【问题描述】:

我在public/images/picture.jpg中展示了带有jade的图片,但是我想保护一些图片或限制对public文件夹的访问怎么做??

project
    node_modules
    public
        images
            image.jpg
        javascripts
        stylesheets
        protected_folder*
            image_protected.jpg
    views

【问题讨论】:

  • 如果你在 linux 服务器上,如何更改文件权限。

标签: node.js express pug


【解决方案1】:

注意:对于所有这些示例,我使用的应用程序结构如下:

.
├── app.js
└── public
    ├── protected
    │   └── file.txt  <-- contains text "protected file"
    └── regular
        └── file.txt  <-- contains text "regular file"

您有几个选择。最简单的一种是让 Express 在公共中间件之前通过您的路由器路由请求,这样您就可以拦截请求:

var express = require('express');
var http = require('http');
var path = require('path');

var app = express();

// use app.router before express.static
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));

function userIsAllowed(callback) {
  // this function would contain your logic, presumably asynchronous,
  // about whether or not the user is allowed to see files in the
  // protected directory; here, we'll use a default value of "false"
  callback(false);
};

app.get('/', function(req, res, next) {
  res.end('Home page');
});

app.get('/protected/*', function(req, res, next) {
  userIsAllowed(function(allowed) {
    if (allowed) {
      next(); // call the next handler, which in this case is express.static
    } else {
      res.end('You are not allowed!');
    }
  });
});

http.createServer(app).listen(3000, function(){
  console.log('Express server listening on port 3000');
});

结果:

http://localhost:3000/regular/file.txt # regular file
http://localhost:3000/protected/file.txt # You are not allowed!

这种方法的问题在于,请求必须通过应用程序的路由器才能提供静态文件,这不是很有效,但可能满足您的需求(您需要进行一些测量并自己找出答案)。


另一种选择是在中间件链中插入一个小功能,其功能基本相同,但不需要运行整个应用路由器:

var express = require('express');
var http = require('http');
var path = require('path');

function userIsAllowed(callback) {
  // this function would contain your logic, presumably asynchronous,
  // about whether or not the user is allowed to see files in the
  // protected directory; here, we'll use a default value of "false"
  callback(false);
};

// This function returns a middleware function
var protectPath = function(regex) {
  return function(req, res, next) {
    if (!regex.test(req.url)) { return next(); }

    userIsAllowed(function(allowed) {
      if (allowed) {
        next(); // send the request to the next handler, which is express.static
      } else {
        res.end('You are not allowed!');
      }
    });
  };
};

var app = express();

app.use(protectPath(/^\/protected\/.*$/));
app.use(express.static(path.join(__dirname, 'public')));

app.get('/', function(req, res, next) {
  res.end('Home page');
});

http.createServer(app).listen(3000, function(){
  console.log('Express server listening on port 3000');
});

这执行基本相同的逻辑,但不是通过整个应用路由器路由每个请求,而是在每个请求的开头运行一个小函数,检查请求的 URL 是否匹配您传入的正则表达式。如果是,它会运行检查以查看用户是否可以访问该文件。

结果:

http://localhost:3000/regular/file.txt # regular file
http://localhost:3000/protected/file.txt # You are not allowed!

【讨论】:

  • 它可以工作...但是如何申请只允许下载而不是修改或上传目录...
  • 我不确定你的意思;用户如何能够首先修改目录?
  • 您的意思是,如何仅将保护应用于 GET 请求而不应用于 POST 或 PUT?你可以检查req.method == 'GET'(你可能还想保护'HEAD'请求)。
  • 我的错……这就是答案 tnx
  • 谢谢。花了一个上午看这些东西,现在因为这篇文章而把它设置得很好。
猜你喜欢
  • 1970-01-01
  • 2021-12-17
  • 2014-09-03
  • 2022-08-11
  • 2014-02-15
  • 1970-01-01
  • 2019-02-16
  • 1970-01-01
  • 2011-06-07
相关资源
最近更新 更多