Keycloak public 客户端和授权
Keycloak public client and authorization
我们将 keycloak-adapter 与 Jetty 结合使用,以使用 Keycloak 进行身份验证和授权。
根据 Keycloak doc for OIDC Auth flow:
Another important aspect of this flow is the concept of a public vs. a confidential client. Confidential clients are required to
provide a client secret when they exchange the temporary codes for
tokens. Public clients are not required to provide this client secret.
Public clients are perfectly fine so long as HTTPS is strictly
enforced and you are very strict about what redirect URIs are
registered for the client.
HTML5/JavaScript clients always have to be public clients because
there is no way to transmit the client secret to them in a secure
manner.
我们有连接到 Jetty 并使用身份验证的网络应用程序。因此,我们创建了一个 public 客户端,它非常适合 webapp/REST 身份验证。
问题是一旦我们启用授权,客户端类型就会从 Public 转换为机密,并且不允许将其重置为 Public。现在,我们在喝汤。由于授权,我们无法拥有 public 个客户端,我们无法将 webapps 连接到机密客户端。
这在我们看来似乎是矛盾的。知道为什么客户需要保密授权吗?对此有何帮助,我们如何解决这个问题?
谢谢
据我了解,您的前端和后端应用程序是分开的。如果你的前端是静态的 web-app 并且没有由同一个后端应用程序(服务器)提供服务,并且你的后端是一个简单的 REST API - 那么你将配置两个 Keycloak 客户端:
public
前端应用程序的客户端。它将负责获取 JWT 令牌。
bearer-only
客户端,将附加到您的后端应用程序。
要启用授权,您将创建角色(领域或客户端范围,从领域级别开始,因为它更容易理解)。然后每个用户都会在 Keycloak 管理员 UI 中分配一个 role/s。基于此,您应该配置您的 keycloak 适配器配置(在后端)。
综合考虑,为了与您的 REST API 对话,您需要将 JWT 令牌附加到授权 header 中的每个 HTTP 请求。根据您的前端框架,您可以使用以下任一方法:
- Keycloak js adapter
- 其他绑定(angular, react)
P.S. For debugging I have just written a CLI tool called brauzie
that would help you fetch and analyse your JWT tokens (scopes, roles, etc.). It could be used for both public and confidential clients. You
could as well use Postman and https://jwt.io
HTH:)
我认为您指的是创建客户端时 Keycloak 管理控制台中的 "Authorization Enabled" 开关。如果您查看标签旁边的问号,您会看到提示“Enable/Disable 对客户端的细粒度授权支持。
Create client in Keycloak admin console (v 6.0.1)
这意味着当您为充当资源服务器的后端应用程序创建客户端时。那样的话,客户会保密的。
如果您想为前端应用程序创建客户端,以验证用户身份并获取 JWT,那么您不需要这个。
另请参阅:https://www.keycloak.org/docs/latest/authorization_services/index.html
经过深思熟虑,我们发现 public 客户端在连接时并不需要启用授权。当任何请求到达 public 客户端时,它只执行身份验证部分。当实际请求使用机密客户端(因为 Jetty 了解其中配置的机密客户端)到达资源服务器(在我们的例子中是 Jetty)时,授权部分就完成了。
我们将 keycloak-adapter 与 Jetty 结合使用,以使用 Keycloak 进行身份验证和授权。 根据 Keycloak doc for OIDC Auth flow:
Another important aspect of this flow is the concept of a public vs. a confidential client. Confidential clients are required to provide a client secret when they exchange the temporary codes for tokens. Public clients are not required to provide this client secret. Public clients are perfectly fine so long as HTTPS is strictly enforced and you are very strict about what redirect URIs are registered for the client.
HTML5/JavaScript clients always have to be public clients because there is no way to transmit the client secret to them in a secure manner.
我们有连接到 Jetty 并使用身份验证的网络应用程序。因此,我们创建了一个 public 客户端,它非常适合 webapp/REST 身份验证。
问题是一旦我们启用授权,客户端类型就会从 Public 转换为机密,并且不允许将其重置为 Public。现在,我们在喝汤。由于授权,我们无法拥有 public 个客户端,我们无法将 webapps 连接到机密客户端。
这在我们看来似乎是矛盾的。知道为什么客户需要保密授权吗?对此有何帮助,我们如何解决这个问题?
谢谢
据我了解,您的前端和后端应用程序是分开的。如果你的前端是静态的 web-app 并且没有由同一个后端应用程序(服务器)提供服务,并且你的后端是一个简单的 REST API - 那么你将配置两个 Keycloak 客户端:
public
前端应用程序的客户端。它将负责获取 JWT 令牌。bearer-only
客户端,将附加到您的后端应用程序。
要启用授权,您将创建角色(领域或客户端范围,从领域级别开始,因为它更容易理解)。然后每个用户都会在 Keycloak 管理员 UI 中分配一个 role/s。基于此,您应该配置您的 keycloak 适配器配置(在后端)。
综合考虑,为了与您的 REST API 对话,您需要将 JWT 令牌附加到授权 header 中的每个 HTTP 请求。根据您的前端框架,您可以使用以下任一方法:
- Keycloak js adapter
- 其他绑定(angular, react)
P.S. For debugging I have just written a CLI tool called brauzie that would help you fetch and analyse your JWT tokens (scopes, roles, etc.). It could be used for both public and confidential clients. You could as well use Postman and https://jwt.io
HTH:)
我认为您指的是创建客户端时 Keycloak 管理控制台中的 "Authorization Enabled" 开关。如果您查看标签旁边的问号,您会看到提示“Enable/Disable 对客户端的细粒度授权支持。
Create client in Keycloak admin console (v 6.0.1)
这意味着当您为充当资源服务器的后端应用程序创建客户端时。那样的话,客户会保密的。
如果您想为前端应用程序创建客户端,以验证用户身份并获取 JWT,那么您不需要这个。
另请参阅:https://www.keycloak.org/docs/latest/authorization_services/index.html
经过深思熟虑,我们发现 public 客户端在连接时并不需要启用授权。当任何请求到达 public 客户端时,它只执行身份验证部分。当实际请求使用机密客户端(因为 Jetty 了解其中配置的机密客户端)到达资源服务器(在我们的例子中是 Jetty)时,授权部分就完成了。