如何在java中使用未绑定的LDAP SDK从Active directory获取解码的objectGUID?

如何在java中使用未绑定的LDAP SDK从Active directory获取解码的objectGUID?,java,active-directory,ldap,unboundid-ldap-sdk,Java,Active Directory,Ldap,Unboundid Ldap Sdk,场景1:我可以从active directory获取objectGUID,但它不是可读的字符串格式。我们还需要以解码格式将其存储在数据库中。对于提供的链接“”中的给定示例,它演示了如何解码objectGUID,但他们认为objectGUID长度为16字节(128位)。在我们的例子中,当我试图获取objectGUID时,我得到的是128位以上,有时我得到的是128位以下,也就是说,我们没有得到特定的位长度。 我的实现代码供参考: public class GetLDAPUsers { publi

场景1:我可以从active directory获取objectGUID,但它不是可读的字符串格式。我们还需要以解码格式将其存储在数据库中。对于提供的链接“”中的给定示例,它演示了如何解码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);
    }
}
公共类GetLDAPUsers{
公共静态void main(字符串参数[]){
新建GetLDAPUsers().getUserFromAD();
}
void getUserFromAD(){
试一试{
LDAPConnection连接=新的LDAPConnection(“192.xxx.xx.xxx”,389);
系统输出打印LN(连接);
String baseDN=“DC=wcomp1,DC=com”;
String[]属性={“entryUUID”、“sn”、“mail”、“givenName”,
“objectGUID”、“userAccountControl”、“isDeleted”、“modifyTimestamp”、“WhenChanged”、“WhenCreated”};
//为服务器超时设置Ldap连接选项
LDAPConnectionOptions connpoption=新的LDAPConnectionOptions();
connOption.setAutoReconnect(true);
connOption.setConnectTimeoutMillis(55000);
connection.setConnectionOptions(连接选项);
//连接绑定
bind(“CN=abc,CN=ab,DC=users,DC=com”,“password”);
System.out.println(“连接成功”);
//搜索筛选器查询搜索特定用户,对于所有用户使用(&(objectclass=user))筛选器。
Filter=Filter.create(&(objectclass=User)(givenName=testUserName));
SearchRequest SearchRequest=新的SearchRequest(baseDN、SearchScope.SUB、filter、,
属性);
SearchResult=connection.search(searchRequest);
//获取用户详细信息
for(SearchResultEntry SearchResultEntry:searchResult.getSearchEntries()){
System.out.println(“用户名”+searchResultEntry.getAttribute(“givenName”).getValue()+
searchResultEntry.getAttribute(“objectGUID”).getValue();//我们在这里得到的objectGUID字符串格式不可读
//我们在这里将objectGUID转换为虚线字符串
System.out.println(“解码的objectGUID=“+convertToDashedString(searchResultEntry.getAttribute(“objectGUID”).getValue().getBytes());
}
}捕获(例外e){
e、 printStackTrace();
}
}
公共静态字符串convertToDashedString(字节[]objectGUID){
StringBuilder displayStr=新建StringBuilder();
displayStr.append(前缀为零((int)objectGUID[3]&0xFF));
displayStr.append(前缀为零((int)objectGUID[2]&0xFF));
displayStr.append(前缀为零((int)objectGUID[1]&0xFF));
displayStr.append(前缀为零((int)objectGUID[0]&0xFF));
displayStr.append(“-”);
displayStr.append(前缀为零((int)objectGUID[5]&0xFF));
displayStr.append(前缀为零((int)objectGUID[4]&0xFF));
displayStr.append(“-”);
displayStr.append(前缀为零((int)objectGUID[7]&0xFF));
displayStr.append(前缀为零((int)objectGUID[6]&0xFF));
displayStr.append(“-”);
displayStr.append(前缀为零((int)objectGUID[8]&0xFF));
displayStr.append(前缀为零((int)objectGUID[9]&0xFF));
displayStr.append(“-”);
displayStr.append(前缀为零((int)objectGUID[10]&0xFF));
displayStr.append(前缀为零((int)objectGUID[11]&0xFF));
displayStr.append(前缀为零((int)objectGUID[12]&0xFF));
displayStr.append(前缀为零((int)objectGUID[13]&0xFF));
displayStr.append(前缀为零((int)objectGUID[14]&0xFF));
displayStr.append(前缀为零((int)objectGUID[15]&0xFF));
返回displayStr.toString();
}
专用静态字符串前缀零(int值){

如果(value您无法将
ObjectGUID
解释为字符串。通常,我会将目录上下文环境设置为将
ObjectGUID
返回为
字节[]
,然后使用convert方法

env.put("java.naming.ldap.attributes.binary", "ObjectGUID");

String newGuid = convertToDashedString(guid);
春季: 注入财产

java.naming.ldap.attributes.binary
正确地输入ldapTemplate


您只需执行以下操作:

public static String getGuidFromByteArray(byte[] bytes) {
    ByteBuffer bb = ByteBuffer.wrap(bytes);
    long high = bb.getLong();
    long low = bb.getLong();
    UUID uuid = new UUID(high, low);
    return uuid.toString();
}

我从您提供的信息猜测,您的问题在URL中得到了解决:提供更多信息和/或一些代码示例。感谢jeemster的评论,但我无法打开您提供的链接。请尝试:这是一个完美的答案,值得投票,感谢@bradvido提供的解决方案:)