我已经将我的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: [] }而且,政策似乎不能适用于视图,但只适用于行动。
发布于 2016-06-04 19:25:59
原因
我认为您的问题来自于这个页面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团队是有意义的,所以他们删除了错误的样本。
相关链接
发布于 2016-06-04 09:19:06
我这样做的方式是使用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();
}
}
};发布于 2022-05-06 07:27:05
包http-auth的作者将函数连接到另一个名为http-auth-connect的包上,实现基本的auth,就像这样对我有用。
然而,我面临一个问题,如何不硬编码用户名和密码,并以环境配置的形式使用它。
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)],
...
}https://stackoverflow.com/questions/37615822
复制相似问题