Merge pull request #1469 from Smartling/KEYCLOAK-1494-Fix
Support loading keycloak.json from the classpath
This commit is contained in:
commit
27ac253098
1 changed files with 23 additions and 5 deletions
|
@ -9,30 +9,48 @@ import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.context.ApplicationContextAware;
|
import org.springframework.context.ApplicationContextAware;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
|
|
||||||
import java.io.InputStream;
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Bean holding the {@link KeycloakDeployment} and {@link AdapterDeploymentContext} for this
|
* Bean holding the {@link KeycloakDeployment} and {@link AdapterDeploymentContext} for this
|
||||||
* Spring application context. The Keycloak deployment is loaded from the required
|
* Spring application context. The Keycloak deployment is loaded from the required
|
||||||
* <code>WEB-INF/keycloak.json</code> file generated by Keycloak.
|
* <code>keycloak.json</code> resource file.
|
||||||
*
|
*
|
||||||
* @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 ApplicationContextAware, 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 ApplicationContext applicationContext;
|
private ApplicationContext applicationContext;
|
||||||
private AdapterDeploymentContext deploymentContext;
|
private AdapterDeploymentContext deploymentContext;
|
||||||
private KeycloakDeployment deployment;
|
private KeycloakDeployment deployment;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void afterPropertiesSet() throws Exception {
|
public void afterPropertiesSet() throws Exception {
|
||||||
Resource resource = applicationContext.getResource("WEB-INF/keycloak.json");
|
this.deployment = loadKeycloakDeployment();
|
||||||
InputStream is = resource.getInputStream();
|
|
||||||
this.deployment = KeycloakDeploymentBuilder.build(is);
|
|
||||||
this.deploymentContext = new AdapterDeploymentContext(deployment);
|
this.deploymentContext = new AdapterDeploymentContext(deployment);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private KeycloakDeployment loadKeycloakDeployment() throws IOException {
|
||||||
|
|
||||||
|
Resource resource = applicationContext.getResource(KEYCLOAK_CONFIG_WEB_RESOURCE);
|
||||||
|
|
||||||
|
if (!resource.isReadable()) {
|
||||||
|
resource= applicationContext.getResource(KEYCLOAK_CONFIG_CLASSPATH_RESOURCE);
|
||||||
|
}
|
||||||
|
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the Keycloak {@link AdapterDeploymentContext} for this application context.
|
* Returns the Keycloak {@link AdapterDeploymentContext} for this application context.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in a new issue