gpt4 book ai didi

java - 使用 JNDI/Java 中的当前用户在 LDAP 上进行身份验证

转载 作者:行者123 更新时间:2023-11-29 05:58:48 25 4
gpt4 key购买 nike

我以为我会找到更多关于这个主题的信息,但我没有找到。

我必须编写一个 java 应用程序来检查特定用户属于哪个用户组。

但是为了向服务器进行身份验证,我不能要求用户名和密码,也不能将其存储在源(或其他文件)中。

有没有办法使用 JNDI 和 Java 对当前登录的用户进行身份验证?

最佳答案

您所能做的就是检查是否有某个用户与当前登录您的 Java 应用程序的用户具有相同的用户名。没有密码,您将无法检查任何其他内容。为此,您需要一些有权列出其他用户的 ldap 用户的用户名和密码。然后您可以为您的用户查询 LDAP。

这是根据我使用的东西改编的示例,它检查 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com