我目前正在从IBM Worklight 7.0迁移到IBM MobileFirst 8.0。目前,我面临的用户身份验证功能是根据LDAP服务器检查的,我是一个关于LDAP的新手。。。
我试图连接到一个免费的LDAP服务器,我已经检查了它是否可用于测试(描述
here
). 这是我目前为止的代码
LDAP connector snippet
来自IBM团队):
if (credentials != null && credentials.containsKey(USERNAME) && credentials.containsKey(PASSWORD)) {
String username = "gauss";
String password = "password";
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.PROVIDER_URL, "ldap://ldap.forumsys.com:389");
SearchControls sc = new SearchControls();
String[] attributeFilter = { "uid", "cn" };
sc.setReturningAttributes(attributeFilter);
sc.setSearchScope(SearchControls.SUBTREE_SCOPE);
try {
LdapContext ldapContext = new InitialLdapContext(env, null);
String searchString = "(&(uid=%v))";
searchString = searchString.replaceAll("%v", username);
NamingEnumeration<SearchResult> searchResults = ldapContext.search("", searchString, sc);
ArrayList<SearchResult> searchResultsList = Collections.list(searchResults);
if (searchResultsList.size() != 1) {
errorMsg = "Wrong Credentials";
return false;
} else {
SearchResult searchResult = searchResultsList.get(0);
ldapContext.addToEnvironment(Context.SECURITY_PRINCIPAL, searchResult.getName());
ldapContext.addToEnvironment(Context.SECURITY_CREDENTIALS, password);
try {
ldapContext.reconnect(null);
userId = (String) searchResult.getAttributes().get(config.getLdapUserAttribute()).get();
displayName = (String) searchResult.getAttributes().get(config.getLdapNameAttribute())
.get();
return true;
} catch (Exception e) {
logger.info(e.toString());
errorMsg = "Wrong Credentials";
}
}
} catch (Exception e) {
errorMsg = "Connection to user repository failed";
logger.info(e.toString());
}
} else {
errorMsg = "Credentials not set properly";
}
执行失败
ldapContext.search(“”,搜索字符串,sc)
javax.naming.NameNotFoundException: [LDAP: error code 32 - No Such
Object]; remaining name ''
到目前为止,执行似乎很简单。。。有人能帮我解决这个错误吗?
提前谢谢!