首页 > 解决方案 > 在springboot中以分页形式返回ldap条目

问题描述

我有一个 ldap 方法,它返回其中的所有用户(几乎 1300 个用户),我想按页面返回它们,类似于 Springboot 中的 PagingAndSortingRepository 所做的:

如果我有这个端点( users/?page=0&size=1 )并且我想在第 0 页返回只有 1 个条目。

有没有办法做到这一点?

目前我有这个,但它不起作用:

SearchRequest searchRequest = new SearchRequest(ldapConfig.getBaseDn(), SearchScope.SUB,
                Filter.createEqualityFilter("objectClass", "person"));
        ASN1OctetString resumeCookie = null;
        while (true) {
            searchRequest.setControls(new SimplePagedResultsControl(pageable.getPageSize(), resumeCookie));
            SearchResult searchResult = ldapConnection.search(searchRequest);
            numSearches++;
            totalEntriesReturned += searchResult.getEntryCount();
            for (SearchResultEntry e : searchResult.getSearchEntries()) {

                 String[] completeDN = UaaUtils.searchCnInDn(e.getDN());
                 String[] username = completeDN[0].split("=");
                 UserEntity u = new UserEntity(username[1]);
                 list.add(u);

                System.out.println("TESTE");
            }

            SimplePagedResultsControl responseControl = SimplePagedResultsControl.get(searchResult);
            if (responseControl.moreResultsToReturn()) {
                // The resume cookie can be included in the simple paged results
                // control included in the next search to get the next page of results.
                System.out.println("Antes "+resumeCookie);
                resumeCookie = responseControl.getCookie();
                System.out.println("Depois "+resumeCookie);
            } else {
                break;
            }

            Page<UserEntity> newPage = new PageImpl<>(list, pageable, totalEntriesReturned);

            System.out.println("content " + newPage.getContent());
            System.out.println("total elements " + newPage.getTotalElements());

            System.out.println(totalEntriesReturned);
        }

标签: springspring-bootldapunboundid-ldap-sdk

解决方案


我不确定这是否是正确的方法,但这是我的做法:

public PaginatedLookup getAll(String page, String perPage) {
  PagedResultsCookie cookie = null;
  List<LdapUser> results;

  try {
      if ( page != null ) {
          cookie = new PagedResultsCookie(Hex.decode(page));
      } // end if

      Integer pageSize = perPage != null ? Integer.parseInt(perPage) : PROCESSOR_PAGE_SIZE;

      PagedResultsDirContextProcessor processor = new PagedResultsDirContextProcessor(pageSize, cookie);
      LdapName base = LdapUtils.emptyLdapName();

      SearchControls sc = new SearchControls();
      sc.setSearchScope(SearchControls.SUBTREE_SCOPE);
      sc.setTimeLimit(THREE_SECONDS);
      sc.setCountLimit(pageSize);
      sc.setReturningAttributes(new String[]{"cn", "title"});

      results = ldapTemplate.search(base, filter.encode(), sc, new PersonAttributesMapper(), processor);

      cookie = processor.getCookie();
  } catch ( Exception e ) {
      log.error(e.getMessage());
      return null;
  } // end try-catch

  String nextPage = null;

  if ( cookie != null && cookie.getCookie() != null ) {
      nextPage = new String(Hex.encode(cookie.getCookie()));
  } // end if
  return new PaginatedLookup(nextPage, results);
}

我一直在解决的主要问题是试图将 cookie 作为可以发送给客户端的东西,这正是我Hex.decode派上Hex.encode用场的地方。 PersonAttributesMapper是一个私有映射器,我必须使字段更易于阅读,并且PaginatedLookup是我用于 API 响应的自定义类。


推荐阅读