在springboot中以分页形式返回ldap条目

时间:2019-03-27 09:39:33

标签: spring spring-boot ldap unboundid-ldap-sdk

我有一个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);
        }

1 个答案:

答案 0 :(得分:0)

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

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.decodeHex.encode派上用场的地方。 PersonAttributesMapper是一个私有映射器,我必须使这些字段更易于阅读,而PaginatedLookup是一个我用于API响应的自定义类。