首页 > 解决方案 > 如何使用 Keycloak authzClient 找到“与我共享”和“由我拥有”的所有资源?

问题描述

在 Keycloak 上,我有几个资源,我需要获取我拥有并由其他用户“与我共享”的所有资源。

例如,此资源归“test”用户所有,并与“test2”用户共享:

钥匙斗篷资源

与 test2 共享的资源

所以这个想法是 test2 将获取与他共享的资源。

但我看到的唯一选择是按所有者/名称/Uri 查找:Keycloak docs

/**
     * Query the server for any resource with the matching arguments.
     *
     * @param id the resource id
     * @param name the resource name
     * @param uri the resource uri
     * @param owner the resource owner
     * @param type the resource type
     * @param scope the resource scope
     * @param matchingUri the resource uri. Use this parameter to lookup a resource that best match the given uri
     * @param exactName if the the {@code name} provided should have a exact match   
     * @param deep if the result should be a list of resource representations with details about the resource. If false, only ids are returned
     * @param firstResult the position of the first resource to retrieve
     * @param maxResult the maximum number of resources to retrieve
     * @return a list of resource representations or an array of strings representing resource ids, depending on the generic type
     */
    public <R> R find(final String id, final String name, final String uri, final String owner, final String type, final String scope, final boolean matchingUri, final boolean exactName, final boolean deep, final Integer firstResult, final Integer maxResult)

另一方面,keycloak 帐户 UI 应用程序似乎调用 端点来获取与我共享的资源

标签: javakotlinkeycloak

解决方案


如果您向 Keycloak 发送一个空的授权请求,那么它会返回所有允许的权限以及相关资源。此列表包括您拥有或与您共享。

fun AuthzClient.findAllGrantedPermissions(accessToken: String): TokenIntrospectionResponse {
    val request = AuthorizationRequest()
    val rpt = this.authorization(accessToken).authorize(request).token
    return this.protection().introspectRequestingPartyToken(rpt)
}

推荐阅读