LDAP 政策

您正在查看 Apigee Edge 文档。
转到 Apigee X 文档
info

内容

LDAP 政策提供:

  • 身份验证:系统会根据 LDAP 提供方中的凭据验证请求中提供的用户凭据 。LDAP 政策在身份验证方面具有很高的灵活性,可让您使用任何 DN 值以及密码,即使您想要的 DN 值不在请求中也是如此。例如,假设您需要使用电子邮件地址 / 密码进行 身份验证。您可以选择以下选项:
    • 如果电子邮件地址在请求中,您可以直接使用该电子邮件地址和密码进行 LDAP 身份验证。
    • 如果电子邮件地址不在请求中,但另一个 DN 属性(例如电话号码)在请求中, 您可以使用电话号码从 LDAP 获取相应的电子邮件地址,然后使用电子邮件地址/ 密码进行身份验证。
  • 标识名 (DN) 搜索:除了身份验证之外,您还可以使用 LDAP 政策来识别请求中的用户属性(例如电子邮件地址),并执行查询以从 LDAP 中检索该用户的其他 DN 属性。检索到的 DN 存储在变量中。

当受保护资源的访问权限应仅限于 LDAP 提供方中的用户(例如管理员用户、组织用户和开发者)时,请使用 LDAP 政策,尤其是在 OAuth 令牌访问权限不必要或过于繁琐的情况下。该政策还旨在 检索域名元数据,以供 API 代理流使用。

例如,您可以让 API 调用仅在用户通过 LDAP 成功进行身份验证后执行 ;然后,您还可以选择在身份验证成功后 检索用户的 DN(域名)属性。

如需了解详情,请参阅:

示例

用户名/密码身份验证

<Ldap name="4GLdapPolicy">
   <LdapResource>ldap1</LdapResource>
   <Authentication>
       <UserName ref="request.header.username"/>
       <Password ref="request.header.password"/>
       <Scope>subtree</Scope>
       <BaseDN ref="apigee.baseDN"></BaseDN> <!-- default is dc=apigee,dc=com -->
    </Authentication>
 </Ldap>

此示例提供了针对 LDAP 提供方的身份验证。该政策会将请求中的用户名 和密码传递给 LDAP 以进行身份验证。

DN 属性身份验证

<Ldap name="LdapPolicy">
   <LdapResource>ldap1</LdapResource>
   <Authentication>
       <Password ref="request.header.password"/>
       <SearchQuery>mail={request.header.mail}</SearchQuery>
       <Scope>subtree</Scope>
       <BaseDN ref="apigee.baseDN"></BaseDN> <!-- default is dc=apigee,dc=com -->
    </Authentication>
 </Ldap>

此政策会使用请求标头中的电子邮件地址获取用户的 DN,然后 使用请求标头中提供的密码针对 LDAP 对用户进行身份验证。

搜索 LDAP

<Ldap name="LdapPolicy">
    <!-- using a custom LDAP provider -->
    <LdapConnectorClass>com.custom.ldap.MyProvider</LdapConnectorClass>
    <LdapResource>MyLdap</LdapResource>
    <Search>
        <BaseDN ref="apigee.baseDN"></BaseDN> <!-- default is dc=apigee,dc=com -->
        <SearchQuery>mail={request.header.mail}</SearchQuery>
        <Attributes>
            <Attribute>address</Attribute>
            <Attribute>phone</Attribute>
            <Attribute>title</Attribute>
        </Attributes>
        <Scope></Scope> <!-- default is subtree -->
    </Search>
</Ldap>

此政策引用了自定义 LDAP 提供方。它使用请求 标头中的电子邮件地址来识别用户,然后从 LDAP 中检索用户的地址、电话号码和职称。检索到的 DN 属性存储在变量中。请参阅“政策专用 变量”。

如需搜索 LDAP 并检索 DN 属性,请求必须包含管理员 凭据。

元素参考

以下是 LDAP 政策元素和属性的说明。

元素

说明

Ldap

父元素,其中包含一个 name 属性,供您输入政策名称。

LdapConnectorClass

将 LDAP 政策与自定义 LDAP 提供方(非 Apigee 提供)搭配使用时,请指定完全限定的 LDAP 连接器类。 这是您在其中实现 Apigee 的 ExternalLdapConProvider 接口的类。

LdapResource

输入 LDAP 资源的环境名称。如需了解详情,请参阅创建 LDAP 资源

BaseDN

LDAP 的基本级别,您的所有数据都位于此级别下。例如,在 Apigee 的 LDAP 提供方中,所有数据都位于 dc=apigee,dc=com 下。

  • ref:用于指定包含 BaseDN 值的流变量,例如 apigee.baseDN。ref 优先于显式 BaseDN 值。如果您同时指定了 ref 和 value,则 ref 具有优先权。如果 ref 在 运行时无法解析,则使用 value。

Scope

  • object:身份验证或搜索仅在 LDAP 的基本级别进行。
  • onelevel:身份验证或搜索在基本级别下一级进行。
  • subtree (默认):身份验证或搜索在基本级别进行 并在基本级别下完全递归进行。

Authentication

Authentication

您实现的身份验证行为的父元素。

UserName

空元素,采用以下属性之一:

  • ref:对请求中用户名的引用,例如 request.header.username
  • value:用户名本身

如果您未使用用户名进行身份验证,或者请求中未包含用户名,则无需添加此元素。

如果请求中包含用户名,但您想使用用户名以外的 DN 属性 对用户进行身份验证,例如电子邮件地址,请添加 SearchQuery 以获取与密码关联的用户电子邮件地址。LDAP 政策使用用户名查询 LDAP 提供方 以获取相应的电子邮件地址,然后使用该电子邮件地址进行身份验证。

Password

空元素,采用以下属性之一:

  • ref:对请求中密码的引用,例如 request.header.password
  • value:加密后的密码本身

SearchQuery

如果您想使用用户名以外的 DN 属性(例如电子邮件地址)进行身份验证,请将 LDAP 政策配置为从请求中获取 DN 属性(例如用户名),该属性用于在 LDAP 中识别用户、检索电子邮件地址并对用户进行身份验证。

例如,假设 LDAP 为存储电子邮件地址定义了“mail”属性:

<SearchQuery>mail={request.header.mail}</SearchQuery>

Search

Search

您实现的搜索行为的父元素。

SearchQuery

通过使用请求或响应中的元数据识别用户,您可以使用此 元素从 LDAP 中检索用户的其他 DN 属性。例如,如果 请求包含用户电子邮件地址,并且您的 LDAP 定义了用于存储用户电子邮件地址的 mail 属性,您将使用以下设置:

<SearchQuery>mail={request.header.mail}</SearchQuery>

此查询会在 LDAP 中搜索与请求中的电子邮件地址匹配的电子邮件地址,现在 政策可以使用 Attributes 元素检索该用户的其他 DN 属性。

Attributes

使用一个或多个 <Attribute> 元素来 标识您要为用户检索的 DN 元数据。至少需要一个属性。

例如,在 SearchQuery 识别用户后, 政策现在可以检索用户的 DN 属性,例如地址、电话号码和 用户的职称,如以下示例所示。

属性值是 LDAP 中定义的 DN 属性名称。

<Attributes>
  <Attribute>address</Attribute>
  <Attribute>phone</Attribute>
  <Attribute>title</Attribute>
</Attributes>

使用说明

适用于私有云的 Apigee Edge 可让您在 API 调用中利用 LDAP 提供方。借助 LDAP 政策,应用可以根据存储在 LDAP 中的用户验证凭据,并且您可以 从 LDAP 中检索标识名 (DN)(与 每个用户关联的元数据或属性,例如电子邮件地址、地址和电话号码)。返回的 DN 存储在变量中,供 API 代理进一步使用。

创建 LDAP 资源

LDAP 政策利用您在 Apigee Edge 中创建的 LDAP 资源。LDAP 资源 提供与 LDAP 代码库的连接信息。

如需创建和管理 LDAP 资源,请使用以下 API 和载荷:

API

创建 (POST) LDAP 资源或列出 (GET) 所有 LDAP 资源:

/v1/organizations/org_name/environments/environment/ldapresources

获取 (GET)、更新 (POST) 和删除 (DELETE) LDAP 资源的详细信息:

/v1/organizations/org_name/environments/environment/ldapresources/ldap_resource_name

载荷

以下是包含使用说明的 XML 载荷示例。

<LdapResource name="ldap1">
  <Connection>
    <Hosts>
      <!-- port is optional: defaults to 389 for ldap:// and 636 for ldaps:// -->
      <Host port="636">foo.com</Host>
    </Hosts>
    <SSLEnabled>false</SSLEnabled> <!-- optional, defaults to false -->
    <Version>3</Version> <!-- optional, defaults to 3-->
    <Authentication>simple</Authentication> <!-- optional, only simple supported -->
    <ConnectionProvider>jndi|unboundid</ConnectionProvider> <!-- required -->
    <ServerSetType>single|round robin|failover</ServerSetType> <!-- not applicable for jndi -->
    <!-- If using a custom LDAP provider, the fully qualified class: -->
    <LdapConnectorClass>com.custom.ldap.MyProvider</LdapConnectorClass>
  </Connection>
  <ConnectPool enabled="true"> <!-- enabled is optional, defaults to true -->
    <Timeout>30000</Timeout> <!-- optional, in milliseconds; if not set, no timeout -->
    <Maxsize>50</Maxsize> <!-- optional; if not set, no max connections -->
    <Prefsize>30</Prefsize> <!-- optional; if not set, no pref size -->
    <Initsize></Initsize> <!-- optional; if not set, defaults to 1 -->
    <Protocol></Protocol> <!-- optional; if not set, defaults to 'ssl plain' -->
  </ConnectPool>
  <Admin>
    <DN>cn=manager,dc=apigee,dc=com</DN>
    <Password>secret</Password>
  </Admin>
</LdapResource>

curl 示例:创建 LDAP 资源

以下示例创建了一个名为 ldap1 的 LDAP 资源。

curl -X POST -H "Content-Type: application/xml" \
  https://api.enterprise.apigee.com/v1/organizations/myorg/environments/test/ldapresources \
  -u apigee_email:password -d \
  '<LdapResource name="ldap1">
    <Connection>
      <Hosts>
      <Host>foo.com</Host>
      </Hosts>
      <SSLEnabled>false</SSLEnabled>
      <Version>3</Version>
      <Authentication>simple</Authentication>
      <ConnectionProvider>unboundid</ConnectionProvider>
      <ServerSetType>round robin</ServerSetType>
    </Connection>
    <ConnectPool enabled="true">
      <Timeout>30000</Timeout>
      <Maxsize>50</Maxsize>
      <Prefsize>30</Prefsize>
      <Initsize></Initsize>
      <Protocol></Protocol>
    </ConnectPool>
    <Admin>
      <DN>cn=manager,dc=apigee,dc=com</DN>
      <Password>secret</Password>
    </Admin>
  </LdapResource>'

响应代码

以下是政策针对成功或失败返回的 HTML 响应代码:

  • 成功: 200
  • 失败: 401

在适用于私有云的 Edge 中使用自定义 LDAP 提供方

使用自定义 LDAP 提供方

适用于私有云的 Apigee Edge 附带一个已配置为 与 LDAP 政策交互的 LDAP 提供方。不过,如果您使用的是自定义 LDAP 提供方,则必须启用 该提供方以支持 LDAP 政策。为此,请执行以下操作:

  1. 在 LDAP 提供方类中,实现 ExternalLdapConProvider 接口。
    public interface ExternalLdapConProvider {
      void doAuthentication(LdapBean LlapBean, String userDN, String password, String baseDN);
    
      void doSearchAndAuthentication(LdapBean LlapBean, String password, String baseDN, String query, int scope);
    
      Collection<Map<String, String[]>> doSearch(LdapBean LlapBean, String query,
        String baseDN, Collection<String> requiredAttributes, int scope);
    
      void closeConnections();
    }
  2. 在政策配置的 <LdapConnectorClass>(下一部分)中, 添加自定义 LDAP 提供方的完全限定类名称。
  3. 下载此文件:custom-ldap.jar_.zip。 (您可能需要右键点击并选择另存为 。)
  4. 将其解压缩。
  5. 将 custom-ldap.jar 文件添加到您的环境,并确保该文件位于类路径中。
  6. 为 LDAP 提供方创建环境资源。您将在 LDAP 政策的 <LdapResource> 元素中使用环境资源 名称。

使用 UnboundID LDAP SDK for Java

您可以将 UnboundID LDAP SDK 与 LDAP 政策搭配使用,但必须先下载版本 2.3.1 并将其添加到每个消息处理器的类路径中。

如需将 UnboundID LDAP SDK 与 LDAP 政策搭配使用,请执行以下操作:

  1. 打开浏览器,然后前往 UnboundID LDAP SDK 的 Sourceforge 文件代码库:
    https://sourceforge.net/projects/ldap-sdk/files/
  2. 找到 SDK 的 2.3.1 版(SE 或 Standard Edition),然后下载该版本的 ZIP 文件 。例如,下载“unboundid-ldapsdk-2.3.1-se.zip”。
  3. 从 SDK ZIP 文件中提取 JAR 文件,如以下示例所示:
    unzip -j -d ~/tmp ~/Downloads/unboundid-ldapsdk-2.3.1-se.zip unboundid-ldapsdk-2.3.1-se/unboundid-ldapsdk-se.jar

    此命令仅将 JAR 文件提取到 ~/tmp 目录。它使用 -j 舍弃目录 结构,不过这是可选的。

  4. 在每个消息处理器节点上
    1. 将 JAR 文件复制到消息处理器的 /opt/apigee/edge-gateway/lib/thirdparty 目录。
    2. 如有必要,请向 Apigee 用户授予 JAR 文件的权限,以便消息处理器可以访问该文件。
    3. Edge 会将 /opt/apigee/edge-gateway/lib/thirdparty 目录中的所有第三方库添加到类路径。

    4. 重启消息处理器:
      /opt/apigee/apigee-service/bin/apigee-service edge-message-processor restart

流变量

以下是由 SearchQuery 填充的 LDAP 政策变量。

变量

说明

ldap.policyName.execution.success

执行政策后,此流变量包含“true”或“false”值, 具体取决于结果。

ldap.policyName.search.result[index].
  attribute.attrName[index]=value

此变量的灵活格式(尤其是索引)考虑了多个属性以及具有多个值的属性。索引是一个从 1 开始的数字。如果未提供索引号,则默认 索引号为 1。

如果政策返回地址、电话号码和电子邮件地址,您可以使用以下变量检索第一个属性 和值:

ldap.policyName.search.result.attribute.address
ldap.policyName.search.result.attribute.phone
ldap.policyName.search.result.attribute.email

如果您想检索搜索结果中的第三个地址属性, 可以使用以下变量:

ldap.policyName.search.result[3].attribute.address

如果某个属性有多个值(例如,如果用户有多个电子邮件 地址),您可以按如下方式从结果中检索第二个电子邮件地址:

ldap.policyName.search.result.attribute.mail[2]

错误代码

Edge 政策返回的错误采用一致的格式,如 错误代码参考 中所述。

此政策使用以下错误代码:

错误代码 讯息
InvalidAttributeName Invalid attribute name {0}.
InvalidSearchBase Search base can not be empty.
InvalidValueForPassword Invalid value for password field. It can not be empty.
InvalidSearchScope Invalid scope {0}. Allowed scopes are {1}.
InvalidUserCredentials Invalid user credentials.
InvalidExternalLdapReference Invalid external ldap reference {0}.
LdapResourceNotFound Ldap resource {0} not found.
BaseDNRequired Base DN required.
OnlyReferenceOrValueIsAllowed Only value or reference is allowed for {0}.
AttributesRequired At least one attribute required for search action.
UserNameIsNull User name is null.
SearchQueryAndUserNameCannotBePresent Both search query and username can not be present in the authentication action. Please specify either one of them.