Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Program to validate a user against LDAP for login Authentication

  1. #1
    Junior Member
    Join Date
    Jul 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Program to validate a user against LDAP for login Authentication

    Hi All ,
    I have a very basic issue in LDAP . I am using OPEN DS as my LDAP Server and JNDI API to access LDAP Server for authorization.
    After creating a new user in Open DS, I created an html with username & password as textfield. Then I created a servlet which connected succesfully to LDAP Server. However, I am getting the password from LDAP Server for the current user in encrypted / digested format and hence my authorization always fails.

    This is my code:
    ==============
    package com.login.servlet; 
     
    import javax.naming.Context; 
    import javax.naming.NamingException; 
    import javax.naming.directory.Attributes; 
    import javax.naming.directory.DirContext; 
    import javax.naming.ldap.InitialLdapContext; 
    .. 
    public class LoginServlet extends HTTPServlet{ 
        private static DirContext createLdapContext() throws NamingException { 
            Hashtable env = new Hashtable(); 
            env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); 
            env.put(Context.PROVIDER_URL, "ldap://172.30.91.123:389"); 
            env.put(Context.SECURITY_AUTHENTICATION, "simple"); 
            env.put(Context.SECURITY_PRINCIPAL, "cn=Directory Manager"); 
            env.put(Context.SECURITY_CREDENTIALS, "opends"); 
            return new InitialLdapContext(env, null); 
        } 
     
    public void validateUser(HttpServletRequest request, SessionVO sessionVO) { 
    try { 
                String un=request.getParameter("username"); 
                String pwd = request.getParameter("password"); 
                DirContext dirContext = createLdapContext(); 
                Attributes attrs = dirContext.getAttributes("uid="un",ou=People,dc=example,dc=com"); 
                String actualPwd = attrs.get("userPassword").toString(); 
                if(pwd.equals(actualpwd)){ 
                    System.out.println("Password correct"); 
                }else { 
                    System.out.println("Password worng"); 
    // I am getting this message always for both correct and incorrect password.
            } 
        } catch (NamingException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        }catch (Exception e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        } 
       } 
    }
    =======================================
    I beleive that the authentication of the user should happen against the directory server and not inside the application like done in the above code .
    Either way I am stuck without a sample to proceed.
    My question is, how to write a program using JNDI API to authorize an user from LDAP Server for a login Screen?
    Thanks in Advance !


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Program to validate a user against LDAP for login Authentication

    Good morning,

    As I see it you either need to call an authorize method on the LDAP interface which just takes care of all this for you or you will have to hash the user password with the same hash algorithm used by the LDAP service and then compare them.

    // Json

Similar Threads

  1. Replies: 1
    Last Post: November 2nd, 2012, 02:21 PM
  2. Replies: 3
    Last Post: December 22nd, 2011, 09:46 AM
  3. Reading user input from the console with the Scanner class
    By JavaPF in forum Java SE API Tutorials
    Replies: 3
    Last Post: September 7th, 2011, 03:09 AM
  4. Problem while implementing a basic user interface menu
    By Rastabot in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 3rd, 2009, 04:38 PM