Skip to content

Commit

Permalink
Merge pull request #72 from battags/CASC-223
Browse files Browse the repository at this point in the history
CASC-223 Backwards Compatibility for Initializing Sign Out Handler in Filter
  • Loading branch information
battags committed May 23, 2014
2 parents aeaa76c + a44b4c1 commit 566c869
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.jasig.cas.client.session;

import java.io.IOException;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
Expand All @@ -34,57 +35,68 @@
*/
public final class SingleSignOutFilter extends AbstractConfigurationFilter {

private static final SingleSignOutHandler handler = new SingleSignOutHandler();
private static final SingleSignOutHandler HANDLER = new SingleSignOutHandler();

private AtomicBoolean handlerInitialized = new AtomicBoolean(false);

public void init(final FilterConfig filterConfig) throws ServletException {
if (!isIgnoreInitConfiguration()) {
handler.setArtifactParameterName(getPropertyFromInitParams(filterConfig, "artifactParameterName",
HANDLER.setArtifactParameterName(getPropertyFromInitParams(filterConfig, "artifactParameterName",
SingleSignOutHandler.DEFAULT_ARTIFACT_PARAMETER_NAME));
handler.setLogoutParameterName(getPropertyFromInitParams(filterConfig, "logoutParameterName",
HANDLER.setLogoutParameterName(getPropertyFromInitParams(filterConfig, "logoutParameterName",
SingleSignOutHandler.DEFAULT_LOGOUT_PARAMETER_NAME));
handler.setFrontLogoutParameterName(getPropertyFromInitParams(filterConfig, "frontLogoutParameterName",
HANDLER.setFrontLogoutParameterName(getPropertyFromInitParams(filterConfig, "frontLogoutParameterName",
SingleSignOutHandler.DEFAULT_FRONT_LOGOUT_PARAMETER_NAME));
handler.setRelayStateParameterName(getPropertyFromInitParams(filterConfig, "relayStateParameterName",
HANDLER.setRelayStateParameterName(getPropertyFromInitParams(filterConfig, "relayStateParameterName",
SingleSignOutHandler.DEFAULT_RELAY_STATE_PARAMETER_NAME));
handler.setCasServerUrlPrefix(getPropertyFromInitParams(filterConfig, "casServerUrlPrefix", null));
handler.setArtifactParameterOverPost(parseBoolean(getPropertyFromInitParams(filterConfig,
HANDLER.setCasServerUrlPrefix(getPropertyFromInitParams(filterConfig, "casServerUrlPrefix", null));
HANDLER.setArtifactParameterOverPost(parseBoolean(getPropertyFromInitParams(filterConfig,
"artifactParameterOverPost", "false")));
handler.setEagerlyCreateSessions(parseBoolean(getPropertyFromInitParams(filterConfig,
HANDLER.setEagerlyCreateSessions(parseBoolean(getPropertyFromInitParams(filterConfig,
"eagerlyCreateSessions", "true")));
}
handler.init();
HANDLER.init();
handlerInitialized.set(true);
}

public void setArtifactParameterName(final String name) {
handler.setArtifactParameterName(name);
HANDLER.setArtifactParameterName(name);
}

public void setLogoutParameterName(final String name) {
handler.setLogoutParameterName(name);
HANDLER.setLogoutParameterName(name);
}

public void setFrontLogoutParameterName(final String name) {
handler.setFrontLogoutParameterName(name);
HANDLER.setFrontLogoutParameterName(name);
}

public void setRelayStateParameterName(final String name) {
handler.setRelayStateParameterName(name);
HANDLER.setRelayStateParameterName(name);
}

public void setCasServerUrlPrefix(final String casServerUrlPrefix) {
handler.setCasServerUrlPrefix(casServerUrlPrefix);
HANDLER.setCasServerUrlPrefix(casServerUrlPrefix);
}

public void setSessionMappingStorage(final SessionMappingStorage storage) {
handler.setSessionMappingStorage(storage);
HANDLER.setSessionMappingStorage(storage);
}

public void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse,
final FilterChain filterChain) throws IOException, ServletException {
final HttpServletRequest request = (HttpServletRequest) servletRequest;
final HttpServletResponse response = (HttpServletResponse) servletResponse;

if (handler.process(request, response)) {
/**
* <p>Workaround for now for the fact that Spring Security will fail since it doesn't call {@link #init(javax.servlet.FilterConfig)}.</p>
* <p>Ultimately we need to allow deployers to actually inject their fully-initialized {@link org.jasig.cas.client.session.SingleSignOutHandler}.</p>
*/
if (!this.handlerInitialized.getAndSet(true)) {
HANDLER.init();
}

if (HANDLER.process(request, response)) {
filterChain.doFilter(servletRequest, servletResponse);
}
}
Expand All @@ -94,6 +106,6 @@ public void destroy() {
}

protected static SingleSignOutHandler getSingleSignOutHandler() {
return handler;
return HANDLER;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,18 +132,20 @@ public void setEagerlyCreateSessions(final boolean eagerlyCreateSessions) {
/**
* Initializes the component for use.
*/
public void init() {
CommonUtils.assertNotNull(this.artifactParameterName, "artifactParameterName cannot be null.");
CommonUtils.assertNotNull(this.logoutParameterName, "logoutParameterName cannot be null.");
CommonUtils.assertNotNull(this.frontLogoutParameterName, "frontLogoutParameterName cannot be null.");
CommonUtils.assertNotNull(this.sessionMappingStorage, "sessionMappingStorage cannot be null.");
CommonUtils.assertNotNull(this.relayStateParameterName, "relayStateParameterName cannot be null.");
CommonUtils.assertNotNull(this.casServerUrlPrefix, "casServerUrlPrefix cannot be null.");

if (this.artifactParameterOverPost) {
this.safeParameters = Arrays.asList(this.logoutParameterName, this.artifactParameterName);
} else {
this.safeParameters = Arrays.asList(this.logoutParameterName);
public synchronized void init() {
if (this.safeParameters == null) {
CommonUtils.assertNotNull(this.artifactParameterName, "artifactParameterName cannot be null.");
CommonUtils.assertNotNull(this.logoutParameterName, "logoutParameterName cannot be null.");
CommonUtils.assertNotNull(this.frontLogoutParameterName, "frontLogoutParameterName cannot be null.");
CommonUtils.assertNotNull(this.sessionMappingStorage, "sessionMappingStorage cannot be null.");
CommonUtils.assertNotNull(this.relayStateParameterName, "relayStateParameterName cannot be null.");
CommonUtils.assertNotNull(this.casServerUrlPrefix, "casServerUrlPrefix cannot be null.");

if (this.artifactParameterOverPost) {
this.safeParameters = Arrays.asList(this.logoutParameterName, this.artifactParameterName);
} else {
this.safeParameters = Arrays.asList(this.logoutParameterName);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,6 @@ public void setUp() throws Exception {
response = new MockHttpServletResponse();
filterChain = new MockFilterChain();
}

@Test(expected = IllegalArgumentException.class)
public void initWithoutCasServerUrlPrefix() throws ServletException {
filter = new SingleSignOutFilter();
filter.init(new MockFilterConfig());
}

@Test
public void tokenRequest() throws IOException, ServletException {
Expand Down

0 comments on commit 566c869

Please sign in to comment.