forked from heidsoft/heidsoft-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLdapBasicExample.java
More file actions
55 lines (52 loc) · 2.08 KB
/
Copy pathLdapBasicExample.java
File metadata and controls
55 lines (52 loc) · 2.08 KB
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
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.directory.*;
import java.util.Hashtable;
/**
* User: gmc
* Date: 16/02/11
*/
public class LdapBasicExample {
public static void main(String[] args) {
String userName = "AWUser1";
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://192.168.1.102:389/dc=agileworks,dc=com");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, new String("agileworks" + "\\" + userName));
env.put(Context.SECURITY_CREDENTIALS, "$password1");
DirContext ctx = null;
NamingEnumeration results = null;
try {
ctx = new InitialDirContext(env);
SearchControls controls = new SearchControls();
controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
results = ctx.search("", "(objectclass=person)", controls);
while (results.hasMore()) {
SearchResult searchResult = (SearchResult) results.next();
Attributes attributes = searchResult.getAttributes();
Attribute attr = attributes.get("cn");
String cn = (String) attr.get();
System.out.println(" Person Common Name = " + attributes.get("cn"));
System.out.println(" Person Display Name = " + attributes.get("displayName"));
System.out.println(" Person logonhours = " + attributes.get("logonhours"));
System.out.println(" Person MemberOf = " + attributes.get("memberOf"));
}
} catch (Throwable e) {
e.printStackTrace();
} finally {
if (results != null) {
try {
results.close();
} catch (Exception e) {
}
}
if (ctx != null) {
try {
ctx.close();
} catch (Exception e) {
}
}
}
}
}