Search in sources :

Example 16 with JndiLdapContextFactory

use of org.apache.shiro.realm.ldap.JndiLdapContextFactory in project zeppelin by apache.

the class ShiroAuthenticationService method getUserList.

/**
 * Function to extract users from LDAP.
 */
private List<String> getUserList(DefaultLdapRealm r, String searchText, int numUsersToFetch) {
    List<String> userList = new ArrayList<>();
    String userDnTemplate = r.getUserDnTemplate();
    String[] userDn = userDnTemplate.split(",", 2);
    String userDnPrefix = userDn[0].split("=")[0];
    String userDnSuffix = userDn[1];
    JndiLdapContextFactory cf = (JndiLdapContextFactory) r.getContextFactory();
    try {
        LdapContext ctx = cf.getSystemLdapContext();
        SearchControls constraints = new SearchControls();
        constraints.setCountLimit(numUsersToFetch);
        constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
        String[] attrIDs = { userDnPrefix };
        constraints.setReturningAttributes(attrIDs);
        NamingEnumeration<SearchResult> result = ctx.search(userDnSuffix, "(" + userDnPrefix + "=*" + searchText + "*)", constraints);
        while (result.hasMore()) {
            Attributes attrs = result.next().getAttributes();
            if (attrs.get(userDnPrefix) != null) {
                String currentUser = attrs.get(userDnPrefix).toString();
                userList.add(currentUser.split(":")[1].trim());
            }
        }
    } catch (Exception e) {
        LOGGER.error("Error retrieving User list from Ldap Realm", e);
    }
    LOGGER.info("UserList: {}", userList);
    return userList;
}
Also used : ArrayList(java.util.ArrayList) Attributes(javax.naming.directory.Attributes) SearchControls(javax.naming.directory.SearchControls) SearchResult(javax.naming.directory.SearchResult) LdapContext(javax.naming.ldap.LdapContext) UnavailableSecurityManagerException(org.apache.shiro.UnavailableSecurityManagerException) NamingException(javax.naming.NamingException) JndiLdapContextFactory(org.apache.shiro.realm.ldap.JndiLdapContextFactory)

Aggregations

JndiLdapContextFactory (org.apache.shiro.realm.ldap.JndiLdapContextFactory)16 LdapContext (javax.naming.ldap.LdapContext)8 ArrayList (java.util.ArrayList)6 Attributes (javax.naming.directory.Attributes)6 SearchControls (javax.naming.directory.SearchControls)6 SearchResult (javax.naming.directory.SearchResult)6 Test (org.junit.Test)5 NamingEnumeration (javax.naming.NamingEnumeration)4 NamingException (javax.naming.NamingException)2 UnavailableSecurityManagerException (org.apache.shiro.UnavailableSecurityManagerException)2 JndiLdapRealm (org.apache.shiro.realm.ldap.JndiLdapRealm)2 SimplePrincipalCollection (org.apache.shiro.subject.SimplePrincipalCollection)2 IOException (java.io.IOException)1 Hashtable (java.util.Hashtable)1 CommunicationException (javax.naming.CommunicationException)1 BasicAttribute (javax.naming.directory.BasicAttribute)1 ModificationItem (javax.naming.directory.ModificationItem)1 InitialLdapContext (javax.naming.ldap.InitialLdapContext)1 StartTlsRequest (javax.naming.ldap.StartTlsRequest)1 StartTlsResponse (javax.naming.ldap.StartTlsResponse)1