Merge pull request #1780 from agolPL/master

extract hardcoded keycloak configuration file name
This commit is contained in:
Bill Burke 2015-11-02 11:51:04 -05:00
commit d46b4eb5fe
4 changed files with 85 additions and 28 deletions

View file

@ -3,10 +3,7 @@ package org.keycloak.adapters.springsecurity;
import org.keycloak.adapters.AdapterDeploymentContext; import org.keycloak.adapters.AdapterDeploymentContext;
import org.keycloak.adapters.KeycloakDeployment; import org.keycloak.adapters.KeycloakDeployment;
import org.keycloak.adapters.KeycloakDeploymentBuilder; import org.keycloak.adapters.KeycloakDeploymentBuilder;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
@ -20,16 +17,17 @@ import java.io.IOException;
* @author <a href="mailto:srossillo@smartling.com">Scott Rossillo</a> * @author <a href="mailto:srossillo@smartling.com">Scott Rossillo</a>
* @version $Revision: 1 $ * @version $Revision: 1 $
*/ */
public class AdapterDeploymentContextBean implements ApplicationContextAware, InitializingBean { public class AdapterDeploymentContextBean implements InitializingBean {
private static final String KEYCLOAK_CONFIG_FILE = "keycloak.json"; private final Resource keycloakConfigFileResource;
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 ApplicationContext applicationContext;
private AdapterDeploymentContext deploymentContext; private AdapterDeploymentContext deploymentContext;
private KeycloakDeployment deployment; private KeycloakDeployment deployment;
public AdapterDeploymentContextBean(Resource keycloakConfigFileResource) {
this.keycloakConfigFileResource = keycloakConfigFileResource;
}
@Override @Override
public void afterPropertiesSet() throws Exception { public void afterPropertiesSet() throws Exception {
this.deployment = loadKeycloakDeployment(); this.deployment = loadKeycloakDeployment();
@ -38,17 +36,12 @@ public class AdapterDeploymentContextBean implements ApplicationContextAware, In
private KeycloakDeployment loadKeycloakDeployment() throws IOException { private KeycloakDeployment loadKeycloakDeployment() throws IOException {
Resource resource = applicationContext.getResource(KEYCLOAK_CONFIG_WEB_RESOURCE); if (!keycloakConfigFileResource.isReadable()) {
throw new FileNotFoundException(String.format("Unable to locate Keycloak configuration file: %s",
if (!resource.isReadable()) { keycloakConfigFileResource.getFilename()));
resource= applicationContext.getResource(KEYCLOAK_CONFIG_CLASSPATH_RESOURCE);
} }
if (!resource.isReadable()) { return KeycloakDeploymentBuilder.build(keycloakConfigFileResource.getInputStream());
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());
} }
/** /**
@ -68,9 +61,4 @@ public class AdapterDeploymentContextBean implements ApplicationContextAware, In
public KeycloakDeployment getDeployment() { public KeycloakDeployment getDeployment() {
return deployment; return deployment;
} }
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
} }

View file

@ -8,7 +8,9 @@ import org.keycloak.adapters.springsecurity.filter.KeycloakAuthenticationProcess
import org.keycloak.adapters.springsecurity.filter.KeycloakCsrfRequestMatcher; import org.keycloak.adapters.springsecurity.filter.KeycloakCsrfRequestMatcher;
import org.keycloak.adapters.springsecurity.filter.KeycloakPreAuthActionsFilter; import org.keycloak.adapters.springsecurity.filter.KeycloakPreAuthActionsFilter;
import org.keycloak.adapters.springsecurity.management.HttpSessionManager; import org.keycloak.adapters.springsecurity.management.HttpSessionManager;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean; 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.WebSecurityConfigurer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity; import org.springframework.security.config.annotation.web.builders.WebSecurity;
@ -26,19 +28,20 @@ import org.springframework.security.web.authentication.www.BasicAuthenticationFi
* *
* @author <a href="mailto:srossillo@smartling.com">Scott Rossillo</a> * @author <a href="mailto:srossillo@smartling.com">Scott Rossillo</a>
* @version $Revision: 1 $ * @version $Revision: 1 $
*
* @see EnableWebSecurity * @see EnableWebSecurity
* @see EnableWebMvcSecurity * @see EnableWebMvcSecurity
*/ */
public abstract class KeycloakWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter implements WebSecurityConfigurer<WebSecurity> { public abstract class KeycloakWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter implements WebSecurityConfigurer<WebSecurity> {
@Value("${keycloak.configurationFile:WEB-INF/keycloak.json}")
private Resource keycloakConfigFileResource;
@Bean @Bean
protected AdapterDeploymentContextBean adapterDeploymentContextBean() { protected AdapterDeploymentContextBean adapterDeploymentContextBean() {
return new AdapterDeploymentContextBean(); return new AdapterDeploymentContextBean(keycloakConfigFileResource);
} }
protected AuthenticationEntryPoint authenticationEntryPoint() protected AuthenticationEntryPoint authenticationEntryPoint() {
{
return new KeycloakAuthenticationEntryPoint(); return new KeycloakAuthenticationEntryPoint();
} }

View file

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

View file

@ -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"
}
}