目录:
- 概述
- 基本操作
- 查询
- 添加
- 删除
- 修改属性
- 验证密码
[一]、概述
jldap 官网:http://www.openldap.org/jldap/
可以从官网下载源编译生成jar包,如果项目是用maven构建的,在pom.xml中增加如下内容即可:
1
2
3
4
5
6
7
|
<dependency>
<groupId>com.novell.ldap</groupId>
<artifactId>jldap</artifactId>
<version>4.3</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
|
[二]、基本操作
为了演示基本的操作,需要搭建个LDAP服务,有关openLDAP在windows上的安装配置可参见:http://www.micmiu.com/enterprise-app/sso/openldap-windows-config/ ,我配置好演示用的LDAP基本信息可见客户端截图:
1.查询
java代码:LDAPSearchDemo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
packagecom.micmiu.ldap;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import java.util.Iterator;
import com.novell.ldap.LDAPAttribute;
import com.novell.ldap.LDAPAttributeSet;
import com.novell.ldap.LDAPConnection;
import com.novell.ldap.LDAPEntry;
import com.novell.ldap.LDAPException;
import com.novell.ldap.LDAPSearchResults;
import com.novell.ldap.util.Base64;
/**
* 查询条目示例 blog http://www.micmiu.com
*
* @author Michael
*
*/
publicclassLDAPSearchDemo{
/**
*
* @param args
*/
publicstaticvoidmain(String[]args){
StringldapHost="localhost";
StringloginDN="cn=Manager,dc=micmiu,dc=com";
Stringpassword="secret";
StringsearchBase="dc=micmiu,dc=com";
StringsearchFilter="objectClass=*";
intldapPort=LDAPConnection.DEFAULT_PORT;
// 查询范围
// SCOPE_BASE、SCOPE_ONE、SCOPE_SUB、SCOPE_SUBORDINATESUBTREE
intsearchScope=LDAPConnection.SCOPE_SUB;
LDAPConnection lc=newLDAPConnection();
try{
lc.connect(ldapHost,ldapPort);
lc.bind(LDAPConnection.LDAP_V3,loginDN,password.getBytes("UTF8"));
LDAPSearchResults searchResults=lc.search(searchBase,
searchScope,searchFilter,null,false);
while(searchResults.hasMore()){
LDAPEntry nextEntry=null;
try{
nextEntry=searchResults.next();
}catch(LDAPExceptione){
System.out.println("Error: "+e.toString());
if(e.getResultCode()==LDAPException.LDAP_TIMEOUT
||e.getResultCode()==LDAPException.CONNECT_ERROR){
break;
}else{
continue;
}
}
System.out.println("DN =: "+nextEntry.getDN());
System.out.println("|---- Attributes list: ");
LDAPAttributeSet attributeSet=nextEntry.getAttributeSet();
Iterator<LDAPAttribute>allAttributes=attributeSet.iterator();
while(allAttributes.hasNext()){
LDAPAttribute attribute=allAttributes.next();
StringattributeName=attribute.getName();
Enumeration<String>allValues=attribute.getStringValues();
if(null==allValues){
continue;
}
while(allValues.hasMoreElements()){
Stringvalue=allValues.nextElement();
if(!Base64.isLDIFSafe(value)){
// base64 encode and then print out
value=Base64.encode(value.getBytes());
}
System.out.println("|---- ---- "+attributeName
+" = "+value);
}
}
}
}catch(LDAPExceptione){
System.out.println("Error: "+e.toString());
}catch(UnsupportedEncodingExceptione){
System.out.println("Error: "+e.toString());
}finally{
try{
if(lc.isConnected()){
lc.disconnect();
}
}catch(Exceptione){
e.printStackTrace();
}
}
}
}
|
运行结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
DN=:dc=micmiu,dc=com
|----Attributes list:
|--------dc=micmiu
|--------o=Michael Blog
|--------objectClass=domain
|--------objectClass=top
DN=:ou=Developer,dc=micmiu,dc=com
|----Attributes list:
|--------description=Container fordeveloper entries
|--------ou=Developer
|--------objectClass=organizationalUnit
DN=:ou=Tester,dc=micmiu,dc=com
|----Attributes list:
|--------description=Container fortest entries
|--------ou=Tester
|--------objectClass=organizationalUnit
DN=:uid=Michael,ou=Developer,dc=micmiu,dc=com
|----Attributes list:
|--------userPassword=111111
|--------labeledURI=http://www.micmiu.com
|--------uid=Michael
|--------sn=Sun
|--------cn=Michael Sun
|--------mail=sjsky007@gmail.com
|--------objectClass=inetOrgPerson
DN=:uid=Miumiu,ou=Tester,dc=micmiu,dc=com
|----Attributes list:
|--------userPassword=111111
|--------labeledURI=http://www.micmiu.com
|--------uid=Miumiu
|--------sn=Wu
|--------cn=Miumiu Wu
|--------objectClass=inetOrgPerson
DN=:dc=app1,dc=micmiu,dc=com
|----Attributes list:
|--------dc=app1
|--------o=Michael Demo
|--------objectClass=domain
DN=:dc=app2,dc=micmiu,dc=com
|----Attributes list:
|--------dc=app2
|--------o=Michael Demo
|--------objectClass=domain
DN=:ou=Demo,dc=app1,dc=micmiu,dc=com
|----Attributes list:
|--------description=Container forDemo entries
|--------ou=Developer
|--------ou=Demo
|--------objectClass=organizationalUnit
DN=:ou=Demo,dc=app2,dc=micmiu,dc=com
|----Attributes list:
|--------description=Container forDemo entries
|--------ou=Developer
|--------ou=Demo
|--------objectClass=organizationalUnit
DN=:uid=michael,ou=Demo,dc=app1,dc=micmiu,dc=com
|----Attributes list:
|--------userPassword=111111
|--------labeledURI=http://www.micmiu.com
|--------uid=michael
|--------sn=Sun
|--------cn=Michael Sun
|--------mail=sjsky007@gmail.com
|--------objectClass=inetOrgPerson
DN=:uid=hazel,ou=Demo,dc=app1,dc=micmiu,dc=com
|----Attributes list:
|--------userPassword=111111
|--------labeledURI=http://www.micmiu.com
|--------uid=hazel
|--------sn=Wu
|--------cn=Hazel Wu
|--------objectClass=inetOrgPerson
DN=:uid=michael,ou=Demo,dc=app2,dc=micmiu,dc=com
|----Attributes list:
|--------userPassword=111111
|--------labeledURI=http://www.micmiu.com
|--------uid=michael
|--------sn=Sun
|--------cn=Michael Sun
|--------mail=sjsky007@gmail.com
|--------objectClass=inetOrgPerson
DN=:uid=hazel,ou=Demo,dc=app2,dc=micmiu,dc=com
|----Attributes list:
|--------userPassword=111111
|--------labeledURI=http://www.micmiu.com
|--------uid=hazel
|--------sn=Wu
|--------cn=Hazel Wu
|--------objectClass=inetOrgPerson
|
查询结果和客户端查询出的信息一致。
2.添加
java代码:LDAPAddEntry.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
packagecom.micmiu.ldap;
import java.io.UnsupportedEncodingException;
import com.novell.ldap.LDAPAttribute;
import com.novell.ldap.LDAPAttributeSet;
import com.novell.ldap.LDAPConnection;
import com.novell.ldap.LDAPEntry;
import com.novell.ldap.LDAPException;
/**
* 添加新条目的示例
* blog http://www.micmiu.com
*
* @author Michael
*
*/
publicclassLDAPAddEntry{
/**
*
* @param args
*/
publicstaticvoidmain(String[]args){
StringldapHost="localhost";
StringloginDN="cn=Manager,dc=micmiu,dc=com";
Stringpassword="secret";
StringcontainerName="dc=micmiu,dc=com";
intldapPort=LDAPConnection.DEFAULT_PORT;
intldapVersion=LDAPConnection.LDAP_V3;
LDAPConnection lc=newLDAPConnection();
LDAPAttributeSet attributeSet=newLDAPAttributeSet();
attributeSet.add(newLDAPAttribute("objectclass",newString(
"inetOrgPerson")));
attributeSet.add(newLDAPAttribute("cn","Wukong Sun"));
attributeSet.add(newLDAPAttribute("sn","Sun"));
attributeSet.add(newLDAPAttribute("mail","sjsky007@gmail.com"));
attributeSet.add(newLDAPAttribute("labeledURI",
"http://www.micmiu.com"));
attributeSet.add(newLDAPAttribute("userPassword","111111"));
attributeSet.add(newLDAPAttribute("uid","addnew"));
Stringdn="uid=addnew,ou=Developer,"+containerName;
LDAPEntry newEntry=newLDAPEntry(dn,attributeSet);
try{
lc.connect(ldapHost,ldapPort);
lc.bind(ldapVersion,loginDN,password.getBytes("UTF8"));
System.out.println("login ldap server successfully.");
lc.add(newEntry);
System.out.println("Added object: "+dn+" successfully.");
}catch(LDAPExceptione){
e.printStackTrace();
}catch(UnsupportedEncodingExceptione){
System.out.println("Error: "+e.toString());
}finally{
try{
if(lc.isConnected()){
lc.disconnect();
}
}catch(Exceptione){
e.printStackTrace();
}
}
}
}
|
运行结果:
1
2
|
login ldap server successfully.
Added object:uid=addnew,ou=Developer,dc=micmiu,dc=com successfully.
|
客户端刷新后的截图:
3.删除
java代码:LDAPDeleteEntry.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
packagecom.micmiu.ldap;
import java.io.UnsupportedEncodingException;
import com.novell.ldap.LDAPConnection;
import com.novell.ldap.LDAPException;
/**
* 删除条目的示例
* blog http://www.micmiu.com
*
* @author Michael
*
*/
publicclassLDAPDeleteEntry{
/**
* @param args
*/
publicstaticvoidmain(String[]args){
StringldapHost="localhost";
StringloginDN="cn=Manager,dc=micmiu,dc=com";
Stringpassword="secret";
StringdeleteDN="uid=addnew,ou=Developer,dc=micmiu,dc=com";
intldapPort=LDAPConnection.DEFAULT_PORT;
intldapVersion=LDAPConnection.LDAP_V3;
LDAPConnection lc=newLDAPConnection();
try{
lc.connect(ldapHost,ldapPort);
lc.bind(ldapVersion,loginDN,password.getBytes("UTF8"));
lc.delete(deleteDN);
System.out.println(" delete Entry: "+deleteDN+" success.");
lc.disconnect();
}catch(LDAPExceptione){
if(e.getResultCode()==LDAPException.NO_SUCH_OBJECT){
System.err.println("Error: No such object");
}elseif(e.getResultCode()==LDAPException.INSUFFICIENT_ACCESS_RIGHTS){
System.err.println("Error: Insufficient rights");
}else{
System.err.println("Error: "+e.toString());
}
}catch(UnsupportedEncodingExceptione){
System.out.println("Error: "+e.toString());
}finally{
try{
if(lc.isConnected()){
lc.disconnect();
}
}catch(Exceptione){
e.printStackTrace();
}
}
}
}
|
运行结果:
1
|
delete Entry:uid=addnew,ou=Developer,dc=micmiu,dc=com success.
|
在刷新客户端后发现刚新增加的条目:addnew 已经被删除了。
4.修改属性
java代码:LDAPAddEntry.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
packagecom.micmiu.ldap;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.novell.ldap.LDAPAttribute;
import com.novell.ldap.LDAPConnection;
import com.novell.ldap.LDAPException;
import com.novell.ldap.LDAPModification;
/**
* 修改操作示例
* blog http://www.micmiu.com
*
* @author Michael
*
*/
publicclassLDAPModifyAttrs{
/**
* @param args
*/
publicstaticvoidmain(String[]args){
StringldapHost="localhost";
StringloginDN="cn=Manager,dc=micmiu,dc=com";
Stringpassword="secret";
StringmodifyDN="uid=Michael,ou=Developer,dc=micmiu,dc=com";
intldapPort=LDAPConnection.DEFAULT_PORT;
intldapVersion=LDAPConnection.LDAP_V3;
LDAPConnection lc=newLDAPConnection();
List<LDAPModification>modList=newArrayList<LDAPModification>();
// Add a new value to the description attribute
Stringdesc="This object was modified at "+newDate();
LDAPAttribute attribute=newLDAPAttribute("description",desc);
modList.add(newLDAPModification(LDAPModification.ADD,attribute));
attribute=newLDAPAttribute("telephoneNumber","180-8888-xxxx");
modList.add(newLDAPModification(LDAPModification.ADD,attribute));
// Replace the labeledURI address with a new value
attribute=newLDAPAttribute("labeledURI","www.micmiu.com");
modList.add(newLDAPModification(LDAPModification.REPLACE,attribute));
// delete the email attribute
attribute=newLDAPAttribute("mail");
modList.add(newLDAPModification(LDAPModification.DELETE,attribute));
LDAPModification[]mods=newLDAPModification[modList.size()];
mods=(LDAPModification[])modList.toArray(mods);
try{
lc.connect(ldapHost,ldapPort);
lc.bind(ldapVersion,loginDN,password.getBytes("UTF8"));
lc.modify(modifyDN,mods);
System.out
.println("LDAPAttribute add、replace、delete all successful.");
}catch(LDAPExceptione){
e.printStackTrace();
}catch(UnsupportedEncodingExceptione){
System.out.println("Error: "+e.toString());
}finally{
try{
if(lc.isConnected()){
lc.disconnect();
}
}catch(Exceptione){
e.printStackTrace();
}
}
}
}
|
修改后客户端查询到的信息截图如下:
5.验证密码
java代码:LDAPVerifyPassword.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
packagecom.micmiu.ldap;
import java.io.UnsupportedEncodingException;
import com.novell.ldap.LDAPAttribute;
import com.novell.ldap.LDAPConnection;
import com.novell.ldap.LDAPException;
/**
* 验证密码的示例
* blog http://www.micmiu.com
*
* @author Michael
*
*/
publicclassLDAPVerifyPassword{
/**
* @param args
*/
publicstaticvoidmain(String[]args){
StringldapHost="localhost";
StringloginDN="cn=Manager,dc=micmiu,dc=com";
Stringpassword="secret";
StringverifyDN="uid=Michael,ou=Developer,dc=micmiu,dc=com";
StringverifyPassword="111111";
intldapPort=LDAPConnection.DEFAULT_PORT;
intldapVersion=LDAPConnection.LDAP_V3;
LDAPConnection lc=newLDAPConnection();
try{
lc.connect(ldapHost,ldapPort);
lc.bind(ldapVersion,loginDN,password.getBytes("UTF8"));
LDAPAttribute attr=newLDAPAttribute("userPassword",
verifyPassword);
booleancorrect=lc.compare(verifyDN,attr);
System.out.println(correct?"The password is correct.^_^"
:"The password is incorrect.!!!");
}catch(LDAPExceptione){
e.printStackTrace();
if(e.getResultCode()==LDAPException.NO_SUCH_OBJECT){
System.err.println("Error: No such entry");
}elseif(e.getResultCode()==LDAPException.NO_SUCH_ATTRIBUTE){
System.err.println("Error: No such attribute");
}else{
System.err.println("Error: "+e.toString());
}
}catch(UnsupportedEncodingExceptione){
System.err.println("Error: "+e.toString());
}finally{
try{
if(lc.isConnected()){
lc.disconnect();
}
}catch(Exceptione){
e.printStackTrace();
}
}
}
}
|
运行结果:
1
|
The password iscorrect.^_^
|
验证密码成功。
相关推荐
Java 语言通过 JNDI(Java Naming and Directory Interface)提供了对 LDAP(Lightweight Directory Access Protocol)的支持,允许开发者通过 Java 语言来操作 LDAP 服务器。LDAP 是一种常用的目录服务协议,用于...
在Java中,我们可以使用JNDI(Java Naming and Directory Interface)和特定的LDAP提供者(如Novell的jLDAP)来实现与LDAP服务器的交互。 在标题提及的"novell-jldap-devel-2013.08.30.1433-xplat"压缩包中,包含了...
java操作ldap类java操作ldap类java操作ldap类
Java 语言可以通过 JNI(Java Native Interface,Java 本地接口)或纯 Java 实现来访问和操作 LDAP 服务器。 二、Java 访问 LDAP 服务器 在 Java 中,访问 LDAP 服务器需要使用 javax.naming 和 javax.naming.ldap...
LDAP(Lightweight Directory Access Protocol)是一种目录访问协议,允许客户端访问和操作目录服务。 LDAP 广泛应用于企业目录服务中,如 Active Directory、OpenLDAP 等。 Java 使用 LDAP 修改 AD 域用户密码 在...
JAVA中使用LDAP进行用户认证 JAVA中使用LDAP进行用户认证是指在JAVA应用程序中使用轻量级目录访问协议(LDAP)来进行用户身份验证。LDAP是一种基于X.500标准的目录访问协议,但它更简单、更灵活,可以根据需要进行...
本文将深入探讨如何使用Java通过LDAP(轻量级目录访问协议)和SSL(安全套接层)来实现用户和组织(部门)的增删改查操作,并结合证书确保通信的安全性。这些功能通常用于大型企业的用户管理,例如Active Directory...
本文将详细介绍如何使用Java来实现对LDAP的访问,主要探讨两种方式:使用`LDAPTemplate`和使用`JLDAP`。 #### 二、使用LDAPTemplate访问LDAP ##### 2.1 LDAPTemplate介绍 `LDAPTemplate`是Spring框架中提供的一种...
总的来说,Java通过Ldap操作AD域涉及网络连接、身份验证、目录查询以及可能的SSL安全通信。理解这些概念和API的使用是实现AD域集成的关键。通过以上介绍和示例代码,你应该能够开始编写自己的AD域操作功能。记得在...
Java 连接和验证 LDAP 文档 Java 连接和验证 LDAP 文档是一份关于 Java 语言连接和验证 LDAP 服务器的学习...通过使用 Java 6.0 API for LDAP,可以轻松地连接和访问 LDAP 服务器,实现目录服务中的数据访问和管理。
本案例将介绍如何使用Java编程语言操作LDAP服务器,实现用户登录验证等功能。 首先,我们需要了解Java中的JNDI(Java Naming and Directory Interface)框架,它是Java提供的一套标准API,用于与各种命名和目录服务...
如何将对ldap的操作与对oracle的操作封装在一个事务中? 如何处理分布的database,LDAP事务? 详见项目代码 其中测试文件位置:src\test\com\aua\service\impl\StudentServiceTest.java spring配置文件位置: src...
总结来说,Java操作LDAP涉及到理解目录服务的基本概念,熟悉Java LDAP API或Spring LDAP,以及如何安全地进行连接和数据操作。这些知识对于在企业级应用中实现身份验证、授权和其他目录服务功能至关重要。通过熟练...
在这篇文章中,我们将探讨使用 JAVA 实现 LDAP 的 AD 域免证书查询、修改、删除、新增、启用、禁用和修改密码的操作。 首先,让我们了解什么是 LDAP 和 AD 域。LDAP(Lightweight Directory Access Protocol)是一...
rar包:一个Web工程, 主要有,利用Spring-ldap对LDAP的基本操作(查询,增删改);Extjs实现的对Ldap的树状结构的显示,结构有点类似Softerra LDAP;一个测试类。 pdf:spring-ldap-reference.pdf Extjs.pdf ...
Java LDAP(轻量级目录访问协议)操作是Java开发者在处理目录...在实际开发中,理解这些基本概念和操作是实现Java LDAP应用程序的关键。结合`LDAPTest`代码,你可以更深入地学习如何在Java中有效地与LDAP服务器交互。
- `ldap.jar`:提供对LDAP协议的具体实现。 - `providerutil.jar`:可能需要,根据具体实现,用于处理服务提供者注册。 这些JAR文件通常在Java的JDK或JRE的`lib`目录下,或者作为第三方库(如Apache Directory ...
8、在编写 Java 代码时,需要使用 Hashtable 对象来存储 LDAP 连接的参数,使用 Socket 对象来连接 LDAP 服务器,并使用 SSL/TLS 加密协议来确保密码修改的安全性。 9、在编写 Java 代码时,需要使用相关的 LDAP ...
采用java 实现LDAP协议的创建用户和查询用户,也就是WIndows AD域控的操作。
Java操作LDAP( Lightweight Directory Access Protocol)在SpringBoot工程中的实现主要涉及到的是企业级的身份验证与目录服务。LDAP是一种开放的标准协议,广泛用于存储用户账户、组信息和其他元数据。在这个项目中...