LDAP Authentication using Java

前端 未结 4 1423
面向向阳花
面向向阳花 2020-12-12 15:23

I need to do LDAP Authentication for an application.

I tried the following program:

import java.util.Hashtable;  

import javax.naming.Context;  
imp         


        
4条回答
  •  独厮守ぢ
    2020-12-12 15:53

    Following Code authenticates from LDAP using pure Java JNDI. The Principle is:-

    1. First Lookup the user using a admin or DN user.
    2. The user object needs to be passed to LDAP again with the user credential
    3. No Exception means - Authenticated Successfully. Else Authentication Failed.

    Code Snippet

    public static boolean authenticateJndi(String username, String password) throws Exception{
        Properties props = new Properties();
        props.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        props.put(Context.PROVIDER_URL, "ldap://LDAPSERVER:PORT");
        props.put(Context.SECURITY_PRINCIPAL, "uid=adminuser,ou=special users,o=xx.com");//adminuser - User with special priviledge, dn user
        props.put(Context.SECURITY_CREDENTIALS, "adminpassword");//dn user password
    
    
        InitialDirContext context = new InitialDirContext(props);
    
        SearchControls ctrls = new SearchControls();
        ctrls.setReturningAttributes(new String[] { "givenName", "sn","memberOf" });
        ctrls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    
        NamingEnumeration answers = context.search("o=xx.com", "(uid=" + username + ")", ctrls);
        javax.naming.directory.SearchResult result = answers.nextElement();
    
        String user = result.getNameInNamespace();
    
        try {
            props = new Properties();
            props.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
            props.put(Context.PROVIDER_URL, "ldap://LDAPSERVER:PORT");
            props.put(Context.SECURITY_PRINCIPAL, user);
            props.put(Context.SECURITY_CREDENTIALS, password);
    
       context = new InitialDirContext(props);
        } catch (Exception e) {
            return false;
        }
        return true;
    }
    

提交回复
热议问题