Merge pull request #2567 from cpitman/proxy-headers

KEYCLOAK-2791 - Proxy handling for X-Forwarded headers
This commit is contained in:
Bill Burke 2016-04-12 17:53:15 -04:00
commit b2a803929d
2 changed files with 22 additions and 2 deletions

View file

@ -185,6 +185,8 @@ public class ProxyConfig {
protected AdapterConfig adapterConfig;
@JsonProperty("error-page")
protected String errorPage;
@JsonProperty("proxy-address-forwarding")
protected boolean proxyAddressForwarding;
@JsonProperty("constraints")
protected List<Constraint> constraints = new LinkedList<Constraint>();
@ -212,6 +214,14 @@ public class ProxyConfig {
this.errorPage = errorPage;
}
public boolean isProxyAddressForwarding() {
return proxyAddressForwarding;
}
public void setProxyAddressForwarding(boolean proxyAddressForwarding) {
this.proxyAddressForwarding = proxyAddressForwarding;
}
public List<Constraint> getConstraints() {
return constraints;
}

View file

@ -32,6 +32,7 @@ import io.undertow.server.HttpServerExchange;
import io.undertow.server.handlers.PathHandler;
import io.undertow.server.handlers.ResponseCodeHandler;
import io.undertow.server.handlers.proxy.ProxyHandler;
import io.undertow.server.handlers.ProxyPeerAddressHandler;
import io.undertow.server.handlers.proxy.SimpleProxyClientProvider;
import io.undertow.server.session.InMemorySessionManager;
import io.undertow.server.session.SessionAttachmentHandler;
@ -135,6 +136,7 @@ public class ProxyServerBuilder {
protected SecurityPathMatches.Builder constraintBuilder = new SecurityPathMatches.Builder();
protected SecurityPathMatches matches;
protected String errorPage;
protected boolean proxyAddressForwarding;
public ApplicationBuilder base(String base) {
this.base = base;
@ -149,6 +151,11 @@ public class ProxyServerBuilder {
return this;
}
public ApplicationBuilder proxyAddressForwarding(boolean proxyAddressForwarding) {
this.proxyAddressForwarding = proxyAddressForwarding;
return this;
}
public ApplicationBuilder(AdapterConfig config) {
this.deployment = KeycloakDeploymentBuilder.build(config);
this.deploymentContext = new AdapterDeploymentContext(deployment);
@ -273,7 +280,9 @@ public class ProxyServerBuilder {
}
};
handler = new UndertowPreAuthActionsHandler(deploymentContext, userSessionManagement, sessionManager, handler);
return new SecurityInitialHandler(AuthenticationMode.PRO_ACTIVE, identityManager, handler);
handler = new SecurityInitialHandler(AuthenticationMode.PRO_ACTIVE, identityManager, handler);
if (proxyAddressForwarding) handler = new ProxyPeerAddressHandler(handler);
return handler;
}
private HttpHandler sessionHandling(HttpHandler toWrap) {
@ -383,7 +392,8 @@ public class ProxyServerBuilder {
for (ProxyConfig.Application application : config.getApplications()) {
ApplicationBuilder applicationBuilder = builder.application(application.getAdapterConfig())
.base(application.getBasePath())
.errorPage(application.getErrorPage());
.errorPage(application.getErrorPage())
.proxyAddressForwarding(application.isProxyAddressForwarding());
if (application.getConstraints() != null) {
for (ProxyConfig.Constraint constraint : application.getConstraints()) {