JAVA Web应用程序中的LDAP身份验证

时间:2017-05-31 08:54:46

标签: java active-directory ldap

我正在尝试从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”。

2 个答案:

答案 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

相关问题