Spring LdapTemplate получает SID от члена группы - PullRequest
0 голосов
/ 04 июня 2018

В одном месте я запускаю запрос, который дает мне определенную группу.

LdapQuery groupQuery = LdapQueryBuilder.query().where("objectCategory").is("group").and("CN").is(groupCn);
List<String> userSids = groupLdapTemplate.search(groupQuery, mapper);

В маппере у меня есть:

public Object mapFromAttributes(Attributes attrs) throws NamingException {
        NamingEnumeration enumeration = attrs.get("member").getAll();
        (...)

Проблема в том, что у меня есть следующий списокучастники:

CN=S-1-5-21-1957455145-3106008588-2947984729-1106,CN=ForeignSecurityPrincipals,DC=mylab2,DC=local  
CN=S-1-5-21-1957455145-3106008588-2947984729-1105,CN=ForeignSecurityPrincipals,DC=mylab2,DC=local

Я хочу получить только их SID без остальных.Я не уверен, как я могу это сделать, поскольку он должен быть универсальным для разных сценариев (таким образом, переменная dn).

Я бы хотел избежать неприятных вещей, как показано ниже:

public Object mapFromAttributes(Attributes attrs) throws NamingException {
        List<String> result = new ArrayList<>();

        NamingEnumeration enumeration = attrs.get("member").getAll();

        while (enumeration.hasMoreElements()) {
            String value = (String) enumeration.nextElement();
            final String sidCn = value.split(",")[0];
            result.add(sidCn.substring(3, sidCn.length()));
        }

        return result;
    }

1 Ответ

0 голосов
/ 05 июня 2018

Вы должны использовать ContextMapper для управления несколькими значениями атрибутов и LdapUtils для анализа отличительных имен:

ContextMapper<List<String>> mapper = new AbstractContextMapper<>() {
  public List<String> doMapFromContext(DirContextOperations ctx) {
    return Stream.of(ctx.getStringAttributes("member"))
                 .map(LdapUtils::newLdapName)
                 .map(name -> LdapUtils.getStringValue(name, name.size() - 1))
                 .collect(Collectors.toList());
    }
 };

 LdapQuery groupQuery = LdapQueryBuilder.query()
                          .where("objectCategory").is("group")
                          .and("CN").is(groupCn);
 List<String> userSids = groupLdapTemplate.search(groupQuery, mapper)
                          .stream()
                          .flatMap(Collection::stream)
                          .collect(Collectors.toList());
...