我正在尝试从LDAP验证用户的用户ID和密码,这是我的java代码
Hashtable<Object, String> props = new Hashtable<Object, String>();
//props.put(Context.SECURITY_AUTHENTICATION, "simple");
props.put(Context.SECURITY_PRINCIPAL,
"uid=muhammad.zafar,ou=IT Operations,ou=Information Systems,dc=bi,dc=com,dc=pk");
props.put(Context.SECURITY_CREDENTIALS, "Passw0rd");
DirContext context;
try {
context = com.sun.jndi.ldap.LdapCtxFactory.getLdapCtxInstance("ldap://ldap.bi.com.pk:389/dc=bi,dc=com,dc=pk" + '/', props);
context.close();
} catch (Exception e) {
throw new BadCredentialsException("Invalid Username or Password");
}
因为用户muhammad.zafar存在于IT运营“ou”中,所以效果很好。但我不想用它来验证用户,所以我尝试了很多SECURITY_PRINCIPAL的设置,但没有一个能为我工作
props.put(Context.SECURITY_PRINCIPAL,
"uid=muhammad.zafar");
它抛出异常“javax.naming.AuthenticationException:[LDAP:错误代码49 - 证书无效]” 甚至我试图放入
props.put(Context.SECURITY_AUTHENTICATION, "simple");
他们都没有为我工作。我希望我的问题很明确,因为我的应用程序中没有“ou”。
答案 0 :(得分:0)
尝试此解决方案,希望它适合您 possible solution
答案 1 :(得分:0)
这不是您想要使用ou
进行身份验证还是不进行身份验证的问题。
事实是,simple
身份验证模式下的LDAP协议和JNDI(请参阅doc)要求您将SECURITY_PRINCIPAL
设置为用户的dn。
所以你有2种方法可以解决这个问题:
使用技术帐户搜索尝试进行身份验证的用户的DN,之后使用之前找到的dn进行身份验证
不要使用simple
身份验证机制,而是使用您的目录支持的SASL,它允许您使用除dn之外的其他值。
有关更多信息,请参阅此处:https://docs.oracle.com/javase/tutorial/jndi/ldap/sasl.html