利用目录服务器实现单点登录

LDAP

https://www.jianshu.com/p/50a214f51dd1

LDAP 是轻量级目录访问协议的简称(Lightweight Directory Access Protocol).用于访问目录服务。它是 X.500 目录访问协议的移植,但是简化了实现方法。

含有目录数据库,提供给用户查询、使用信息的计算机就是目录服务器。X.500是一套目录服务标准,定义了一个机构在全局范围内共享名称和与名称相关联的数据。X.500采用层次结构,其中的管理域可以提供这些域内的用户和资源信息,并定义了强大的搜索功能,因此查询变得更简单。由于X.500目录服务协议过于复杂,因此开发了LDAP(轻量级的目录访问协议)。
LDAP目录存储和组织的基本数据结构称为条目,每个条目都有唯一的标识符,并有一些属性,就类似数据库中每一行代表一条数据,并且有一项是唯一标识。但是LDAP比数据库简单很多,并且常用于查询(也就是读取),写操作不常使用。
 
上图是一个典型的目录结构
  第一个节点 DN 命名为:dn:dc=example,dc=com
  第二个节点 DN 命名为:dn:ou=People,dc=example,dc=com
  第三个节点 DN 命名为:dn:uid=bjensen,ou=people,dc=example,dc=com

ldap.js库

https://www.npmjs.com/package/ldapjs

LDAPjs makes the LDAP protocol a first class citizen in Node.js.

For full docs, head on over to http://ldapjs.org.

var ldap require('ldapjs');
 
var server ldap.createServer();
 
server.search('dc=example'function(reqresnext{
  var obj {
    dnreq.dn.toString(),
    attributes{
      objectclass['organization''top'],
      o'example'
    }
  };
 
  if (req.filter.matches(obj.attributes))
  res.send(obj);
 
  res.end();
});
 
server.listen(1389function({
  console.log('ldapjs listening at server.url);
});

To run that, assuming you've got the OpenLDAP client on your system:

利用LDAP实现登录

https://www.ibm.com/developerworks/cn/opensource/se-use-ldap-authentication-authorization-node.js-bluemix-application/index.html

  1. 使用用户的 DN,您可尝试使用该用户密码绑定到服务器。
    1
    2
    3
    4
    5
    // When you have the DN, try to bind with it to check the password
    var userClient = ldap.createClient({
         url: sessionData.ldap.url
    });
    userClient.bind(sessionData.dn, sessionData.passwd, function(err) {
  2. 如果绑定成功,则意味着用户信息是正确的,您可以开始新会话。如果绑定失败,则密码是错误的(该 uid 已被用,否则该流程将在子步骤 4 中失败)。
    1
    2
    3
    4
    5
    6
    7
    8
    if (err == null) {
         var sessionID = logon(sessionData);
     
         res.setHeader("Set-Cookie", ["sessionID=" + sessionID]);
         res.redirect("main.html");
    } else
         res.send("You are not " + sessionData.uid);
    });

源码:

https://github.com/qbzzt/bluemix/tree/master/security/201802/login-using-ldap

app.post("/ad", (req, res) => {
    var client = ldap.createClient({
          url: req.body.serverUrl
    });
    
    client.bind(req.body.username + '@' + req.body.domain, req.body.password, function(err) {
        if (err) {
            res.send("Bind failed " + err);
            return;
        }
        
        res.send("Log on successful");        

    }); // client.bind
    
}); // app.post("/ad...")

猜你喜欢

转载自www.cnblogs.com/lightsong/p/10416076.html