您正在查看 Apigee Edge 文档。
前往 Apigee X 文档。 信息
内容
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 政策元素和属性的说明。
|
元素 |
说明 |
|---|---|
|
|
具有 name 属性的父元素,用于输入政策名称。 |
|
|
将 LDAP 政策与自定义 LDAP 提供程序(非 Apigee 提供)搭配使用时,请指定完全限定的 LDAP 连接器类。
这是您实现 Apigee 的 |
|
|
输入 LDAP 资源的环境名称。如需了解详情,请参阅创建 LDAP 资源。 |
|
|
所有数据所在的 LDAP 基本级别。例如,在 Apigee 的 LDAP 提供程序中,所有数据都位于
|
|
|
|
|
Authentication |
|
|
|
您实现的身份验证行为的父元素。 |
|
|
空元素,可采用以下属性之一:
如果您未使用用户名进行身份验证,或者请求中未包含用户名,则无需添加此元素。 如果请求中包含用户名,但您想使用电子邮件等非用户名 DN 属性对用户进行身份验证,请添加 |
|
|
空元素,可采用以下属性之一:
|
|
|
如果您想使用用户名以外的 DN 属性(例如电子邮件地址)进行身份验证,请配置 LDAP 政策以从请求(例如用户名)中获取 DN 属性,该属性用于在 LDAP 中识别用户、检索电子邮件地址并对用户进行身份验证。 例如,假设 LDAP 定义了一个用于存储电子邮件地址的“mail”属性:
|
|
搜索 |
|
|
|
您实现的搜索行为的父元素。 |
|
|
通过在请求或响应中使用元数据来标识用户,您可以使用此元素从 LDAP 中检索用户的其他 DN 属性。例如,如果请求包含用户电子邮件地址,并且您的 LDAP 定义了用于存储用户电子邮件地址的
此查询会在 LDAP 中搜索与请求中的电子邮件地址匹配的电子邮件地址,现在,政策可以使用 Attributes 元素检索该用户的其他 DN 属性。 |
|
|
使用一个或多个 例如,在 属性值是 LDAP 中定义的 DN 属性名称。 <Attributes> <Attribute>address</Attribute> <Attribute>phone</Attribute> <Attribute>title</Attribute> </Attributes> |
使用说明
借助 Apigee Edge for Private Cloud,您可以在 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 for Private Cloud 中使用自定义 LDAP 提供程序
使用自定义 LDAP 提供程序
Apigee Edge for Private Cloud 随附一个已配置为与 LDAP 政策互动的 LDAP 提供程序。不过,如果您使用的是自定义 LDAP 提供方,则必须启用该提供方以支持 LDAP 政策。具体操作步骤如下:
- 在 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(); } - 在政策配置的
<LdapConnectorClass>(下一部分)中,添加自定义 LDAP 提供程序的完全限定类名称。 - 下载以下文件:custom-ldap.jar_.zip。 (您可能需要右键点击并选择另存为。)
- 解压缩。
- 将 custom-ldap.jar 文件添加到您的环境中,并确保该文件位于您的类路径中。
- 为 LDAP 提供方创建环境资源。您将在 LDAP 政策的
<LdapResource>元素中使用环境资源名称。
使用 UnboundID LDAP SDK for Java
您可以将 UnboundID LDAP SDK 与 LDAP 政策搭配使用,但必须先下载版本 2.3.1 并将其添加到每个消息处理器的类路径中。
如需将 UnboundID LDAP SDK 与 LDAP 政策搭配使用,请执行以下操作:
- 打开浏览器,然后前往 UnboundID LDAP SDK 的 Sourceforge 文件库:
https://sourceforge.net/projects/ldap-sdk/files/
- 找到 SDK 的 2.3.1 版(SE 或标准版),然后下载该版本的 ZIP 文件。例如,下载“unboundid-ldapsdk-2.3.1-se.zip”。
- 从 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的目录结构,不过这是可选的。 - 在每个消息处理器节点上:
- 将 JAR 文件复制到消息处理器的
/opt/apigee/edge-gateway/lib/thirdparty目录中。 - 如有必要,请向 Apigee 用户授予对 JAR 文件的权限,以便消息处理器可以访问该文件。
- 重启消息处理器:
/opt/apigee/apigee-service/bin/apigee-service edge-message-processor restart
Edge 会将
/opt/apigee/edge-gateway/lib/thirdparty目录中的所有第三方库添加到类路径中。 - 将 JAR 文件复制到消息处理器的
流变量
以下是由 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. |