1 package com.buckosoft.BSAccountMan.ldap;
2
3 import java.util.Hashtable;
4
5 import javax.naming.Context;
6 import javax.naming.NamingException;
7 import javax.naming.directory.DirContext;
8 import javax.naming.directory.InitialDirContext;
9
10 public class LdapManager {
11
12
13
14 private String url = "ldap://ldap:389/";
15 private String baseDN = "ou=people,dc=buckosoft,dc=com";
16 private String userType = "cn";
17
18 DirContext ctx = null;
19
20 public boolean authenticate(String username, String password) {
21 Hashtable<String, Object> env = new Hashtable<String, Object>();
22 env.put(Context.INITIAL_CONTEXT_FACTORY,
23 "com.sun.jndi.ldap.LdapCtxFactory");
24 env.put(Context.PROVIDER_URL, url+baseDN);
25 String query = userType + "=" + username + "," + baseDN;
26
27 System.out.println("query = " + query);
28
29 env.put(Context.SECURITY_AUTHENTICATION, "simple");
30 env.put(Context.SECURITY_PRINCIPAL, query);
31 env.put(Context.SECURITY_CREDENTIALS, password);
32 env.put("com.sun.jndi.ldap.connect.pool.debug", "all");
33 env.put("com.sun.jndi.ldap.trace.ber", System.err);
34
35 try {
36 ctx = new InitialDirContext(env);
37 ctx.close();
38 } catch (NamingException e) {
39 e.printStackTrace();
40 return(false);
41 }
42
43
44
45
46
47
48 return(true);
49 }
50
51
52
53
54
55 public static void main(String[] args) {
56 LdapManager lm = new LdapManager();
57 boolean result = lm.authenticate("Layla Balaska", "layla");
58 System.out.println("result = " + result);
59 }
60
61
62 }