at recent versions of Keycloak, there is DefaultKeyManager which can be used to retrieve keys from realms.
KeyManager keyManager = new DefaultKeyManager(keycloakSession);Key key = keyManager.getActiveKey(keycloakSession.getContext().getRealm(), KeyUse.SIG, "RS256").getPrivateKey();