密钥斗篷:从自定义端点 (SPI) 发送管理事件



我有一个Keycloak扩展(自定义端点,SPI(。现在我想添加发送AdminEvents,我实现如下:

    private void logAdminEvent(ClientConnection clientConnection, UserRepresentation rep, OperationType operation, ResourceType resource) {
    RealmModel realm = session.getContext().getRealm();
    // beware: clientConnection must not be null because of missing check for NullPointer in Keycloak
    ClientModel client = realm.getClientByClientId(ROLE_ATTRIBUTE_CLIENT);
    AdminAuth adminAuth = new AdminAuth(realm, authResult.getToken(), authResult.getUser(), client);
    AdminEventBuilder adminEvent = new AdminEventBuilder(realm, adminAuth, session, clientConnection);
    adminEvent
            .operation(operation)
            .resource(resource)
            .authIpAddress(authResult.getSession().getIpAddress())
            .authClient(client)
            .resourcePath(session.getContext().getUri())
            .representation(rep);
    adminEvent
            .success();
}

我知道必须在Keycloak管理控制台中激活管理员event日志记录,我做到了。

也许登录用户没有管理权限是相关的,但是当我授予管理员权限时,它也没有工作。

我需要想法或提示来说明我在这里做错了什么。不幸的是,文档和网络研究没有帮助。

看看Keycloak的来源,尤其是像RootAdminResource这样的东西。据我所知,所有管理资源(例如 controllers ( 通过从父资源通过构造函数注入的builder克隆的builder创建events。您可能缺少一些初始化技巧。

好的,我们找到了。 首先,对于 update/delete ,我们必须将领域添加到adminEvent中。 其次,对于创建,event我们在

session.getTransactionManager().commit();

发生。在adminEvent.success()后设置提交解决了问题。

也许这可以帮助任何人。

最新更新