使用 Keycloak 的自定义身份验证

Custom Authentication using Keycloak

过去几周我一直在努力掌握 Keycloak,因此我可以实现一种使用旧版提供程序(基于 Oracle,具有 table 会话和各种奇怪的东西)。我们计划在不久的将来摆脱它,但现在我们必须处理它,所以我们的想法是在前线使用 Keycloak - 利用它提供的主要好处,如 SSO - 从需要身份验证的应用程序。

我阅读了一些有关构建自定义 OIDC 身份提供者的内容,但对于这种简单的身份验证方法交换来说,它看起来很麻烦。

是否有更简单的方法(而不是新的 OIDC 提供程序)使用 Keycloak 构建自定义身份验证提供程序?如果是这样,你能给我举个例子或者至少更深入的解释吗? 我发现 Keycloak 文档在实际示例方面非常薄弱,这增加了我对一般身份验证协议(我已经在研究)的知识的缺乏。

提前致谢。

这也是我的组织采取的措施。这充其量是棘手的。

这个用户存储SPI就是我们走的路线。我们首先实现了一种基本方法,通过 API 调用从远程数据库读取数据。在我们的例子中,我们有两个数据库要连接,我们的 SPI 实现将调用这两个数据库并统一它们提供给用户的任何数据。

https://www.keycloak.org/docs/latest/server_development/index.html#_user-storage-spi

You can use the User Storage SPI to write extensions to Keycloak to connect to external user databases and credential stores. The built-in LDAP and ActiveDirectory support is an implementation of this SPI in action. Out of the box, Keycloak uses its local database to create, update, and look up users and validate credentials. Often though, organizations have existing external proprietary user databases that they cannot migrate to Keycloak’s data model. For those situations, application developers can write implementations of the User Storage SPI to bridge the external user store and the internal user object model that Keycloak uses to log in users and manage them.

然后在本节中:https://www.keycloak.org/docs/latest/server_development/index.html#provider-capability-interfaces

If you have examined the UserStorageProvider interface closely you might notice that it does not define any methods for locating or managing users. These methods are actually defined in other capability interfaces depending on what scope of capabilities your external user store can provide and execute on. For example, some external stores are read-only and can only do simple queries and credential validation. You will only be required to implement the capability interfaces for the features you are able to. You can implement these interfaces:

该页面后面列出了各种其他接口,您可以根据需要实施这些接口以提供所需的行为。

org.keycloak.storage.user.UserLookupProvider

This interface is required if you want to be able to log in with users from this external store. Most (all?) providers implement this interface.

org.keycloak.storage.user.UserQueryProvider

Defines complex queries that are used to locate one or more users. You must implement this interface if you want to view and manage users from the administration console.

org.keycloak.storage.user.UserRegistrationProvider

Implement this interface if your provider supports adding and removing users.

org.keycloak.storage.user.UserBulkUpdateProvider

Implement this interface if your provider supports bulk update of a set of users.

org.keycloak.credential.CredentialInputValidator

Implement this interface if your provider can validate one or more different credential types (for example, if your provider can validate a password).

org.keycloak.credential.CredentialInputUpdater

Implement this interface if your provider supports updating one or more different credential types.

建议:从他们的 github 存储库中克隆 keycloak 源代码,以便更好地理解他们的代码以及您的实现将如何与他们现有的框架交互。这主要在调试或计算 "what the hell is happening that my code doesn't work the way I think it should" 时很有用。通常这是因为您的实现使用方式不同或者您没有考虑缓存。克隆源代码很好,因为它还提供了您可以在示例文件夹中找到的一些基本实现的有价值的示例。

这是我现在可以提供的最佳建议。这是本机 LDAP 提供程序实现:

https://github.com/keycloak/keycloak/blob/master/federation/ldap/src/main/java/org/keycloak/storage/ldap/LDAPStorageProvider.java

可以作为参考。

免责声明:我们在 Keycloak 3.4 版中进行了这项工作。文档略有不同,当时还有另一个示例直接在源代码中完成。最新的master(版本8)在界面层面看起来差不多。

在高层次上,工作看起来像这样:

  1. 实现用户存储提供接口。
  2. 创建一个新的身份验证器,或覆盖现有的用户名身份验证器,以在您的上述用户存储提供商提供其自定义功能后自定义任何其他行为。

如果仅实施步骤 1 即可达到您想要的效果,则您可能不需要步骤 2。第 1 步提取您的自定义数据库后端并读取用户信息,并将其作为 UserModel 返回给 Keycloak,它将导入到其本地数据库中供以后使用。