java获取Active Directory RootDSE

时间:2018-08-19 16:13:30

标签: java active-directory

我正在尝试使用Java获得Active Directory rootDSE。到目前为止,这是我尝试过的事情:

public class RootDSE {

    public DirContext context;
    public Attributes attributes;
    public NamingEnumeration enumerations;

    public RootDSE()
    {
        try {
            this.context = new InitialDirContext();
            this.attributes = context.getAttributes(
                "ldap://192.168.122.115", new String[]{"*"}
            );
            this.enumerations = this.attributes.getIDs();
            while(this.enumerations != null && this.enumerations.hasMore()) {
                String nextAttribute = (String)this.enumerations.next();
                System.out.println(attributes.get(nextAttribute));
            }
            context.close();
        } catch (NamingException e) {
            e.printStackTrace();
        }
    }
}

(我已评论import,以使阅读更容易。 我通过创建RootDSE对象来启动代码:

RootDSE dse = new RootDSE();
javax.naming.NamingException: [LDAP: error code 1 - 000004DC: LdapErr: DSID-0C090728, comment: In order to perform this operation a successful bind must be completed on the connection., data 0, v2580

我已经执行了经过身份验证的ldap请求,因此授予了网络连接和对目录服务的访问权限。此外,rootDSE请求应该是匿名的吗?不需要执行“ successful bind”来获得它吗?

有人可以解释为什么我会收到此错误,以及如何解决该错误吗?

非常感谢!

1 个答案:

答案 0 :(得分:0)

这是 AD 特有的问题,并且与 Java 的 JNDI LDAP 实现有冲突,Java 的 JNDI LDAP 实现在默认情况下假定 LDAPv3 服务器支持 RFC3296,但 AD 不支持。这导致报告的 - 也许不是那么直观 - 来自 AD 的错误消息。

解决方案:根据 this answer,您需要在上下文中设置 Context.REFERRAL 属性。

因此,像这样初始化你的上下文:

Properties props = new Properties();
props.setProperty(Context.REFERRAL, "throw");  // any other allowed value than the default ('ignore') will do
this.context = new InitialDirContext(props);