方案1:我可以从活动目录中获取objectGUID,但它不是可读的字符串格式。另外,我们需要将其以解码格式存储在db中。通过提供的链接“ http://www.developerscrappad.com/1109/windows/active-directory/java-ldap-jndi-2-ways-of-decoding-and-using-the-objectguid-from-windows-active-directory/”中的给定示例,它演示了如何解码objectGUID,但是他们认为objectGUID的长度为16个字节(128位)。在我们的情况下,当我尝试获取objectGUID时,我得到的位数超过128位,有时我得到的位数少于128位,即我们没有获得特定的位长。
我实现的代码供参考:
public class GetLDAPUsers {
public static void main(String args[]) {
new GetLDAPUsers().getUserFromAD();
}
void getUserFromAD() {
try {
LDAPConnection connection = new LDAPConnection("192.xxx.xx.xxx", 389);
System.out.println(connection);
String baseDN = "DC=wcomp1,DC=com";
String[] attributes = { "entryUUID", "sn", "mail", "givenName",
"objectGUID", "userAccountControl", "isDeleted", "modifyTimestamp", "WhenChanged", "WhenCreated"};
// Set Ldap Connection Options for server timeout
LDAPConnectionOptions connOption = new LDAPConnectionOptions();
connOption.setAutoReconnect(true);
connOption.setConnectTimeoutMillis(55000);
connection.setConnectionOptions(connOption);
//connection bind
connection.bind("CN=abc,CN=ab,DC=users,DC=com", "password");
System.out.println("connection successfully");
//search filter query for search specific user,for all users use (&(objectclass=User)) filter.
Filter filter = Filter.create("(&(objectclass=User)(givenName=testUserName))");
SearchRequest searchRequest = new SearchRequest(baseDN, SearchScope.SUB, filter,
attributes);
SearchResult searchResult = connection.search(searchRequest);
//get user detail
for (SearchResultEntry searchResultEntry : searchResult.getSearchEntries()) {
System.out.println("user name " + searchResultEntry.getAttribute("givenName").getValue() +
searchResultEntry.getAttribute("objectGUID").getValue()); //We get here objectGUID string which unreadable format
//We convert here objectGUID in dashed string
System.out.println("decoded objectGUID = " + convertToDashedString(searchResultEntry.getAttribute("objectGUID").getValue().getBytes()));
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static String convertToDashedString(byte[] objectGUID) {
StringBuilder displayStr = new StringBuilder();
displayStr.append(prefixZeros((int) objectGUID[3] & 0xFF));
displayStr.append(prefixZeros((int) objectGUID[2] & 0xFF));
displayStr.append(prefixZeros((int) objectGUID[1] & 0xFF));
displayStr.append(prefixZeros((int) objectGUID[0] & 0xFF));
displayStr.append("-");
displayStr.append(prefixZeros((int) objectGUID[5] & 0xFF));
displayStr.append(prefixZeros((int) objectGUID[4] & 0xFF));
displayStr.append("-");
displayStr.append(prefixZeros((int) objectGUID[7] & 0xFF));
displayStr.append(prefixZeros((int) objectGUID[6] & 0xFF));
displayStr.append("-");
displayStr.append(prefixZeros((int) objectGUID[8] & 0xFF));
displayStr.append(prefixZeros((int) objectGUID[9] & 0xFF));
displayStr.append("-");
displayStr.append(prefixZeros((int) objectGUID[10] & 0xFF));
displayStr.append(prefixZeros((int) objectGUID[11] & 0xFF));
displayStr.append(prefixZeros((int) objectGUID[12] & 0xFF));
displayStr.append(prefixZeros((int) objectGUID[13] & 0xFF));
displayStr.append(prefixZeros((int) objectGUID[14] & 0xFF));
displayStr.append(prefixZeros((int) objectGUID[15] & 0xFF));
return displayStr.toString();
}
private static String prefixZeros(int value) {
if (value <= 0xF) {
StringBuilder sb = new StringBuilder("0");
sb.append(Integer.toHexString(value));
return sb.toString();
} else {
return Integer.toHexString(value);
}
}
}
场景2:同样,当我尝试在Windows环境和Linux环境中使用上述示例获取objectGUID时,我为同一用户获得了不同的objectGUID。
最佳答案
您不能将ObjectGUID
解释为字符串。通常,我将设置目录上下文环境以将ObjectGUID
作为byte[]
返回,然后使用convert方法
env.put("java.naming.ldap.attributes.binary", "ObjectGUID");
String newGuid = convertToDashedString(guid);