Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • User Authentication using LDAP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 158
    Comment on it

    If your application has requirement to authenticate user using LDAP you can use following code for same:

    LDAP: is Lightweight Directory Access Protocol. LDAP server will be having users information, and requirement could be to only authenticate those users which exists on LDAP. For simplicity we are ignoring the authorization module here and just coding for Login Submit button.

    Please note you need to change following values as per your requirement:

    ldap-server-host:                   Change this to your LDAP Server Host Name. 
    389:                                Change the LDAP Server Port if it is different from 389. 
    MyLoginId:                          Change the MyLoginId value to user User Login Id input. 
    ,ou=internal,ou=people,DC=MyCompany,DC=COM: Change the post-fix as per LDAP Configuration. 
    MyPassword:                         Change the MyPassword value to user Password input.
    

    Following would be method call to check LDAP Authentication:

    public static boolean isValidLDAPUser() { 
    
        try{
            // Set up the environment for creating the initial context
            Hashtable<String, String> env = new Hashtable<String, String>();
            env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
            env.put(Context.PROVIDER_URL, "ldap://ldap-server-host:389");
            // 
            env.put(Context.SECURITY_AUTHENTICATION, "simple");
            env.put(Context.SECURITY_PRINCIPAL, "uid=MyLoginId,ou=internal,ou=people,DC=MyCompany,DC=COM");
            env.put(Context.SECURITY_CREDENTIALS, "MyPassword");
    
            // Create the initial context
    
            DirContext ctx = new InitialDirContext(env);
            boolean result = ctx != null;
    
            if(ctx != null)
                ctx.close();
    
            System.out.println( result);
            return result;
        }catch (Exception e){           
            e.printStackTrace();
            return false;
            }
    }
    

    If user will give Login-id and Password we can call this method and if it return true we can allow user to enter the web-application else can give invalid credentials error.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: