does this ldap code work?

Discussion in 'Java' started by timasmith@hotmail.com, Aug 26, 2006.

  1. Guest

    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;

    }
    }
     
    , Aug 26, 2006
    #1
    1. Advertising

  2. Nigel Wade Guest

    wrote:

    > 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";


    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();
    > }
    >
    > 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);



    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.

    --
    Nigel Wade, System Administrator, Space Plasma Physics Group,
    University of Leicester, Leicester, LE1 7RH, UK
    E-mail :
    Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555
     
    Nigel Wade, Aug 29, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    554
    Raymond DeCampo
    Feb 21, 2006
  2. rcmn
    Replies:
    1
    Views:
    373
    =?ISO-8859-1?Q?Michael_Str=F6der?=
    Nov 6, 2006
  3. Jason Wold

    using LDAP Controls in ruby-ldap

    Jason Wold, Nov 4, 2004, in forum: Ruby
    Replies:
    5
    Views:
    281
  4. Ian Macdonald
    Replies:
    0
    Views:
    239
    Ian Macdonald
    Mar 15, 2005
  5. James Hughes
    Replies:
    4
    Views:
    351
    James Hughes
    Dec 13, 2005
Loading...

Share This Page