繁体   English   中英

如何在猫鼬节点js中对mongodb连接进行身份验证

[英]How to authenticate mongodb connection in mongoose node js

我已经使用创建了管理员用户

use admin
db.createUser(
   {
       user: "username",
       pwd: "password",
       roles: [ ]
   }
)

如何映射这些凭据mongoose.connect有关MongoDB的标准连接字符串的格式

或其他任何方式,以便我可以验证连接。 如果usernamepassword无效/未提供,则连接将失败。

您可以直接在连接网址中使用一对登录名/密码,例如

mongoose.connect('mongodb://username:password@host:port');

在这种情况下,如果credentaisl无效,则连接时会出错。

希望对您有所帮助。

var Common = {

// Node Module dependencies 
express : require('express'),

bodyParser : require('body-parser'),

autoIncrement : require('mongoose-auto-increment'),

mongoose : require('mongoose'),

Schema:require('mongoose').Schema,

conn:require('mongoose').createConnection('mongodb://localhost/XYZ'),

// check if the user is authenticated and can access resources 
ensureAuthenticated : function ensureAuthenticated(req, res, next) {
    if (req.isAuthenticated()) {
        return next();
    }
    res.redirect('/');
}

};

我创建了一个称为common.js的引用类。 如果您从其他类(例如common.ensureAuthenticated(username,password))引用此连接类,它将起作用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM