LdapRealm extends JndiLdapRealm { @Override protected String getUserDn(final String principal) throws IllegalArgumentException, IllegalStateException { String userDn; String matchedPrincipal = matchPrincipal(principal); String userSearchBase = getUserSearchBase(); String userSearchAttributeName = getUserSearchAttributeName(); if ((userSearchBase == null || userSearchBase.isEmpty()) || (userSearchAttributeName == null && userSearchFilter == null && !"object".equalsIgnoreCase(userSearchScope))) { userDn = expandTemplate(userDnTemplate, matchedPrincipal); if (log.isDebugEnabled()) { log.debug("LDAP UserDN and Principal: " + userDn + "," + principal); } return userDn; } String searchBase = expandTemplate(getUserSearchBase(), matchedPrincipal); String searchFilter = null; if (userSearchFilter == null) { if (userSearchAttributeName == null) { searchFilter = String.format("(objectclass=%1$s)", getUserObjectClass()); } else { searchFilter = String.format("(&(objectclass=%1$s)(%2$s=%3$s))", getUserObjectClass(), userSearchAttributeName, expandTemplate(getUserSearchAttributeTemplate(), matchedPrincipal)); } } else { searchFilter = expandTemplate(userSearchFilter, matchedPrincipal); } SearchControls searchControls = getUserSearchControls(); LdapContext systemLdapCtx = null; NamingEnumeration<SearchResult> searchResultEnum = null; try { systemLdapCtx = getContextFactory().getSystemLdapContext(); if (log.isDebugEnabled()) { log.debug("SearchBase,SearchFilter,UserSearchScope: " + searchBase + "," + searchFilter + "," + userSearchScope); } searchResultEnum = systemLdapCtx.search(searchBase, searchFilter, searchControls); if (searchResultEnum.hasMore()) { SearchResult searchResult = searchResultEnum.next(); userDn = searchResult.getNameInNamespace(); if (log.isDebugEnabled()) { log.debug("UserDN Returned,Principal: " + userDn + "," + principal); } return userDn; } else { throw new IllegalArgumentException("Illegal principal name: " + principal); } } catch (AuthenticationException ne) { ne.printStackTrace(); throw new IllegalArgumentException("Illegal principal name: " + principal); } catch (NamingException ne) { throw new IllegalArgumentException("Hit NamingException: " + ne.getMessage()); } finally { try { if (searchResultEnum != null) { searchResultEnum.close(); } } catch (NamingException ne) { } finally { LdapUtils.closeContext(systemLdapCtx); } } } LdapRealm(); void setHadoopSecurityCredentialPath(String hadoopSecurityCredentialPath); @Override AuthorizationInfo queryForAuthorizationInfo(final PrincipalCollection principals,
      final LdapContextFactory ldapContextFactory); Map<String, String> getListRoles(); String getSearchBase(); void setSearchBase(String searchBase); String getUserSearchBase(); void setUserSearchBase(String userSearchBase); int getPagingSize(); void setPagingSize(int pagingSize); String getGroupSearchBase(); void setGroupSearchBase(String groupSearchBase); String getGroupObjectClass(); void setGroupObjectClass(String groupObjectClassAttribute); String getMemberAttribute(); void setMemberAttribute(String memberAttribute); String getGroupIdAttribute(); void setGroupIdAttribute(String groupIdAttribute); void setMemberAttributeValueTemplate(String template); void setAllowedRolesForAuthentication(List<String> allowedRolesForAuthencation); void setRolesByGroup(Map<String, String> rolesByGroup); Map<String, String> getRolesByGroup(); void setPermissionsByRole(String permissionsByRoleStr); Map<String, List<String>> getPermissionsByRole(); boolean isAuthorizationEnabled(); void setAuthorizationEnabled(boolean authorizationEnabled); String getUserSearchAttributeName(); void setUserSearchAttributeName(String userSearchAttributeName); String getUserObjectClass(); void setUserObjectClass(String userObjectClass); String getPrincipalRegex(); void setPrincipalRegex(String regex); String getUserSearchAttributeTemplate(); void setUserSearchAttributeTemplate(final String template); String getUserSearchFilter(); void setUserSearchFilter(final String filter); String getGroupSearchFilter(); void setGroupSearchFilter(final String filter); boolean getUserLowerCase(); void setUserLowerCase(boolean userLowerCase); String getUserSearchScope(); void setUserSearchScope(final String scope); String getGroupSearchScope(); void setGroupSearchScope(final String scope); boolean isGroupSearchEnableMatchingRuleInChain(); void setGroupSearchEnableMatchingRuleInChain(
      boolean groupSearchEnableMatchingRuleInChain); @Override void setUserDnTemplate(final String template);  }
@Test public void testGetUserDn() { LdapRealm realm = new LdapRealm(); realm.setUserSearchFilter(null); assertEquals("foo ", realm.getUserDn("foo ")); realm.setUserSearchFilter("memberUid={0}"); assertEquals("foo", realm.getUserDn("foo")); }