[英]Query using objectGUID - Spring LDAP Template
我試圖獲取,存儲,然后使用objectGUID來查詢Active目錄。 要獲取用戶屬性,我正在使用以下內容
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搜索查詢中使用。
提前致謝。
對於Spring,將“java.naming.ldap.attributes.binary”prop注入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));
}
如果您不希望將二進制屬性(objectGUID具有Octet String語法)作為字符串進行檢索,則必須這樣說。 使用Spring,您必須將<entry key="java.naming.ldap.attributes.binary" value="objectGUID"/>
到您的上下文環境中。
稍后在byte[] guid = (byte[]) namingEnumeration.getAttributes().get("objectGUID").get();
應該返回你想要的東西。
只是鍵入,未測試。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.