diff --git a/oauth-rest/oauth-authorization-server/src/main/java/com/baeldung/auth/config/EmbeddedKeycloakApplication.java b/oauth-rest/oauth-authorization-server/src/main/java/com/baeldung/auth/config/EmbeddedKeycloakApplication.java index 28adb912..2a5850cc 100644 --- a/oauth-rest/oauth-authorization-server/src/main/java/com/baeldung/auth/config/EmbeddedKeycloakApplication.java +++ b/oauth-rest/oauth-authorization-server/src/main/java/com/baeldung/auth/config/EmbeddedKeycloakApplication.java @@ -1,10 +1,14 @@ package com.baeldung.auth.config; +import java.nio.charset.StandardCharsets; import java.util.NoSuchElementException; import org.keycloak.Config; +import org.keycloak.common.util.StringPropertyReplacer; +import org.keycloak.common.util.SystemEnvProperties; import org.keycloak.exportimport.ExportImportManager; import org.keycloak.models.KeycloakSession; +import org.keycloak.models.RealmModel; import org.keycloak.representations.idm.RealmRepresentation; import org.keycloak.services.managers.ApplianceBootstrap; import org.keycloak.services.managers.RealmManager; @@ -65,12 +69,18 @@ private void createBaeldungRealm() { session.getTransactionManager().begin(); RealmManager manager = new RealmManager(session); - Resource lessonRealmImportFile = new ClassPathResource(keycloakServerProperties.getRealmImportFile()); - - manager.importRealm( - JsonSerialization.readValue(lessonRealmImportFile.getInputStream(), RealmRepresentation.class)); - - session.getTransactionManager().commit(); + RealmModel baeldung = manager.getRealmByName("baeldung"); + if (baeldung == null) { // maybe already exist realm if using external DB, skip importing. + Resource lessonRealmImportFile = new ClassPathResource(keycloakServerProperties.getRealmImportFile()); + // Read realm to string + String stringRealm = lessonRealmImportFile.getContentAsString(StandardCharsets.UTF_8); + // replace ${name} to ENV values in realm + String realmWithEnv = StringPropertyReplacer.replaceProperties(stringRealm, new SystemEnvProperties(System.getenv())); + RealmRepresentation realmRepresentation = JsonSerialization.readValue(realmWithEnv, RealmRepresentation.class); + manager.importRealm( + realmRepresentation); + session.getTransactionManager().commit(); + } } catch (Exception ex) { LOG.warn("Failed to import Realm json file: {}", ex.getMessage()); session.getTransactionManager().rollback(); diff --git a/oauth-rest/oauth-authorization-server/src/main/java/com/baeldung/auth/config/RegularJsonConfigProviderFactory.java b/oauth-rest/oauth-authorization-server/src/main/java/com/baeldung/auth/config/RegularJsonConfigProviderFactory.java index 1e955fc3..a4081588 100644 --- a/oauth-rest/oauth-authorization-server/src/main/java/com/baeldung/auth/config/RegularJsonConfigProviderFactory.java +++ b/oauth-rest/oauth-authorization-server/src/main/java/com/baeldung/auth/config/RegularJsonConfigProviderFactory.java @@ -3,5 +3,8 @@ import org.keycloak.services.util.JsonConfigProviderFactory; public class RegularJsonConfigProviderFactory extends JsonConfigProviderFactory { - + @Override + protected Properties getProperties() { + return new SystemEnvProperties(System.getenv()); + } }