Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2006

Tip: Looking for answers? Try searching our database.

does this ldap code work?

Thread view: 
timasmith@hotmail.com - 26 Aug 2006 13:22 GMT
Hi,

I dont have an ldap server, tested this at work without success - but I
may in the wrong server something - can you tell me if this code alone
is enough to purely authenticate against an LDAP server?  Do I need
other key properties - I tried port 389 without success - I get a timed
out error.

import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.*;

public class LdapAuthentication {

    private String initialContext = "com.sun.jndi.ldap.LdapCtxFactory";
    private String authenticationMethod = "simple";
    private String providerUrl = "ldap://ldap:636";

    public LdapAuthentication() {
        super();
    }

    public void authenticate (String username, String password) throws
InvalidUserLoginException {
       DirContext ctx = null;
       try {
           ctx = getContext(username, password);
       } catch (NamingException ne) {
           throw new InvalidUserLoginException(ne.toString());
       }  finally {
           if (ctx != null) {
               try {
                   ctx.close();
               } catch (NamingException ne) {
               }
           }
       }
   }

   protected DirContext getContext(String username, String password)
throws NamingException {
       Hashtable env = new Hashtable();

       env.put(Context.INITIAL_CONTEXT_FACTORY, initialContext);
       env.put(Context.PROVIDER_URL, providerUrl);
       env.put(Context.SECURITY_AUTHENTICATION,authenticationMethod);
       env.put(Context.SECURITY_PRINCIPAL, username);
       env.put(Context.SECURITY_CREDENTIALS, password);

       DirContext ctx = new InitialDirContext(env);
       return ctx;
       
    }
}
Nigel Wade - 29 Aug 2006 10:23 GMT
> Hi,
>
[quoted text clipped - 13 lines]
>       private String authenticationMethod = "simple";
>       private String providerUrl = "ldap://ldap:636";

the format is protocol://hostname:port. If your ldap server is called "ldap"
this should be ok. Also, remember that 636 implies that the server is
configured to handle SSL, so you will need:

           env.put(Context.SECURITY_PROTOCOL, "ssl");

added to the environment.

>       public LdapAuthentication() {
>               super();
[quoted text clipped - 26 lines]
>         env.put(Context.SECURITY_PRINCIPAL, username);
>         env.put(Context.SECURITY_CREDENTIALS, password);

The SECURITY_CREDENTIALS should be set to a byte array, so use
password.getBytes() here.

>         DirContext ctx = new InitialDirContext(env);
>         return ctx;
>              
>       }
> }

Appart from those couple of points, it looks ok.

Signature

Nigel Wade, System Administrator, Space Plasma Physics Group,
           University of Leicester, Leicester, LE1 7RH, UK
E-mail :    nmw@ion.le.ac.uk
Phone :     +44 (0)116 2523548, Fax : +44 (0)116 2523555



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.