利用目錄服務器實現單點登陸

LDAP

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

LDAP 是輕量級目錄訪問協議的簡稱(Lightweight Directory Access Protocol).用於訪問目錄服務。它是 X.500 目錄訪問協議的移植,可是簡化了實現方法。html

含有目錄數據庫,提供給用戶查詢、使用信息的計算機就是目錄服務器。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/ldapjsjava

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

 

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

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:github

 

利用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);
    });

 

源碼:npm

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...")
相關文章
相關標籤/搜索