如何在 LDAP 上下文中设置放宽控制

How to set relax controls on a LDAP context

我想在 javax.naming.ldap.LdapContext 上设置 relax 控件(如 https://datatracker.ietf.org/doc/html/draft-zeilenga-ldap-relax-03 中定义),但我不知道如何构建控制正确:

LdapContext context = new InitialLdapContext(...);
Control[] controls = { new BasicControl(/* What to put here? */) };
context.setRequestControls(controls);

幸运的是我在草稿中找到了部分答案:

The Relax Rules control is an LDAP Control [RFC4511] whose controlType is IANA-ASSIGNED-OID, controlValue is empty, and the criticality of TRUE.

所以唯一的问题是找到 IANA-ASSIGNED-OID

查看 OpenLdap 2.4.40 (include/ldap.h) 的源代码是:

#define LDAP_CONTROL_RELAX              "1.3.6.1.4.1.4203.666.5.12" 

现在可以正常工作了:

LdapContext context = new InitialLdapContext(...);
Control[] controls = { new BasicControl(
    "1.3.6.1.4.1.4203.666.5.12", // OID
    true, // criticality
    null // control value
) };
context.setRequestControls(controls);

请务必注意,此 OID 属于 OpenLDAP Experimental Arc (1.3.6.1.4.1.4203.666),并且可能会在未来的 OpenLDAP 版本中更改。

在一般情况下,您需要编写一个 class 来扩展 BasicControl,并实现编码和解码所需的所有 ASN.1 内容。鉴于官方 JDK.

缺乏对 ASN.1 的支持,这不是一项简单的任务

然而,由于此控件很简单:

import javax.naming.ldap.BasicControl;

/**
 * Relax Rules control
 * @author Esmond Pitt
 * @see <a href="https://tools.ietf.org/html/draft-zeilenga-ldap-relax-03">The Relax Rules Control</a>
 * @see <a href="">Stack Overflow</a>
 */
public class RelaxRulesControl extends BasicControl
{
    /** The OID, see Tobias's answer for provenance. */
    public static final String  OID = "1.3.6.1.4.1.4203.666.5.12";

    /** Construct an instance with criticality = true */
    public RelaxRulesControl()
    {
        super(OID, true, null);
    }

    /** Construct an instance with critically as specified */
    public RelaxRulesControl(boolean critical)
    {
        super(OID, critical, null);
    }
}