LDAPException:无效凭据(49)包含grails的凭据无效

时间:2011-08-25 16:33:10

标签: java grails ldap bind openldap

这些是我正在使用的导入:

import com.novell.ldap.*;
import java.io.UnsupportedEncodingException;

我正在尝试进行一个非常简单的密码验证,我在以下位置找到:

http://developer.novell.com/documentation/samplecode/jldap_sample/index.htm

我似乎无法让绑定工作。有没有人用grails或java做更好的方法。我发现自己真的迷失了,任何例子或指导都会有所帮助。

感谢。

1 个答案:

答案 0 :(得分:1)

此Java示例使用UnboundID LDAP SDK连接并绑定到目录服务器。像以下一样运行:

$ java -cp YOUR_CLASSPATH BindExample hostname port bindDn password

BindExample.java:

import com.unboundid.ldap.sdk.BindRequest;
import com.unboundid.ldap.sdk.BindResult;
import com.unboundid.ldap.sdk.Control;
import com.unboundid.ldap.sdk.LDAPConnection;
import com.unboundid.ldap.sdk.LDAPException;
import com.unboundid.ldap.sdk.ResultCode;
import com.unboundid.ldap.sdk.SimpleBindRequest;

public final class BindExample {

    public static void main(String... args) {
    if(args.length == 4) {
        final String hostname = args[0];
        final String dn = args[2];
        final String password = args[3];
        int port;
        LDAPConnection ldapConnection;
        try {
            port = Integer.parseInt(args[1]);
        } catch(NumberFormatException nfx) {
            System.err.println(args[1] + " is not an integer, using 389");
            port = 389;
        }
        try {
            ldapConnection =
                new LDAPConnection(hostname,port);
        } catch(final LDAPException lex) {
            System.err.println("failed to connect to "
                   + hostname + " " +
                   lex.getMessage());
            return;
        }
        try {
            final BindRequest bindRequest =
                new SimpleBindRequest(dn,password);
            BindResult bindResult = 
                ldapConnection.bind(bindRequest);
            if(bindResult.getResultCode() == ResultCode.SUCCESS) {
                System.out.println("authentication successful");
            }
            if(bindResult.hasResponseControl()) {
                Control[] controls = 
                    bindResult.getResponseControls();
                // handle response controls ...
            }
            ldapConnection.close();
        } catch(final LDAPException lex ) {
            System.err.println("bind failed");
            ldapConnection.close();
            return;
        }
    }
  }
}