View Javadoc
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  	//private String	group;
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  		//String query = userType + "=" + username;
27  		System.out.println("query = " + query);
28  		// Authenticate as S. User and password "mysecret"
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  		// Create the initial context
35  		try {
36  			ctx = new InitialDirContext(env);
37  			ctx.close();
38  		} catch (NamingException e) {
39  			e.printStackTrace();
40  			return(false);
41  		}
42  		//env.put(Context.SECURITY_AUTHENTICATION, "simple");
43  		//env.put(Context.SECURITY_PRINCIPAL, query);
44  		//env.put(Context.SECURITY_CREDENTIALS, password);
45  		//DirContext dc = ctx.createSubcontext(username, env);
46  
47  		
48  		return(true);
49  	}
50  
51  	/**
52  	 * @param args
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  }