Javaオペレーションは、リレーショナルデータベースサーバとして動作し、プロジェクトのディレクトリサーバをMavenの

Directory Serverは何ですか?

Directoryサーバーが設計された特殊な検索やブラウズデータベースであり、それはまた、単純な挿入、削除、変更機能をサポートしています。そのため、彼は(次の図のように)木である、あなたは私たちの伝統的なリレーショナルデータベースの使用としてそれを理解することができますが、彼はLinuxファイルシステムのディレクトリに似て私たちのリレーショナル・データベース・ストレージ構造とは本質的に異なるものです閲覧と検索のために設計されて、それがトランザクションロールバックと複雑な挿入、更新機能をサポートしていない、逆スローを挿入し、非常に高速なクエリです。ディレクトリサーバは、スタンドアロンまたはクラスタ化されたスタイルをすることができ、データサービスを、提供するために、外部のリレーショナル・データベースと同じにすることができます。クラスタ化されたアーキテクチャでは、各マシンは、一貫性のあるデータのバックアップを持っています。ツリーの各ノードはエントリと呼ばれる、ディレクトリサービスは、マップのツリー構造に基づいてエントリ(エントリ)であり、各エントリは固有の名前、絶対ディレクトリ名(DN)と相対的な名前を持っていますRDN。各エントリは属性のセットを持って、各属性は、キー、一つ以上の値に対応する各キーがあります。各エントリはObjectClassのを与えることによって、そのプロパティを指定しておく必要があり、各エントリのObjectClass制約の特定の属性は、どのようなものです。

例えば図にエントリをBABS:

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

 

LDAPとは何ですか?

LDAPは、ライトウェイトディレクトリアクセスプロトコル(ライトウェイトディレクトリアクセスプロトコル)クライアントとサーバのLDAPディレクトリプロトコルの相互作用が発生し、次たとえば、ノード、ノードクエリのプロパティを追加し、ようにと呼ばれます。

一般的なLDAPディレクトリサーバ

リレーショナルデータベースと同様、LDAPディレクトリ・サーバーは、製品の多くの特定の実装を含む概念であり、リレーショナルデータベースなどの共通のMySQL、Oracle、および上のようです。OpenLDAPの、アクティブディレクトリ(マイクロソフト):LDAPディレクトリ・サーバはまた、などの一般的な具体的な実装を持っています。本論文では、OpenLDAPの例では、Java LDAPサーバを操作する方法を紹介します。

LDAPディレクトリサーバを使用する場合は?

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

使用目录服务器最常见的情况就是多系统间的集中用户管理,比如公司会使用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

おすすめ

転載: blog.csdn.net/as4589sd/article/details/104032720