@Configuration
public class KeyCloakServerConfiguration {
@Bean
public EmbeddedServletContainerCustomizer getKeycloakContainerCustomizer() {
return new EmbeddedServletContainerCustomizer() {
@Override
public void customize(
ConfigurableEmbeddedServletContainer configurableEmbeddedServletContainer) {
if (configurableEmbeddedServletContainer instanceof TomcatEmbeddedServletContainerFactory) {
TomcatEmbeddedServletContainerFactory container = (TomcatEmbeddedServletContainerFactory) configurableEmbeddedServletContainer;
KeycloakAuthenticatorValve authenticatorValve = new KeycloakAuthenticatorValve();
container.addContextValves(authenticatorValve);
container.addContextCustomizers(getKeycloakContextCustomizer());
}
}
};
}
@Bean
public TomcatContextCustomizer getKeycloakContextCustomizer() {
return new TomcatContextCustomizer() {
@Override
public void customize(Context context) {
SecurityConstraint secConstraints = new SecurityConstraint();
secConstraints.setAuthConstraint(true);
secConstraints.addAuthRole("ROLE_USER");
//The only time the application Should Allow Puts is when and administrator
//is authenticated with the site.
SecurityCollection putCollection = new SecurityCollection();
putCollection.addPattern("/**");
putCollection.addMethod("POST");
SecurityCollection getAuthenticatedMaterialsCollection = new SecurityCollection();
getAuthenticatedMaterialsCollection.addPattern("/**");
getAuthenticatedMaterialsCollection.addPattern("/*");
secConstraints.addCollection(putCollection);
secConstraints.addCollection(getAuthenticatedMaterialsCollection);
context.addConstraint(secConstraints);
LoginConfig loginConfig = new LoginConfig();
loginConfig.setAuthMethod("KEYCLOAK");
context.setLoginConfig(loginConfig);
context.addParameter("keycloak.config.resolver",
SpringBootKeycloakConfigResolver.class.getName());
}
};
}
public static class SpringBootKeycloakConfigResolver implements
KeycloakConfigResolver {
private KeycloakDeployment keycloakDeployment;
@Override
public KeycloakDeployment resolve(HttpFacade.Request request) {
if (keycloakDeployment != null) {
return keycloakDeployment;
}
InputStream configInputStream = getClass().getResourceAsStream(
"/keycloak.json");
if (configInputStream == null) {
keycloakDeployment = new KeycloakDeployment();
} else {
keycloakDeployment = KeycloakDeploymentBuilder
.build(configInputStream);
}
return keycloakDeployment;
}
}
[DEBUG] org.keycloak.adapters.PreAuthActionsHandler - adminRequest http://localhost:8080/
[DEBUG] org.keycloak.adapters.KeycloakDeployment - resolveBrowserUrls
[DEBUG] org.keycloak.adapters.KeycloakDeployment - resolveNonBrowserUrls
[DEBUG] org.keycloak.adapters.KeycloakDeploymentBuilder - Use authServerUrl: http://192.168.53.252:8080/auth, codeUrl: http://192.168.53.252:8080/auth/realms/Spring-Development/protocol/openid-connect/access/codes, relativeUrls: NEVER