繁体   English   中英

验证Java中的LDAP用户密码

[英]verify LDAP user password in Java

我正在尝试通过一些Java代码验证用户的密码:

private void bindToLdap(String un, String pw) throws NamingException, AuthenticationException {
    log.debug ("Doing LDAP bind as user");
    Hashtable<String, Object> env = new Hashtable<String, Object>();
    env.put(Context.PROVIDER_URL, LdapConns.openLdapUrl);
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, "uid="+un+",ou=people,<base OU>");
    env.put(Context.SECURITY_CREDENTIALS, pw);
    DirContext dctx = new InitialDirContext(env);
    log.debug("Context below:");
    log.debug(dctx.getEnvironment().toString());
    dctx.lookup("ou=people");
    log.debug("connection context:");
    log.debug(dctx.getEnvironment().toString());
    log.debug ("Bound as user?");
}

到目前为止,我所读的所有内容都表明,如果我输入了错误的密码,则在实例化DirContext时应该采取异常处理。 事实并非如此-即使我输入了已知的错误密码,我也会获得有效的DirContext。 我希望在dctx.lookup上捕获到AuthenticationException,实际上是在捕获NamingException。

在调试日志中,我显示:

[usermgmt.action.ChangeInternalPassword:bindToLdap:242] Context below:
[usermgmt.action.ChangeInternalPassword:bindToLdap:243] {java.naming.provider.url=ldaps://<ldap server>:636/, java.naming.factory.initial=org.apache.naming.java.javaURLContextFactory, java.naming.security.authentication=simple, java.naming.security.principal=nononyen_test, java.naming.factory.url.pkgs=org.apache.naming, java.naming.security.credentials=fubar!}

如何验证密码是否正确? 绑定后是否需要对LDAP数据库执行搜索以采取例外措施?

LDAP数据库是OpenLDAP。

要确定身份验证ID和密码是否正确,应用程序必须建立与目录服务器的连接,然后传输绑定请求并检查响应。 来自服务器的绑定响应将包含指示绑定请求成功或失败的整数结果代码,并且还可能包含带有一些有关用户条目状态的额外信息的响应控件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM