KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > simya > ldap > beans > LdapAuthenticateBean


1 package net.simya.ldap.beans;
2
3 import javax.naming.*;
4 import javax.naming.directory.*;
5
6 //import java.util.Hashtable;
7

8 /**
9  * This code authenticates users againts ldap
10  *
11  * assumption1: user only sets DN ;
12  */

13 public class LdapAuthenticateBean
14 {
15     
16     private DirContext ctx = null;
17     
18     private String JavaDoc authType="simple",
19             DN,
20             password;
21     
22     public void setContext (DirContext ctx)
23     {
24         this.ctx = ctx;
25     }
26     
27     public void setAuthType ( String JavaDoc authType)
28     {
29         this.authType = authType;
30     }
31     
32     public void setDN (String JavaDoc dn)
33     {
34         this.DN = dn;
35     }
36     
37     public void setPassword ( String JavaDoc password)
38     {
39         this.password = password;
40     }
41     
42     public void authenticate ()
43     throws NamingException
44     {
45         
46             // Authenticate as S. User and password "mysecret"
47
ctx.addToEnvironment(Context.SECURITY_AUTHENTICATION, authType);
48             
49             // if principal and credentials are empty, you will bind anonymously
50
ctx.addToEnvironment(Context.SECURITY_PRINCIPAL, DN);
51             ctx.addToEnvironment(Context.SECURITY_CREDENTIALS, password);
52             
53             // we have to make something in order to activate new authentication
54
// so we are only looking provider url.
55
String JavaDoc tempurl = (String JavaDoc) (ctx.getEnvironment()).get(ctx.PROVIDER_URL) ;
56             ctx.lookup(tempurl);
57             
58     }
59             
60     
61 }// end of class
62
Popular Tags