code: @Nullable
      protected String findUserDn(LdapContextFactory ldapContextFactory, String username) throws NamingException {
          LdapContext ctx = null;
          try {
              // Binds using the system username and password.
              ctx = ldapContextFactory.getSystemLdapContext();
  
              final SearchControls ctrl = new SearchControls();
              ctrl.setCountLimit(1);
              ctrl.setSearchScope(SearchControls.SUBTREE_SCOPE);
              ctrl.setTimeLimit(searchTimeoutMillis);
  
              final String filter =
                      searchFilter != null ? USERNAME_PLACEHOLDER.matcher(searchFilter)
                                                                 .replaceAll(username)
                                           : username;
              final NamingEnumeration<SearchResult> result = ctx.search(searchBase, filter, ctrl);
              try {
                  if (!result.hasMore()) {
                      return null;
                  }
                  return result.next().getNameInNamespace();
              } finally {
                  result.close();
              }
          } finally {
              LdapUtils.closeContext(ctx);
          }
      }
docs: Finds a distinguished name(DN) of a user by querying the active directory LDAP context for the
  specified username.
  
  @return the DN of the user, or {@code null} if there's no such user