MobileFirst 8.0 用户身份验证 LDAP 错误
MobileFirst 8.0 user authentication LDAP error
我目前正在从 IBM Worklight 7.0 迁移到 IBM MobileFirst 8.0。目前,我正面临针对 LDAP 服务器检查的用户身份验证功能,我是 LDAP 的新手...
我试图连接到一个免费的 LDAP 服务器,我检查过该服务器可用于测试目的(IBM 团队 here). This is my code so far (extracted from this LDAP connector snippet 描述):
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);
// Search the user
NamingEnumeration<SearchResult> searchResults = ldapContext.search("", searchString, sc);
ArrayList<SearchResult> searchResultsList = Collections.list(searchResults);
if (searchResultsList.size() != 1) {
errorMsg = "Wrong Credentials";
return false;
} else {
// login with user DN + password
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("", searchString, sc) 执行失败,我不确定原因:
javax.naming.NameNotFoundException: [LDAP: error code 32 - No Such
Object]; remaining name ''
到目前为止,执行似乎很简单...有人可以帮我解决这个错误吗?
提前致谢!
在 test-error 的几次迭代之后,我发现了如何执行搜索:
ldapContext.search("dc=example,dc=com", searchString, sc)
我目前正在从 IBM Worklight 7.0 迁移到 IBM MobileFirst 8.0。目前,我正面临针对 LDAP 服务器检查的用户身份验证功能,我是 LDAP 的新手...
我试图连接到一个免费的 LDAP 服务器,我检查过该服务器可用于测试目的(IBM 团队 here). This is my code so far (extracted from this LDAP connector snippet 描述):
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);
// Search the user
NamingEnumeration<SearchResult> searchResults = ldapContext.search("", searchString, sc);
ArrayList<SearchResult> searchResultsList = Collections.list(searchResults);
if (searchResultsList.size() != 1) {
errorMsg = "Wrong Credentials";
return false;
} else {
// login with user DN + password
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("", searchString, sc) 执行失败,我不确定原因:
javax.naming.NameNotFoundException: [LDAP: error code 32 - No Such
Object]; remaining name ''
到目前为止,执行似乎很简单...有人可以帮我解决这个错误吗?
提前致谢!
在 test-error 的几次迭代之后,我发现了如何执行搜索:
ldapContext.search("dc=example,dc=com", searchString, sc)