环境搭建:拷贝novell ldap中ldap.jar,Novel_LDAP.jar ,utilities.jar到项目环境中即可。
程序代码:
public java.util.List getInfoSearch( java.lang.String name )
{
//@@begin getInfoSearch()
//wdComponentAPI.getMessageManager().reportWarning("以下为查询关键字/'"+name+"/'结果:");
List resultList =new ArrayList();
//HashMap map=null;
String ldapHost = "10.209.4.165";
int ldapPort = 389;
String loginDN = "cn=admin,ou=users,ou=nw,o=sgcc";
String password = "nwpn1234";
//String[] mechanisms = { "DIGEST-MD5" };
int ldapVersion = LDAPConnection.LDAP_V3;
int searchScope = LDAPConnection.SCOPE_ONE;
String searchBase = "ou=users,ou=nw,o=sgcc";
//String searchFilter = "(givenName=*)&(fullName=user*)";
String searchFilter = "(fullName="+name+"*)";
LDAPConnection lc = new LDAPConnection();
System.out.println(searchFilter);
try {
// connect to the server
lc.connect(ldapHost, ldapPort);
// bind to the server
try {
lc.bind(ldapVersion, loginDN, password.getBytes("UTF8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
System.out.println("Digest-MD5 Sasl bind was successful.");
//lc.disconnect();
} catch (LDAPException e) {
System.err.println("MD5Bind example failed");
System.err.println("Error: " + e.toString());
}
//search ldap
try {
LDAPSearchResults searchResults = lc.search(searchBase,
searchScope, searchFilter, null, // return all attributes
false);
while (searchResults.hasMore()) {
LDAPEntry nextEntry = null;
try {
nextEntry = searchResults.next();
} catch (LDAPException e) {
System.out.println("Error: " + e.toString());
}
LDAPAttributeSet attributeSet = nextEntry.getAttributeSet();
Iterator allAttributes = attributeSet.iterator();
HashMap map=new HashMap();
//List resultList=new ArrayList();
while (allAttributes.hasNext()) {
LDAPAttribute attribute = (LDAPAttribute) allAttributes
.next();
String attributeName = attribute.getName();
if(attributeName.equals("fullName")){
//System.out.println(" " + attributeName);
Enumeration allValues = attribute.getStringValues();
if (allValues != null) {
while (allValues.hasMoreElements()) {
String Value = (String) allValues.nextElement();
if (Base64.isLDIFSafe(Value)) {
//System.out.println(" " + Value);
map.put(attributeName,Value);
} else {
// base64 encode and then print out
Value = Base64.encode(Value.getBytes());
map.put(attributeName,Value);
//System.out.println(" " + Value);
}
}
}
}
}
resultList.add(map);
}
} catch (Exception ex) {
wdComponentAPI.getMessageManager().reportWarning("search Error:" + ex.toString());
}
return resultList;
//@@end
}
注:若是web dynpro调用LDAP的话,在controller中创建一个方法(假如为A),将上述代码拷贝到A中即可。