Currently I am trying to use the middleware "passport" in an object oriented Node.js server. After restarting the server everything works fine. I can access the route where no authentication is required. But when I am trying to access a route with authentication I always get the response 401 (No-Authentication). This seems to be ok but unfortunately I do not enter the function(username, password, done) in the LocalStrategy.
目前,我正在尝试在面向对象的节点中使用中间件“passport”。js服务器。重启服务器后一切正常。我可以访问不需要身份验证的路由。但是当我尝试使用身份验证访问路由时,我总是得到响应401(无身份验证)。这似乎没问题,但不幸的是,我没有在LocalStrategy中输入函数(用户名、密码、done)。
So I think my problem is in the way I try to use a middleware in the oo javascript style. The template I used to start is from the RedHat OpenShift Cloud and can be seen in: https://github.com/openshift/nodejs-custom-version-openshift/blob/master/server.js
所以我认为我的问题在于我尝试使用oo javascript风格的中间件。我用来启动的模板来自RedHat OpenShift云,可以在:https://github.com/openshift/nodejs-custom-version-openshift/blob/master/server.js中看到
Here is my server initialization method where I tried to use the passport middleware:
下面是我尝试使用passport中间件的服务器初始化方法:
self.initializeServer = function() {
self.createGetRoutes();
self.createPostRoutes();
self.app = express.create();
passport.use(new LocalStrategy(
function(username, password, done) {
console.log("TEST");
process.nextTick(function () {
console.log('Here I am!');
return done(null, user);
});
}
));
self.app.configure(function() {
self.app.use(passport.initialize());
});
// Paths without authentication
self.app.get('/holy', function(req, res) {res.send('SHIT! \n')});
// Add GET handlers for the app with authentication (from the getRoutes).
for (var g in self.getRoutes) {
self.app.get(g, passport.authenticate('local', { session: false }), self.getRoutes[g]);
}
// Add POST handlers for the app with authentication (from the postRoutes).
for (var p in self.postRoutes) {
self.app.post(p, passport.authenticate('local', { session: false }), self.postRoutes[p]);
}
};
3 个解决方案
#1
3
Your problem here is that you don't use the right strategy. You should use the BasicStrategy to use the http authentication: Passport-HTTP
你的问题是你没有使用正确的策略。您应该使用BasicStrategy来使用http身份验证:Passport-HTTP
#2
0
It looks to me that you are not using the express.router()
middleware.
在我看来,您没有使用express.router()中间件。
#3
0
You can also use http-auth for HTTP Basic/Digest authentication:
您还可以使用HTTP -auth进行HTTP基本/摘要身份验证:
// Authentication module.
var auth = require('http-auth');
var basic = auth.basic({
realm: "Simon Area.",
file: __dirname + "/../data/users.htpasswd" // gevorg:gpass, Sarah:testpass ...
});
// Creating new HTTP server.
http.createServer(basic, function(req, res) {
res.end("Welcome to private area - " + req.user + "!");
}).listen(1337);
or you can use it with passport:
或者你可以用护照:
// Authentication module.
var auth = require('http-auth');
var basic = auth.basic({
realm: "Simon Area.",
file: __dirname + "/../data/users.htpasswd" // gevorg:gpass, Sarah:testpass ...
});
// Application setup.
var app = express();
// Setup strategy.
var passport = require('passport');
passport.use(auth.passport(basic));
// Setup route.
app.get('/', passport.authenticate('http', { session: false }), function(req, res) {
res.end("Welcome to private area - " + req.user + "!");
});
#1
3
Your problem here is that you don't use the right strategy. You should use the BasicStrategy to use the http authentication: Passport-HTTP
你的问题是你没有使用正确的策略。您应该使用BasicStrategy来使用http身份验证:Passport-HTTP
#2
0
It looks to me that you are not using the express.router()
middleware.
在我看来,您没有使用express.router()中间件。
#3
0
You can also use http-auth for HTTP Basic/Digest authentication:
您还可以使用HTTP -auth进行HTTP基本/摘要身份验证:
// Authentication module.
var auth = require('http-auth');
var basic = auth.basic({
realm: "Simon Area.",
file: __dirname + "/../data/users.htpasswd" // gevorg:gpass, Sarah:testpass ...
});
// Creating new HTTP server.
http.createServer(basic, function(req, res) {
res.end("Welcome to private area - " + req.user + "!");
}).listen(1337);
or you can use it with passport:
或者你可以用护照:
// Authentication module.
var auth = require('http-auth');
var basic = auth.basic({
realm: "Simon Area.",
file: __dirname + "/../data/users.htpasswd" // gevorg:gpass, Sarah:testpass ...
});
// Application setup.
var app = express();
// Setup strategy.
var passport = require('passport');
passport.use(auth.passport(basic));
// Setup route.
app.get('/', passport.authenticate('http', { session: false }), function(req, res) {
res.end("Welcome to private area - " + req.user + "!");
});