From c9affe95373232ab9ae8e7e18ccd2ede5f8baee5 Mon Sep 17 00:00:00 2001 From: PNV Date: Mon, 14 Feb 2022 13:16:40 +0300 Subject: [PATCH] resolved circular dependency --- pom.xml | 22 +++---- .../config/ConfigResolverConfig.java | 63 +++++++++++++++++++ .../orgmanager/config/SecurityConfig.java | 61 +----------------- .../config/properties/KeyCloakProperties.java | 3 +- .../repository/AbstractRepositoryTest.java | 2 +- 5 files changed, 79 insertions(+), 72 deletions(-) create mode 100644 src/main/java/dev/vality/orgmanager/config/ConfigResolverConfig.java diff --git a/pom.xml b/pom.xml index ca72f7c..1ebe691 100644 --- a/pom.xml +++ b/pom.xml @@ -23,21 +23,13 @@ 8023 ${server.port} ${server.rest.port} ${management.port} 5.5.2 - 14.0.0 + 17.0.0 4.14.0 1.19-8707f87-server - - - io.micrometer - micrometer-core - - - io.micrometer - micrometer-registry-prometheus - + dev.vality.woody woody-thrift @@ -166,6 +158,14 @@ + + io.micrometer + micrometer-core + + + io.micrometer + micrometer-registry-prometheus + javax.servlet javax.servlet-api @@ -228,7 +228,7 @@ org.testcontainers postgresql - 1.16.0 + 1.16.3 test diff --git a/src/main/java/dev/vality/orgmanager/config/ConfigResolverConfig.java b/src/main/java/dev/vality/orgmanager/config/ConfigResolverConfig.java new file mode 100644 index 0000000..bf7af82 --- /dev/null +++ b/src/main/java/dev/vality/orgmanager/config/ConfigResolverConfig.java @@ -0,0 +1,63 @@ +package dev.vality.orgmanager.config; + +import com.google.common.base.Strings; +import dev.vality.orgmanager.config.properties.KeyCloakProperties; +import org.keycloak.adapters.KeycloakConfigResolver; +import org.keycloak.adapters.KeycloakDeployment; +import org.keycloak.adapters.KeycloakDeploymentBuilder; +import org.keycloak.adapters.springsecurity.config.KeycloakSpringConfigResolverWrapper; +import org.keycloak.representations.adapters.config.AdapterConfig; +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.DependsOn; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.List; +import java.util.stream.Collectors; + +@Configuration +public class ConfigResolverConfig { + + @Bean + public KeycloakConfigResolver keycloakConfigResolver(KeyCloakProperties keyCloakProperties) { + return facade -> { + KeycloakDeployment deployment = KeycloakDeploymentBuilder.build(adapterConfig(keyCloakProperties)); + deployment.setNotBefore(keyCloakProperties.getNotBefore()); + return deployment; + }; + } + + private AdapterConfig adapterConfig(KeyCloakProperties keyCloakProperties) { + String keycloakRealmPublicKey; + if (!Strings.isNullOrEmpty(keyCloakProperties.getRealmPublicKeyFilePath())) { + keycloakRealmPublicKey = readKeyFromFile(keyCloakProperties.getRealmPublicKeyFilePath()); + } else { + keycloakRealmPublicKey = keyCloakProperties.getRealmPublicKey(); + } + + AdapterConfig adapterConfig = new AdapterConfig(); + adapterConfig.setRealm(keyCloakProperties.getRealm()); + adapterConfig.setRealmKey(keycloakRealmPublicKey); + adapterConfig.setResource(keyCloakProperties.getResource()); + adapterConfig.setAuthServerUrl(keyCloakProperties.getAuthServerUrl()); + adapterConfig.setUseResourceRoleMappings(true); + adapterConfig.setBearerOnly(true); + adapterConfig.setSslRequired(keyCloakProperties.getSslRequired()); + return adapterConfig; + } + + private String readKeyFromFile(String filePath) { + try { + List strings = Files.readAllLines(Paths.get(filePath)); + strings.remove(strings.size() - 1); + strings.remove(0); + + return strings.stream().map(String::trim).collect(Collectors.joining()); + } catch (IOException ex) { + throw new RuntimeException(ex); + } + } +} diff --git a/src/main/java/dev/vality/orgmanager/config/SecurityConfig.java b/src/main/java/dev/vality/orgmanager/config/SecurityConfig.java index 7d9f354..302fdeb 100644 --- a/src/main/java/dev/vality/orgmanager/config/SecurityConfig.java +++ b/src/main/java/dev/vality/orgmanager/config/SecurityConfig.java @@ -1,20 +1,11 @@ package dev.vality.orgmanager.config; -import com.google.common.base.Strings; -import dev.vality.orgmanager.config.properties.KeyCloakProperties; -import org.keycloak.adapters.KeycloakConfigResolver; -import org.keycloak.adapters.KeycloakDeployment; -import org.keycloak.adapters.KeycloakDeploymentBuilder; import org.keycloak.adapters.springsecurity.KeycloakSecurityComponents; import org.keycloak.adapters.springsecurity.config.KeycloakWebSecurityConfigurerAdapter; import org.keycloak.adapters.springsecurity.management.HttpSessionManager; -import org.keycloak.representations.adapters.config.AdapterConfig; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.FilterType; +import org.springframework.context.annotation.*; import org.springframework.http.HttpMethod; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; @@ -26,12 +17,6 @@ import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.CorsConfigurationSource; import org.springframework.web.cors.UrlBasedCorsConfigurationSource; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.List; -import java.util.stream.Collectors; - @Configuration @EnableWebSecurity @ComponentScan( @@ -43,11 +28,9 @@ import java.util.stream.Collectors; ) @EnableGlobalMethodSecurity(prePostEnabled = true, proxyTargetClass = true) @ConditionalOnProperty(value = "auth.enabled", havingValue = "true") +@DependsOn("configResolverConfig") public class SecurityConfig extends KeycloakWebSecurityConfigurerAdapter { - @Autowired - private KeyCloakProperties keyCloakProperties; - @Override protected HttpSessionManager httpSessionManager() { return super.httpSessionManager(); @@ -77,15 +60,6 @@ public class SecurityConfig extends KeycloakWebSecurityConfigurerAdapter { auth.authenticationProvider(keycloakAuthenticationProvider()); } - @Bean - public KeycloakConfigResolver keycloakConfigResolver() { - return facade -> { - KeycloakDeployment deployment = KeycloakDeploymentBuilder.build(adapterConfig()); - deployment.setNotBefore(keyCloakProperties.getNotBefore()); - return deployment; - }; - } - @Bean public CorsConfigurationSource corsConfigurationSource() { CorsConfiguration configuration = new CorsConfiguration(); @@ -97,35 +71,4 @@ public class SecurityConfig extends KeycloakWebSecurityConfigurerAdapter { source.registerCorsConfiguration("/**", configuration); return source; } - - private AdapterConfig adapterConfig() { - String keycloakRealmPublicKey; - if (!Strings.isNullOrEmpty(keyCloakProperties.getRealmPublicKeyFilePath())) { - keycloakRealmPublicKey = readKeyFromFile(keyCloakProperties.getRealmPublicKeyFilePath()); - } else { - keycloakRealmPublicKey = keyCloakProperties.getRealmPublicKey(); - } - - AdapterConfig adapterConfig = new AdapterConfig(); - adapterConfig.setRealm(keyCloakProperties.getRealm()); - adapterConfig.setRealmKey(keycloakRealmPublicKey); - adapterConfig.setResource(keyCloakProperties.getResource()); - adapterConfig.setAuthServerUrl(keyCloakProperties.getAuthServerUrl()); - adapterConfig.setUseResourceRoleMappings(true); - adapterConfig.setBearerOnly(true); - adapterConfig.setSslRequired(keyCloakProperties.getSslRequired()); - return adapterConfig; - } - - private String readKeyFromFile(String filePath) { - try { - List strings = Files.readAllLines(Paths.get(filePath)); - strings.remove(strings.size() - 1); - strings.remove(0); - - return strings.stream().map(String::trim).collect(Collectors.joining()); - } catch (IOException ex) { - throw new RuntimeException(ex); - } - } } diff --git a/src/main/java/dev/vality/orgmanager/config/properties/KeyCloakProperties.java b/src/main/java/dev/vality/orgmanager/config/properties/KeyCloakProperties.java index e991b7e..a042c53 100644 --- a/src/main/java/dev/vality/orgmanager/config/properties/KeyCloakProperties.java +++ b/src/main/java/dev/vality/orgmanager/config/properties/KeyCloakProperties.java @@ -3,8 +3,9 @@ package dev.vality.orgmanager.config.properties; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; +import org.springframework.stereotype.Component; -@Configuration +@Component @ConfigurationProperties(prefix = "keycloak") @Data public class KeyCloakProperties { diff --git a/src/test/java/dev/vality/orgmanager/repository/AbstractRepositoryTest.java b/src/test/java/dev/vality/orgmanager/repository/AbstractRepositoryTest.java index 8ba9704..d27f53c 100644 --- a/src/test/java/dev/vality/orgmanager/repository/AbstractRepositoryTest.java +++ b/src/test/java/dev/vality/orgmanager/repository/AbstractRepositoryTest.java @@ -56,7 +56,7 @@ public abstract class AbstractRepositoryTest { @ClassRule @SuppressWarnings("rawtypes") - public static PostgreSQLContainer postgres = new PostgreSQLContainer<>("postgres:9.6") + public static PostgreSQLContainer postgres = new PostgreSQLContainer<>("postgres:13.6") .withStartupTimeout(Duration.ofMinutes(5)); public static class Initializer implements ApplicationContextInitializer {