resolved circular dependency

This commit is contained in:
PNV 2022-02-14 13:16:40 +03:00
parent ca759261d1
commit c9affe9537
5 changed files with 79 additions and 72 deletions

22
pom.xml
View File

@ -23,21 +23,13 @@
<management.port>8023</management.port>
<exposed.ports>${server.port} ${server.rest.port} ${management.port}</exposed.ports>
<spring-security.version>5.5.2</spring-security.version>
<keycloak.version>14.0.0</keycloak.version>
<keycloak.version>17.0.0</keycloak.version>
<schedlock.version>4.14.0</schedlock.version>
<swag.organizations.version>1.19-8707f87-server</swag.organizations.version>
</properties>
<dependencies>
<!--rbkmoney-->
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-core</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
<!--vality-->
<dependency>
<groupId>dev.vality.woody</groupId>
<artifactId>woody-thrift</artifactId>
@ -166,6 +158,14 @@
<!--third party-->
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-core</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
@ -228,7 +228,7 @@
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<version>1.16.0</version>
<version>1.16.3</version>
<scope>test</scope>
<exclusions>
<exclusion>

View File

@ -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<String> 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);
}
}
}

View File

@ -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<String> 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);
}
}
}

View File

@ -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 {

View File

@ -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<ConfigurableApplicationContext> {