5.4. 启用缓存


安全域还提供针对身份验证和组加载缓存 LDAP 查询结果的功能。这在某些情况下允许不同用户在多个搜索之间重复使用不同查询的结果,例如反复查询组的组成员资格信息。有三个不同的缓存,每个缓存都是单独配置并独立运行:

  • 身份验证
  • group-to-principal
  • username-to-dn

5.4.1. 缓存配置

尽管缓存彼此独立,但所有三个缓存的配置方式都相同。每个缓存都提供以下配置选项:

Expand
属性描述

type

这定义了缓存将遵循的驱除策略。选项为 by-access-timeby-search-timeby-access-time 在上次访问后经过了一定时间后从缓存中驱除项目。按搜索时间 根据它们在缓存中的项目的时间(不论它们上次访问权限如何)驱除项目。

eviction-time

根据策略,这定义用于驱除的时间(以秒为单位)。

cache-failures

这是一个布尔值,可启用/禁用失败搜索的缓存。这有可能防止 LDAP 服务器被同一失败搜索重复访问,但也有可能在缓存中填入缓存中搜索不存在的用户。此设置对于身份验证缓存尤为重要。

max-cache-size

这会定义缓存的最大大小(项目数),以内向中指示项目何时开始被驱除。旧项目从缓存中驱除,以便为新身份验证和搜索腾出空间,这意味着 max-cache-size 不会阻止新的身份验证尝试或搜索。

5.4.2. 示例

注意

此示例假定已创建了名为 LDAPRealm 的安全域。它连接到现有的 LDAP 服务器,并配置为进行身份验证和授权。用于显示当前 配置的命令包括在阅读当前缓存 配置中。有关创建使用 LDAP 的安全域的更多详细信息,请参阅使用传统核心管理身份验证

基本配置示例

"core-service" : {"management" : {"security-realm" : {"LDAPRealm" : {"authentication" : {
          "ldap" : {
            "allow-empty-passwords" : false,
            "base-dn" : "...", ​
            "connection" : "MyLdapConnection", ​
            "recursive" : false, ​
            "user-dn" : "dn", ​
            "username-attribute" : "uid", ​
            "cache" : null
          }
        },
        "authorization" : {
          "ldap" : {
            "connection" : "MyLdapConnection", ​
            "group-search" : {
              "group-to-principal" : {"base-dn" : "...", ​
                "group-dn-attribute" : "dn", ​
                "group-name" : "SIMPLE", ​
                "group-name-attribute" : "uid", ​
                "iterative" : true, ​
                "principal-attribute" : "uniqueMember", ​
                "search-by" : "DISTINGUISHED_NAME", ​
                "cache" : null ​
              }
            }, ​
            "username-to-dn" : {
              "username-filter" : {"attribute" : "uid", ​
                "base-dn" : "...", ​
                "force" : false, ​
                "recursive" : false, ​
                "user-dn-attribute" : "dn", ​
                "cache" : null ​
                }
              }}
          }, ​
        }}}
}

在所有出现 "cache": null 的区域中,可以配置一个缓存:

身份验证
身份验证期间,使用此定义发现用户的可分辨名称,并尝试连接到 LDAP 服务器并验证其身份是否使用这些凭据来实现。
group-search 定义
有组搜索定义。在本例中,它是迭代搜索,因为在上面的示例配置中, 被设置为 true。首先,将执行搜索以查找用户是以下直接成员的所有组:之后,将对每个组执行搜索,以确定它们是否与其他组成员资格。此过程将继续,直到检测到循环引用或最终组不属于任何其他组。
组搜索 中的用户名到dn 定义
组搜索取决于用户的可分辨名称的可用性。本节并不适用于所有情况,但可以作为第二次尝试来发现用户的可分辨名称。当支持第二种身份验证形式(如本地身份验证)时,这可能很有用,甚至也可以是必需的。

5.4.2.1. 阅读 Current Cache 配置

注意

本节和后续小节中使用的 CLI 命令使用 LDAPRealm 作为安全域的名称。这应当替换为正在配置的实际域的名称。

读取当前缓存配置的 CLI 命令

/core-service=management/security-realm=LDAPRealm:read-resource(recursive=true)

输出。

{"outcome" => "success", ​
  "result" => {"map-groups-to-roles" => true, ​
    "authentication" => {
      "ldap" => {"advanced-filter" => undefined, ​
        "allow-empty-passwords" => false, ​
        "base-dn" => "dc=example,dc=com", ​
         "connection" => "ldapConnection", ​
         "recursive" => true, ​
         "user-dn" => "dn", ​
         "username-attribute" => "uid",
         "cache" => undefined ​
        }
      }, ​
      "authorization" => {
        "ldap" => {"connection" => "ldapConnection", ​
          "group-search" => {
            "principal-to-group" => {"group-attribute" => "description", ​
              "group-dn-attribute" => "dn", ​
              "group-name" => "SIMPLE", ​
              "group-name-attribute" => "cn", ​
              "iterative" => false, ​
              "prefer-original-connection" => true, ​
              "skip-missing-groups" => false, ​
              "cache" => undefined ​
            }
          }, ​
          "username-to-dn" => {
            "username-filter" => {"attribute" => "uid", ​
              "base-dn" => "ou=Users,dc=jboss,dc=org", ​
              "force" => true, ​
              "recursive" => false, ​
              "user-dn-attribute" => "dn", ​
              "cache" => undefined ​
            }
          }}
      }, ​
      "plug-in" => undefined, ​
      "server-identity" => undefined ​
    }}

5.4.2.2. 启用缓存

注意

本节和后续小节中使用的管理 CLI 命令在安全域的身份验证部分中配置缓存,换句话说是 authentication=ldap/。授权部分中的缓存也可以通过更新命令的路径以类似的方式进行配置。

用于启用缓存的管理 CLI 命令

/core-service=management/security-realm=LDAPRealm/authentication=ldap/cache=by-access-time:add(eviction-time=300, cache-failures=true, max-cache-size=100)

这个命令为验证添加 by-access-time 缓存,驱除时间为 300 秒(5 分钟),最大缓存大小为 100 个项目。此外,还将缓存失败的搜索。另外,也可以配置 by-search-time 缓存:

/core-service=management/security-realm=LDAPRealm/authentication=ldap/cache=by-search-time:add(eviction-time=300, cache-failures=true, max-cache-size=100)

5.4.2.3. 检查现有缓存

用于内省现有缓存的管理 CLI 命令

/core-service=management/security-realm=LDAPRealm/authentication=ldap/cache=by-access-time:read-resource(include-runtime=true)

{
  "outcome" => "success",
  "result" => {
    "cache-failures" => true,
    "cache-size" => 1,
    "eviction-time" => 300,
    "max-cache-size" => 100
  }
}

include-runtime 属性添加 cache-size,它显示缓存中的当前项目数。它在上面的输出中是 1

5.4.2.4. 测试现有缓存的内容

用于测试现有缓存内容的管理 CLI 命令

/core-service=management/security-realm=LDAPRealm/authentication=ldap/cache=by-access-time:contains(name=TestUserOne)

{
  "outcome" => "success",
  "result" => true
}

这表明 TestUserOne 的条目存在于缓存中。

5.4.2.5. 清空缓存

您可以从缓存中清空单个项目,或者清空整个缓存。

用于清理单项的管理 CLI 命令

/core-service=management/security-realm=LDAPRealm/authentication=ldap/cache=by-access-time:flush-cache(name=TestUserOne)

用于查找 Entire Cache 的管理 CLI 命令

/core-service=management/security-realm=LDAPRealm/authentication=ldap/cache=by-access-time:flush-cache()

5.4.2.6. 删除缓存

用于删除缓存的管理 CLI 命令

/core-service=management/security-realm=LDAPRealm/authentication=ldap/cache=by-access-time:remove()

reload

Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

关于红帽文档

Legal Notice

Theme

© 2026 Red Hat
返回顶部