-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLdap.java
More file actions
65 lines (52 loc) · 2.39 KB
/
Ldap.java
File metadata and controls
65 lines (52 loc) · 2.39 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
56
57
58
59
60
61
62
63
64
65
import java.util.Hashtable;
import javax.naming.AuthenticationException;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.*;
import javax.naming.NamingEnumeration;
public class Ldap {
public static void main(String[] args) throws NamingException {
String username = "itadmin@domain.local";
String password = "123456789";
String url = "ldap://xxx.xxx.xxx.xxx:389/";
String base = "OU=User,OU=Development,DC=Develop,DC=local";
Hashtable<String, Object> ldapParams = new Hashtable<String, Object>();
ldapParams.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
ldapParams.put(Context.PROVIDER_URL, url);
ldapParams.put(Context.SECURITY_AUTHENTICATION, "simple");
ldapParams.put(Context.SECURITY_PRINCIPAL, username);
ldapParams.put(Context.SECURITY_CREDENTIALS, password);
// Specify SSL
//ldapParams.put(Context.SECURITY_PROTOCOL, "ssl");
InitialDirContext ldapCtx = null;
try {
ldapCtx = new InitialDirContext(ldapParams);
System.out.println(ldapCtx);
if (ldapCtx != null) {
System.out.println("login success.");
String searchFilter = "(cn=itadmin)";
SearchControls controls = new SearchControls();
controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration<SearchResult> results = ldapCtx.search(base, searchFilter, 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 = " + cn);
}
}
} catch (AuthenticationException ex) {
System.out.println("login fail. [err 1]");
System.err.println(ex);
} catch (NamingException ex) {
System.out.println("login fail. [err 2]");
System.err.println(ex);
} catch (Exception e) {
System.out.println("login fail. [err 3]");
System.err.println(e);
} finally {
System.out.println("LDAP Context is " + ldapCtx);
}
}
}