如何在 Keycloak SPI 扩展中编写简单的事务包装器?

How do I write a simple transaction wrapper in a Keycloak SPI extension?

通过遵循 this Keycloak 开发人员指南,我试图编写一个 SPI 扩展来帮助联合自定义用户服务(将其视为具有一堆处理用户数据的 CRUD REST API 的数据存储)进入钥匙斗篷。

我想实现的一个基本场景是,当新用户通过 Keycloak 登录时,her/his 需要通过创建用户条目将信息传播到自定义用户服务。

通过实现 UserRegistrationProvider 接口,我的提供程序中有以下方法。

    @Override
    public UserModel addUser(RealmModel realm, String username) {
        log.infov("Adding new user: username={0}", username);
        User user = new User();
        // do something to populate the user object with additional attributes, e.g. those edited via the Keycloak console UI
        UserModel userModel = createAdapter(realm, user);
        return userModel;
    }

奇怪的是,这个接口方法只接受字符串形式的username参数。我不知道如何访问其他用户属性。我在下面找到了两个关于这个问题的帖子。

UserRegistrationProvider value object add method

Custom federation - webservice

Keycloak团队的回复是,只有在提交事务时,才能应用事务包装器来持久化更改。 good example is the official implementation for LDAP federation. And this 是 LDAP 事务的源代码。

但是该代码部分太多了,难以阅读。如果有人能提供一种简单明了的实现方式,我们将不胜感激。

在阅读 LDAP 提供程序上的 Keycloak 源代码后,我发现数据持久化步骤可以发生在主事务之后的 customised transaction that is enlisted 中。

上面引用的存储库是我试验过的基于文件的 Keycloak 用户存储提供程序。希望它能对此提供一些见解。