我以为我会找到有关该主题的更多信息,但我没有。
我必须编写一个 java 应用程序来检查特定用户属于哪个组织。
但是要通过服务器进行身份验证,我无法询问用户名和密码,也无法将其存储在源代码(或其他文件)中。
是否可以使用 JNDI 和 Java 对当前登录的用户进行身份验证?
最佳答案
您所能做的就是检查是否有某个用户与当前登录 Java 应用程序的用户具有相同的用户名。如果没有密码,您将无法检查其他任何内容。为此,您需要某个有权列出其他用户的 LDAP 用户的用户名和密码。然后您可以查询您的用户的 LDAP。
这是一个改编 self 使用的示例,它会检查 Activity 目录,因此也许需要一些更改:
boolean userFound = user_exits("searchUser",
"searchPassword",
"(sAMAccountName={USERNAME})",
"ldap://ldap.mydomain.com",
"OU=MYOU,dc=mydomain,dc=com");
private boolean user_exits(String searchUser, String searchPassword,
String filter, String url, String baseDn) throws NamingException {
DirContext ctx = null;
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, url);
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, searchUser);
env.put(Context.SECURITY_CREDENTIALS, searchPassword);
try {
ctx = new InitialDirContext(env);
String[] attributeFilter = {};
SearchControls sc = new SearchControls();
sc.setReturningAttributes(attributeFilter);
sc.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration<SearchResult> results = ctx.search(baseDn, filter, sc);
return results.hasMore();
} catch (NamingException e) {
throw e;
} finally {
if (ctx != null) {
try {
ctx.close();
} catch (NamingException e) {}
}
}
}
关于java - 使用 JNDI/Java 中的当前用户在 LDAP 上进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10979766/