如何使用 public 客户端发出的令牌查询 keycloak 资源权限

How to query keycloak resource permission with a token issued from a public client

我有一个受 keycloak 保护的前端 Javascript 客户端。前端应用程序的 Keycloak 客户端类型为 Public,名为 blog_gui

我还有一个 API 受名为 'blog_api' 的 Confidential 客户端保护,启用了 Authorization

当我从我的前端应用程序向 API 发出请求时,我从 public 客户端 (blog_gui) 发送由 keycloak 发出的 JWT 作为 Bearer 令牌请求的header。

在 API 中,我想检查发送 JWT 的前端应用程序是否具有对特定资源的权限。所以我在以下 URL 向 keycloak 服务器发送请求,包括来自前端应用程序的 JWT 作为承载令牌

http://${host}:${post}/auth/realms/${realm}/authz/protection/uma-policy

我从 keycloak 得到的结果是

{
    "error": "invalid_clientId",
    "error_description": "Client application [blog_gui] is not registered as a resource server."
}

在您的场景中,keycloak-js 将向 Keycloak 查询具有 audience/client blog_gui 的访问令牌。此客户端是 public,因此它不是已注册的资源服务器。 您可能想要执行 token exchange to obtain an access token for your backend client (blog_api) and use the obtained token to query the uma-policy endpoint. You can find additional info on how to query the endpoint in the Authorization Services docs over here.

确保您的 blog_api 客户端是机密的并且 Authorization Enabled 已开启。您可以参考 this documentation 如何相应地设置客户端。