使用unboundid sdk在ApacheDS Server中搜索组内的用户

时间:2015-05-29 05:52:11

标签: java ldap apacheds unboundid-ldap-sdk

我正在使用ApacheDS作为目录服务器& UnboundID作为用于输入用户及其相关组的LDAP SDK。我正在关注一个链接

http://ldapwiki.willeke.com/wiki/Tips%20using%20UnboundID%20LDAP%20SDK

他们使用CompareRequest方法查明用户是否是特定群组的成员?

我针对此方法编写的代码就是这样



	//input ---- uid = "rohit.joshi" and groupName = "Java"
	public boolean isUserMemberOfGroup(String uid, String groupName) {
		boolean answ = false;
		LDAPConnection connection = connect();
		try {
		//userDN -- uid=rohit.joshi,ou=users,o=wipro
		String userDN = "uid=" + uid + ",ou=users," + LDAP_BASE_DN; 
		//groupDN -- cn=Java,ou=groups,o=wipro
		String groupDN = "cn=" + groupName + ",ou=groups," + LDAP_BASE_DN;
	CompareRequest compareRequest = new CompareRequest(userDN, "uniqueMember", groupDN);
		CompareResult compareResult = connection.compare(compareRequest);
			if (compareResult.compareMatched()) {
				// The user is a member of the group.
				System.out.println("user is a member of group");
			}
			else {
				// The user is not a member of the group.
				System.out.println("user is not a member of group");
			}
		} catch (LDAPException e) {
			e.printStackTrace();
		}
		return answ;
	}






CREATING LDAP CONNECTOR
Connection with LDAP Server Established : true
LDAPException(resultCode=16 (no such attribute), errorMessage='NO_SUCH_ATTRIBUTE: failed for MessageType : COMPARE_REQUEST
Message ID : 1
    Compare request
        Entry : 'uid=mahesh.joshi,ou=users,o=wipro'
        Attribute description : 'uniqueMember'
        Attribute value : 'cn=Java,ou=groups,o=wiproorg.apache.directory.api.ldap.model.message.CompareRequestImpl@1d9123e1: null', diagnosticMessage='NO_SUCH_ATTRIBUTE: failed for MessageType : COMPARE_REQUEST
Message ID : 1
    Compare request
        Entry : 'uid=mahesh.joshi,ou=users,o=wipro'
        Attribute description : 'uniqueMember'
        Attribute value : 'cn=Java,ou=groups,o=wiproorg.apache.directory.api.ldap.model.message.CompareRequestImpl@1d9123e1: null')
	at com.unboundid.ldap.sdk.LDAPConnection.compare(LDAPConnection.java:2236)
	at com.LdapServiceImpl.isUserMemberOfGroup(LdapServiceImpl.java:380)
	at com.App.main(App.java:102)




但运行此方法我在控制台中收到错误,如上所示。虽然我能够单独检查用户和组的存在。但是仍然无法在单个查询操作中检查它们。在这方面的任何建议都将是一个很大的帮助。

1 个答案:

答案 0 :(得分:1)

显然有' NO_SUCH_ATTRIBUTE'独特会员'在ApacheDS中。

LDAPException(resultCode=16 (no such attribute), errorMessage='NO_SUCH_ATTRIBUTE: failed for MessageType : COMPARE_REQUEST
Message ID : 1
    Compare request
        Entry : 'uid=mahesh.joshi,ou=users,o=wipro'
        Attribute description : 'uniqueMember'

尝试使用'成员'而不是“uniqueMember'。

。”

工作示例:

public static void main(String[] args)
{
    LDAPConnection connection = new LDAPConnection();
    try
    {
       connection.connect("localhost", 10389);
    }
    catch (LDAPException e)
    {
    // TODO Auto-generated catch block
       e.printStackTrace();
    }
    String attributeName = "uniqueMember";
    String assertionValue = "uid=mahesh.joshi,ou=users,ou=sevenSeas,dc=example,dc=com";
    String entryDN = "cn=Java,ou=groups,ou=sevenSeas,dc=example,dc=com";
    CompareRequest compareRequest = new CompareRequest(entryDN, attributeName, assertionValue);
    CompareResult compareResult = null;
    try
    {
       compareResult = connection.compare(compareRequest);
       if (compareResult.compareMatched())
       {
           System.out.println("The user: " + assertionValue + " is a member of the group: " + entryDN);
       }
       else
       {
           System.out.println("The user: " + assertionValue + " is NOT a member of the group: " + entryDN);
       }
    }
    catch (LDAPException e)
    {
    // TODO Auto-generated catch block
       e.printStackTrace();
    }
}

尽可能地为您的场景设置示例。 -Jim