diff --git a/integration/spring-security/src/main/java/org/keycloak/adapters/springsecurity/AdapterDeploymentContextBean.java b/integration/spring-security/src/main/java/org/keycloak/adapters/springsecurity/AdapterDeploymentContextBean.java
index 60d413c4ad..a517416396 100644
--- a/integration/spring-security/src/main/java/org/keycloak/adapters/springsecurity/AdapterDeploymentContextBean.java
+++ b/integration/spring-security/src/main/java/org/keycloak/adapters/springsecurity/AdapterDeploymentContextBean.java
@@ -3,10 +3,7 @@ package org.keycloak.adapters.springsecurity;
import org.keycloak.adapters.AdapterDeploymentContext;
import org.keycloak.adapters.KeycloakDeployment;
import org.keycloak.adapters.KeycloakDeploymentBuilder;
-import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
-import org.springframework.context.ApplicationContext;
-import org.springframework.context.ApplicationContextAware;
import org.springframework.core.io.Resource;
import java.io.FileNotFoundException;
@@ -20,16 +17,17 @@ import java.io.IOException;
* @author Scott Rossillo
* @version $Revision: 1 $
*/
-public class AdapterDeploymentContextBean implements ApplicationContextAware, InitializingBean {
+public class AdapterDeploymentContextBean implements InitializingBean {
- private static final String KEYCLOAK_CONFIG_FILE = "keycloak.json";
- private static final String KEYCLOAK_CONFIG_WEB_RESOURCE = "WEB-INF/" + KEYCLOAK_CONFIG_FILE;
- private static final String KEYCLOAK_CONFIG_CLASSPATH_RESOURCE = "classpath:" + KEYCLOAK_CONFIG_FILE;
+ private final Resource keycloakConfigFileResource;
- private ApplicationContext applicationContext;
private AdapterDeploymentContext deploymentContext;
private KeycloakDeployment deployment;
+ public AdapterDeploymentContextBean(Resource keycloakConfigFileResource) {
+ this.keycloakConfigFileResource = keycloakConfigFileResource;
+ }
+
@Override
public void afterPropertiesSet() throws Exception {
this.deployment = loadKeycloakDeployment();
@@ -38,17 +36,12 @@ public class AdapterDeploymentContextBean implements ApplicationContextAware, In
private KeycloakDeployment loadKeycloakDeployment() throws IOException {
- Resource resource = applicationContext.getResource(KEYCLOAK_CONFIG_WEB_RESOURCE);
-
- if (!resource.isReadable()) {
- resource= applicationContext.getResource(KEYCLOAK_CONFIG_CLASSPATH_RESOURCE);
+ if (!keycloakConfigFileResource.isReadable()) {
+ throw new FileNotFoundException(String.format("Unable to locate Keycloak configuration file: %s",
+ keycloakConfigFileResource.getFilename()));
}
- if (!resource.isReadable()) {
- throw new FileNotFoundException(String.format("Unable to locate Keycloak from %s or %s", KEYCLOAK_CONFIG_WEB_RESOURCE, KEYCLOAK_CONFIG_CLASSPATH_RESOURCE));
- }
-
- return KeycloakDeploymentBuilder.build(resource.getInputStream());
+ return KeycloakDeploymentBuilder.build(keycloakConfigFileResource.getInputStream());
}
/**
@@ -68,9 +61,4 @@ public class AdapterDeploymentContextBean implements ApplicationContextAware, In
public KeycloakDeployment getDeployment() {
return deployment;
}
-
- @Override
- public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
- this.applicationContext = applicationContext;
- }
}
diff --git a/integration/spring-security/src/main/java/org/keycloak/adapters/springsecurity/config/KeycloakWebSecurityConfigurerAdapter.java b/integration/spring-security/src/main/java/org/keycloak/adapters/springsecurity/config/KeycloakWebSecurityConfigurerAdapter.java
index 6cbcbe69f8..b5ef6659e3 100644
--- a/integration/spring-security/src/main/java/org/keycloak/adapters/springsecurity/config/KeycloakWebSecurityConfigurerAdapter.java
+++ b/integration/spring-security/src/main/java/org/keycloak/adapters/springsecurity/config/KeycloakWebSecurityConfigurerAdapter.java
@@ -8,7 +8,9 @@ import org.keycloak.adapters.springsecurity.filter.KeycloakAuthenticationProcess
import org.keycloak.adapters.springsecurity.filter.KeycloakCsrfRequestMatcher;
import org.keycloak.adapters.springsecurity.filter.KeycloakPreAuthActionsFilter;
import org.keycloak.adapters.springsecurity.management.HttpSessionManager;
+import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
+import org.springframework.core.io.Resource;
import org.springframework.security.config.annotation.web.WebSecurityConfigurer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
@@ -26,19 +28,20 @@ import org.springframework.security.web.authentication.www.BasicAuthenticationFi
*
* @author Scott Rossillo
* @version $Revision: 1 $
- *
* @see EnableWebSecurity
* @see EnableWebMvcSecurity
*/
public abstract class KeycloakWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter implements WebSecurityConfigurer {
+ @Value("${keycloak.configurationFile:WEB-INF/keycloak.json}")
+ private Resource keycloakConfigFileResource;
+
@Bean
protected AdapterDeploymentContextBean adapterDeploymentContextBean() {
- return new AdapterDeploymentContextBean();
+ return new AdapterDeploymentContextBean(keycloakConfigFileResource);
}
- protected AuthenticationEntryPoint authenticationEntryPoint()
- {
+ protected AuthenticationEntryPoint authenticationEntryPoint() {
return new KeycloakAuthenticationEntryPoint();
}
@@ -48,7 +51,7 @@ public abstract class KeycloakWebSecurityConfigurerAdapter extends WebSecurityCo
@Bean
protected KeycloakAuthenticationProcessingFilter keycloakAuthenticationProcessingFilter() throws Exception {
- KeycloakAuthenticationProcessingFilter filter = new KeycloakAuthenticationProcessingFilter(authenticationManagerBean());
+ KeycloakAuthenticationProcessingFilter filter = new KeycloakAuthenticationProcessingFilter(authenticationManagerBean());
filter.setSessionAuthenticationStrategy(sessionAuthenticationStrategy());
return filter;
}
@@ -64,7 +67,7 @@ public abstract class KeycloakWebSecurityConfigurerAdapter extends WebSecurityCo
@Bean
protected HttpSessionManager httpSessionManager() {
- return new HttpSessionManager();
+ return new HttpSessionManager();
}
protected KeycloakLogoutHandler keycloakLogoutHandler() {
diff --git a/integration/spring-security/src/test/java/org/keycloak/adapters/springsecurity/AdapterDeploymentContextBeanTest.java b/integration/spring-security/src/test/java/org/keycloak/adapters/springsecurity/AdapterDeploymentContextBeanTest.java
new file mode 100644
index 0000000000..3510db7f7a
--- /dev/null
+++ b/integration/spring-security/src/test/java/org/keycloak/adapters/springsecurity/AdapterDeploymentContextBeanTest.java
@@ -0,0 +1,56 @@
+package org.keycloak.adapters.springsecurity;
+
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.springframework.core.io.ClassPathResource;
+import org.springframework.core.io.Resource;
+
+import java.io.FileNotFoundException;
+
+import static org.junit.Assert.assertNotNull;
+
+public class AdapterDeploymentContextBeanTest {
+
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+
+ private AdapterDeploymentContextBean adapterDeploymentContextBean;
+
+ @Test
+ public void should_create_deployment_and_deployment_context() throws Exception {
+
+ //given:
+ adapterDeploymentContextBean = new AdapterDeploymentContextBean(getCorrectResource());
+
+ //when:
+ adapterDeploymentContextBean.afterPropertiesSet();
+
+ //then
+ assertNotNull(adapterDeploymentContextBean.getDeployment());
+ assertNotNull(adapterDeploymentContextBean.getDeploymentContext());
+ }
+
+ private Resource getCorrectResource() {
+ return new ClassPathResource("keycloak.json");
+ }
+
+ @Test
+ public void should_throw_exception_when_configuration_file_was_not_found() throws Exception {
+
+ //given:
+ adapterDeploymentContextBean = new AdapterDeploymentContextBean(getEmptyResource());
+
+ //then:
+ expectedException.expect(FileNotFoundException.class);
+ expectedException.expectMessage("Unable to locate Keycloak configuration file: no-file.json");
+
+ //when:
+ adapterDeploymentContextBean.afterPropertiesSet();
+ }
+
+ private Resource getEmptyResource() {
+ return new ClassPathResource("no-file.json");
+ }
+}
diff --git a/integration/spring-security/src/test/resources/keycloak.json b/integration/spring-security/src/test/resources/keycloak.json
new file mode 100644
index 0000000000..61e0f9371d
--- /dev/null
+++ b/integration/spring-security/src/test/resources/keycloak.json
@@ -0,0 +1,10 @@
+{
+ "realm": "spring-security",
+ "realm-public-key": "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCh65Gqi3BSaVe12JHlqChWm8WscICrj46MVqmRoO9FCmqbxEpCQhE1RLjW+GDyc3YdXW3xqUQ3AZxDkTmN1h6BWkhdxPLzA4EnwgWmGurhyJlUF9Id2tKns0jbC+Z7kIb2LcOiKHKL7mRb3q7EtWubNnrvunv8fx+WeXGaQoGEVQIDAQAB",
+ "auth-server-url": "http://localhost:8080/auth",
+ "ssl-required": "external",
+ "resource": "some-resource",
+ "credentials": {
+ "secret": "a9c3501e-20dd-4277-8a7b-351063848446"
+ }
+}