場景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位,即我們沒有得到特定的位長度。 我實施了參考代碼:如何使用Java中的UnboundID LDAP SDK從Active Directory中解析objectGUID?
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:另外當我嘗試獲取的objectGUID使用例如在windows環境和Linux環境上面我得到不同的objectGUID爲相同的用戶。
我從你提供的,你的問題是在URL地址中的信息猜測:https://ldapwiki.com/wiki/Universally%20Unique%20Identifier#section-Universally + Unique + Identifier-BinaryEncoding提供更多信息或一些代碼示例。 – jwilleke
謝謝jeemster您的意見,但我無法打開你提供的鏈接。 –
嘗試:http://ldapwiki.com/wiki/Universally%20Unique%20Identifier#section-Universally+Unique+Identifier-BinaryEncoding – jwilleke