使用GSS和频道绑定连接到LDAPS

问题描述

我正在尝试以域用户身份登录的Windows计算机上的GSS绑定到Active Directory服务器。通常,这很好。但是在执行签名和绑定时

(请参见https://support.microsoft.com/en-us/help/4520412/2020-ldap-channel-binding-and-ldap-signing-requirements-for-windows

GSS无法绑定。根据{{​​3}},此功能添加到JDK 16(b18)中,但是我无法成功绑定失败并显示错误消息

javax.naming.AuthenticationException: [LDAP: error code 49 - 80090346: LdapErr: DSID-0C090595,comment: AcceptSecurityContext error,data 80090346,v3839

其他相关详细信息:

  • 我的公司有3个域控制器,每个都返回相同的错误。 它们当前设置为value = 1(如果支持),尽管value = 2 (总是)具有相同的效果

  • 我相当确定我的项目使用的是JDK 16 build 20。

  • 使用用户名和密码绑定时,所有功能都可以正常工作。

  • 此外,通过LDAP(而非LDAPS)进行的绑定也适用于GSS。

  • 关键组合是GSS + LDAPS。

编辑 在JDK 15.0.1.9中,我得到的行为与在JDK 16中相同。这使我认为,在检查了包含的源代码之后,该功能并未在JDK16b20中完全实现,但我可以看到代码在哪里已添加

以下是用于连接到服务器的代码

public class ActiveDirectory {

    public LdapContext getConnection(boolean ssl) throws NamingException {
        return getConnection(null,null,ssl);
    }
    public LdapContext getConnection(String username,String password,boolean ssl) throws NamingException {
        String domainName = "";
        try {
            String fqdn = java.net.InetAddress.getLocalHost().getCanonicalHostName(); //lookup the domain
            if (fqdn.split("\\.").length > 1) {
                domainName = fqdn.substring(fqdn.indexOf(".") + 1);
            }
        } catch (java.net.UnkNownHostException e) {
        }
        Hashtable props = new Hashtable();
        if (password != null) { 
            password = password.trim();
            if (password.length() == 0) { //the password was blank. Bind with GSS anyway
                password = null;
            }
        }
        if (password != null) { // a password was provided. Bind as that user
            String principalName = username + "@" + domainName;
            props.put(Context.Security_PRINCIPAL,principalName);
            props.put(Context.Security_AUTHENTICATION,"simple");
            props.put(Context.Security_CREDENTIALS,password);
        } else { //no password. Bind with GSS
            String principalName = System.getProperty("user.name")  + "@" + domainName;
            username=System.getProperty("user.name");
            props.put(Context.Security_PRINCIPAL,"GSSAPI");
            System.setProperty("sun.security.jgss.native","true");
        }
        props.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
        String ldapURL = "";
        for (String server : domainControllers) {
            if (ssl) { //use SSL to bind
                ldapURL = "ldaps://" + server + "." + domainName + "/";
                props.put(Context.Security_PROTOCOL,"ssl");
                props.put("com.sun.jndi.ldap.tls.cbtype","tls-server-end-point"); //use channel binding !!! DOESNT WORK WITH GSS. See https://bugs.openjdk.java.net/browse/JDK-8245527?attachmentviewmode=gallery
                props.put("com.sun.jndi.ldap.connect.timeout","2000");
            } else { //dont use ssl to bind
                ldapURL = "ldap://" + server + "." + domainName + '/';
                props.put(Context.Security_PROTOCOL,"");
            }
            props.put(Context.PROVIDER_URL,ldapURL);
            try {
                return connect(props);
            } catch (NamingException e) {
                System.out.println(e.toString());
            }
        }
        throw new NamingException("Failed to authenticate " + username + "@" + domainName);
    }

    private LdapContext connect(Hashtable props) throws NamingException {
        return new InitialLdapContext(props,null);
    }

如果任何人都可以发现我在做什么错,那将不胜感激。谢谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)