簡體   English   中英

如何在 Sails 中使用 http-auth

[英]How to use http-auth with Sails

我已將我的 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: [] }

看起來政策也不適用於視圖,但僅適用於操作 hm...

原因

我認為您的問題來自此頁面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團隊很有意義,因此他們刪除了錯誤的示例。

相關鏈接

我這樣做的方法是使用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();
        }

    }
};

package http-auth的作者將 function 連接移動到另一個名為http-auth-connect的 package,實現像這樣的基本身份驗證對我有用。

但是,我面臨一個問題,如何不對用戶名和密碼進行硬編碼,並以環境配置的形式使用它。

var httpAuth = require('http-auth');
var authConnect = require('http-auth-connect');

var basic = httpAuth.basic({
  realm: 'admin area'
}, function (username, password, onwards) {
  return onwards(username === "username" && password === "password");
});
module.export.policies = {

    ...

    'controllerpath/': [authConnect(basic)],

    ...

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM