我正在尝试对用户进行身份验证,但它抛出Exception
.可能是配置有问题。
public class LdapApplication {
private static final String INITIAL_CONTEXT_FACTORY = "com.sun.jndi.ldap.LdapCtxFactory";
private static final String SECURITY_AUTHENTICATION ="simple";
private static final String NAMED_CONTEXT = "CN=Users";
private static final String SAM_ACCOUNT_NAME = "sAMAccountName=";
public static void main(String[] args) {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,INITIAL_CONTEXT_FACTORY);
env.put(Context.PROVIDER_URL, "ldap://ip:portNo/dc=organisation,dc=in");
env.put(Context.SECURITY_AUTHENTICATION, SECURITY_AUTHENTICATION);
env.put(Context.SECURITY_PRINCIPAL, "cn=userName,cn=Users");
env.put(Context.SECURITY_CREDENTIALS, "password" );
DirContext context = null;
NamingEnumeration namingEnumeration = null;
try {
context = new InitialDirContext(env);
namingEnumeration = context.search(NAMED_CONTEXT, SAM_ACCOUNT_NAME+ userName, null);
while (namingEnumeration.hasMore()) {
SearchResult searchResult = (SearchResult) namingEnumeration.next();
Attributes attributes = searchResult.getAttributes();
System.out.println(" Person Common Name = " + attributes.get("cn"));
System.out.println(" Person Display Name = " + attributes.get("displayName"));
}catch(Exception e){
System.out.println(e.getMessage());
e.printStackTrace();
}
}
} catch (Throwable e) {
e.printStackTrace();
} finally {
if (namingEnumeration != null) {
try {
namingEnumeration.close();
} catch (Exception e) {
}
}
if (context != null) {
try {
context.close();
} catch (Exception e) {
}
}
}
}
}
但如果我提到Context.SECURITY_PRINCIPAL
as "organisation\\userName"
代替"cn=userName,cn=Users"
它工作得很好。请建议一个可能的解决方案,因为我的要求是使用 cn 或 dc 为 SECURITY_PRINCIPAL 提供一些东西。
您使用的相对专有名称不起作用。
更改您要使用的代码
env.put(Context.SECURITY_PRINCIPAL, "cn=userName,cn=Users,dc=organisation,dc=in");
并将您的搜索上下文更改为:
private static final String NAMED_CONTEXT = "CN=Users,dc=organisation,dc=in";
始终对 LDAP 使用完整的专有名称。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)