使用objectGUID查询 - Spring LDAP模板

时间:2017-02-01 00:52:18

标签: java spring active-directory ldap hex

我正在尝试获取,存储并依次使用objectGUID来查询Active Directory。 获取用户属性我正在使用

public static class MyDnKeyValueAttMapper implements AttributesMapper<Object> {
        @Override
        public List<LdapKeyValueList> mapFromAttributes(Attributes attributes)
                throws NamingException, javax.naming.NamingException {
            List<LdapKeyValueList> attributeKeyValMap = new ArrayList<LdapKeyValueList>();
            NamingEnumeration<String> namingEnumeration = attributes.getIDs();

            while (namingEnumeration.hasMoreElements()) {
                String attributeName = (String) namingEnumeration.nextElement();
                String AttributeValue = attributes.get(attributeName).get().toString();
                attributeKeyValMap.add(new LdapKeyValueList(attributeName, AttributeValue));
            }
            return attributeKeyValMap;
        }
    }

objectGuid似乎总是以字符串格式返回。 我也试过 -

UUID guid = (UUID) attributes.get("objectGUID").get();

抛出“无法将字符串转换为uuid”

的错误

似乎之前我可以做任何事情ldaptemplate搜索总是以字符串格式返回属性。

如何以其格式获取“objectGUID”,以便我可以存储它并在ldapTemplate搜索查询中使用。

提前致谢。

2 个答案:

答案 0 :(得分:3)

对于Spring,将“ java.naming.ldap.attributes.binary”属性注入ldapTemplate

@Bean
public LdapTemplate ldapTemplate() {
  return new LdapTemplate(contextSource());
}

@Bean
public ContextSource contextSource() {
  final LdapContextSource contextSource = new LdapContextSource();
  contextSource.setUrl(env.getRequiredProperty("ldap.url"));
  contextSource.setBase(env.getRequiredProperty("ldap.base"));
  contextSource.setUserDn(env.getRequiredProperty("ldap.user"));
  contextSource.setPassword(env.getRequiredProperty("ldap.password"));

  // Important!!! Tell ldapTemplate to retrieve AD field
  // "objectGUID" as binary. Otherwise it will be
  // retrieved as a String, thus, modifying the byte[] array
  final Map<String, Object> envProps = new HashMap<>();
  envProps.put("java.naming.ldap.attributes.binary","objectGUID");
  contextSource.setBaseEnvironmentProperties(envProps);

  return contextSource;
}

...

// Will not complain about the String to byte[] conversion and
// Has to be 16 in length. If not, you did something 
// wrong. For example ldapTemplate still retrieves objectGUID
// as String, modifying the value
byte[] guidBytes = (byte[]) attributes.get("objectGUID").get();
if (guidBytes.length == 16) {
  // Convert encoded AD objectGUID to UUID
  // objectGUID is not storing bits sequentially, so do the dance
  UUID uuid = UUID.fromString(
    String.format("%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", 
    guidBytes[3] & 255, 
    guidBytes[2] & 255, 
    guidBytes[1] & 255, 
    guidBytes[0] & 255, 
    guidBytes[5] & 255, 
    guidBytes[4] & 255, 
    guidBytes[7] & 255, 
    guidBytes[6] & 255, 
    guidBytes[8] & 255, 
    guidBytes[9] & 255, 
    guidBytes[10] & 255, 
    guidBytes[11] & 255, 
    guidBytes[12] & 255, 
    guidBytes[13] & 255, 
    guidBytes[14] & 255, 
    guidBytes[15] & 255));
}

答案 1 :(得分:2)

如果您不希望将二进制属性(objectGUID具有Octet String语法)作为字符串检索,则必须这样说。使用Spring,您必须将<entry key="java.naming.ldap.attributes.binary" value="objectGUID"/>添加到您的上下文环境中。

稍后byte[] guid = (byte[]) namingEnumeration.getAttributes().get("objectGUID").get();应该返回您要查找的内容。

刚打字,未经过测试。