【问题标题】:How to use http-auth with Sails如何在 Sails 中使用 http-auth
【发布时间】:2016-06-03 13:33:41
【问题描述】:

我已将我的 Sails 应用程序部署到 PaaS,我想要简单的密码保护,这样任何人都无法访问我的登台服务器。

最简单的方法是什么?

看起来像 http-auth,文档解释了如何为 ExpressJS 实现,但使用 SailsJS 我找不到 app.use()

我尝试过的

在我的policies.js 文件中

module.exports.policies = {

  // '*': true,
    '*': require('http-auth').basic({
      realm: 'admin area'
    }, function customAuthMethod (username, password, onwards) {
      return onwards(username === "Tina" && password === "Bullock");
    }),

导致

info: Starting app...

error: Cannot map invalid policy:  { realm: 'admin area',
  msg401: '401 Unauthorized',
  msg407: '407 Proxy authentication required',
  contentType: 'text/plain',
  users: [] }

而且看起来策略不能应用于视图,但只能应用于操作......

【问题讨论】:

  • 如果你真的有兴趣使用http-auth,请看我的回答。显然 SailsJS 文档不正确并导致了您的问题。
  • 我只是 created pull request,所以 Sails 团队更新了他们的文档。

标签: node.js express sails.js http-auth


【解决方案1】:

原因

我认为您的问题来自此页面 http://sailsjs.org/documentation/concepts/middleware,它为 http-auth 模块使用了不正确的模式。

解决方案

SailsJS 使用connect/express 样式的中间件,所以您唯一需要做的就是为其提供适当的中间件。

// Authentication module.
var auth = require('http-auth');
var basic = auth.basic({
        realm: "Simon Area."
    }, function (username, password, callback) { // Custom authentication.
        callback(username === "Tina" && password === "Bullock");
    }
});

// Use proper middleware.
module.exports.policies = {
    '*': auth.connect(basic)
    ...

待办事项

通知 SailsJS 团队是有意义的,因此他们删除了错误的样本。

相关链接

【讨论】:

    【解决方案2】:

    我这样做的方式是使用config/http.js 文件。在那里创建自定义中间件...

    这是我的http.js 文件:

    var basicAuth = require('basic-auth'),
        auth = function (req, res, next) {
            var user = basicAuth(req);
            if (user && user.name === "username" && user.pass === "password") return next();
            res.set('WWW-Authenticate', 'Basic realm=Authorization Required');
            return res.send(401);
        };
    
    module.exports.http = {
    
        customMiddleware: function (app) {
            app.use('/protected', auth);
        },
    
        middleware: {
    
            order: [
                'startRequestTimer',
                'cookieParser',
                'session',
                // 'requestLogger',
                'bodyParser',
                'handleBodyParserError',
                'compress',
                'methodOverride',
                'poweredBy',
                '$custom',
                'router',
                'www',
                'favicon',
                '404',
                '500'
            ],
    
            requestLogger: function (req, res, next) {
                console.log("Requested :: ", req.method, req.url);
                console.log('=====================================');
                return next();
            }
    
        }
    };
    

    【讨论】:

    • 刚刚意识到您正在使用 http-auth,而我的示例显示 basic-auth...但是逻辑是相同的,它也将以相同的方式工作 http-auth...跨度>
    • 成功了!感谢你的分享。 /protected 是您放置要保护的页面的文件夹吗?如果您觉得这个问题很有趣,感谢您投票。
    • /protected 是一个将使用该中间件的路由...您可以将任何内容放入该路由...这只是一个示例...在实际用例中,我提供了静态文件(文档对于由 apidoc 生成的 api) 在该路由上,然后还将此身份验证添加到它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多