Java operations maven project directory server, operating as a relational database server

What is the Directory Server?

Directory server is a specialized search and browse database designed, it also supports simple insert, delete, modify function. You can understand it as our traditional relational database use, but he is essentially different with our relational database storage structure similar to the linux file system directory, he is a tree (similar to the figure below), because it is designed for browsing and searching, it's very fast queries, insert the contrary slow, it does not support the transaction and roll back and complex insert, update function. Directory server can be the same as the external relational database to provide data services, which can be stand-alone or clustered style. In clustered architecture each machine has a consistent data backup. Each node in the tree is called an entry, directory services are Entries (Entry) based in the tree structure on the map, each entry has a unique name absolute Directory Name (DN) and relative name rDN. Each entry has a set of attributes, each attribute has a key, each key corresponding to one or more value. What are the specific attributes of each entry ObjectClass constraints, that is, each entry must have to specify its properties by giving ObjectClass.

E.g. babs entries on figure:

DN:uid=babs,ou=people,dc=example,dc=com 
相对于ou=people,dc=example,dc=com 节点的rDN:uid=babs
ObjectClass:Person

 

What is LDAP?

LDAP called the Lightweight Directory Access Protocol (Lightweight Directory Access Protocol) client and server follow the LDAP directory protocol interaction occurs, for example, add a node, a node query property and so on.

Common LDAP directory server

As with relational databases, LDAP directory server is a concept that contains many specific implementation of products, such as relational databases are common MySQL, Oracle, and so on. LDAP directory server also has common specific implementation, such as: OpenLDAP, Active Directory (Microsoft). In this paper, OpenLDAP example to introduce how to operate Java LDAP server.

When using an LDAP directory server?

看到目前为止,你可能会觉得目录服务器貌似和关系型数据库服务器没有什么区别,到底什么时候该使用目录服务器呢?

使用目录服务器最常见的情况就是多系统间的集中用户管理,比如公司会使用OA,Confluence,gitlab,jira等等。如果每个系统都需要我们记住一个账号密码,那无疑是很费力的。通过使用LDAP目录服务器将多个应用的用户集中管理起来,每个应用都通过通用的协议与目录服务器通信。

在使用Java语言前,需要引入jldap的Maven依赖

<dependency>
 <groupId>com.novell.ldap</groupId>
 <artifactId>jldap</artifactId>
 <version>4.3</version>
 <type>jar</type>
 <scope>compile</scope>
</dependency>

具体代码示例如下

package XXXXX.com;

import com.novell.ldap.*;
import com.novell.ldap.util.Base64;
import lombok.extern.slf4j.Slf4j;

import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


/**
* 参考地址:http://www.micmiu.com/opensource/java-ldap-demo/
*/
@Slf4j
public class App {

private static String ldapHost = "192.158.6.64";
private static int ldapPort = 389;
private static String ldapBindDN = "cn=Manager,dc=my-domain,dc=com";
private static String ldapPassword = "123456";
private static int ldapVersion = LDAPConnection.LDAP_V3;

 /**
* 获取连接
 *
* @return A LDAP Connection
* @throws LDAPException
* @throws UnsupportedEncodingException
*/
 @SuppressWarnings("deprecation")
private static LDAPConnection connection() throws UnsupportedEncodingException, LDAPException {
try {
LDAPConnection lc = new LDAPConnection();
 //获取连接
 lc.connect(ldapHost, ldapPort);
 //认证
 lc.bind(ldapVersion, ldapBindDN, ldapPassword.getBytes("UTF8"));
 log.info("连接LDAP服务器成功!");
return lc;
 } catch (Exception e) {
log.debug("LDAP服务器连接失败!");
throw e;

 }
}

/**
* 搜索某目录节点下所有节点及其属性
 * @param DN 目录节点名
 * @throws LDAPException
* @throws UnsupportedEncodingException
*/
 public static void searchEntry(String DN) throws LDAPException, UnsupportedEncodingException {
LDAPConnection conn = connection();
try {
LDAPSearchResults searchResults = conn.search(DN,
 LDAPConnection.SCOPE_SUB, "objectClass=*", null, false);
while (searchResults.hasMore()) {
LDAPEntry nextEntry;
try {
nextEntry = searchResults.next();
 } catch (LDAPException e) {
log.debug("Error: " + e);
if (e.getResultCode() == LDAPException.LDAP_TIMEOUT
 || e.getResultCode() == LDAPException.CONNECT_ERROR) {
break;
 } else {
continue;
 }
}
log.info("DN :" + nextEntry.getDN());
 log.info("|---- Attributes list: ");
 LDAPAttributeSet attributeSet = nextEntry.getAttributeSet();
 Iterator<LDAPAttribute> allAttributes = attributeSet.iterator();
while (allAttributes.hasNext()) {
LDAPAttribute attribute = allAttributes.next();
 String attributeName = attribute.getName();

 Enumeration<String> allValues = attribute.getStringValues();
if (null == allValues) {
continue;
 }
while (allValues.hasMoreElements()) {
String value = allValues.nextElement();
if (!Base64.isLDIFSafe(value)) {
// base64 encode and then print out
 value = Base64.encode(value.getBytes());
 }
log.info("|---- ---- " + attributeName
+ " = " + value);
 }
}
}
} finally {
if (conn.isConnected()) {
conn.disconnect();
 }
}
}

/**
* 新增一个目录节点
 * @param baseDN
 * @param rDN
 * @param attribute
 * @throws UnsupportedEncodingException
* @throws LDAPException
*/
 public static void addEntry(String baseDN, String rDN, Map<String, String> attribute) throws UnsupportedEncodingException, LDAPException {
LDAPConnection conn = connection();
try {
LDAPAttributeSet attributeSet = new LDAPAttributeSet();
for (Map.Entry<String, String> entry : attribute.entrySet()) {
attributeSet.add(new LDAPAttribute(entry.getKey(), entry.getValue()));
 }
String DN = rDN + "," + baseDN;
 conn.add(new LDAPEntry(DN, attributeSet));
 } finally {
if (conn.isConnected())
conn.disconnect();
 }
}

/**
* 删除一个目录节点
 * @param DN 目录节点名
 * @throws UnsupportedEncodingException
* @throws LDAPException
*/
 public static void deleteEntry(String DN) throws UnsupportedEncodingException, LDAPException {
LDAPConnection conn = connection();
try {
conn.delete(DN);
 } finally {
if (conn.isConnected()) {
conn.disconnect();
 }
}

}

public static void main(String[] args) throws UnsupportedEncodingException, LDAPException {
//新增节点
 Map<String, String> map = new HashMap<>();
 map.put("objectclass", "inetOrgPerson");
 map.put("cn", "liuruojing");
 map.put("sn", "liuruojing");
 addEntry("ou=userAccount,dc=my-domain,dc=com", "uid=liuruojing", map);

 //查询节点
 searchEntry("uid=liuruojing,ou=userAccount,dc=my-domain,dc=com");

 //删除节点
 deleteEntry("uid=liuruojing,ou=userAccount,dc=my-domain,dc=com");

 }


}

 

发布了30 篇原创文章 · 获赞 3 · 访问量 2318

Guess you like

Origin blog.csdn.net/as4589sd/article/details/104032720