Added redirect strategy to support Faces.

This commit is contained in:
Scott Battaglia 2013-06-18 21:56:58 -04:00
parent 091f1d5f90
commit 8df9f0272b
6 changed files with 152 additions and 8 deletions

View File

@ -68,6 +68,8 @@ public class AuthenticationFilter extends AbstractCasFilter {
private GatewayResolver gatewayStorage = new DefaultGatewayResolverImpl();
private AuthenticationRedirectStrategy authenticationRedirectStrategy = new DefaultAuthenticationRedirectStrategy();
protected void initInternal(final FilterConfig filterConfig) throws ServletException {
if (!isIgnoreInitConfiguration()) {
super.initInternal(filterConfig);
@ -81,13 +83,23 @@ public class AuthenticationFilter extends AbstractCasFilter {
final String gatewayStorageClass = getPropertyFromInitParams(filterConfig, "gatewayStorageClass", null);
if (gatewayStorageClass != null) {
try {
this.gatewayStorage = (GatewayResolver) Class.forName(gatewayStorageClass).newInstance();
} catch (final Exception e) {
logger.error(e.getMessage(),e);
throw new ServletException(e);
}
this.gatewayStorage = classNameToClass(gatewayStorageClass);
}
final String authenticationRedirectStrategyClass = getPropertyFromInitParams(filterConfig, "authenticationRedirectStrategyClass", null);
if (authenticationRedirectStrategyClass != null) {
this.authenticationRedirectStrategy = classNameToClass(authenticationRedirectStrategyClass);
}
}
}
private <T> T classNameToClass(final String className) throws ServletException {
try {
return (T) Class.forName(className).newInstance();
} catch (final Exception e) {
logger.error(e.getMessage(),e);
throw new ServletException(e);
}
}
@ -131,8 +143,7 @@ public class AuthenticationFilter extends AbstractCasFilter {
final String urlToRedirectTo = CommonUtils.constructRedirectUrl(this.casServerLoginUrl, getServiceParameterName(), modifiedServiceUrl, this.renew, this.gateway);
logger.debug("redirecting to \"{}\"", urlToRedirectTo);
response.sendRedirect(urlToRedirectTo);
this.authenticationRedirectStrategy.redirect(request, response, urlToRedirectTo);
}
public final void setRenew(final boolean renew) {

View File

@ -0,0 +1,14 @@
package org.jasig.cas.client.authentication;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* Created by battags on 6/18/13.
*/
public interface AuthenticationRedirectStrategy {
void redirect(HttpServletRequest request, HttpServletResponse response, String potentialRedirectUrl) throws IOException;
}

View File

@ -0,0 +1,18 @@
package org.jasig.cas.client.authentication;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* Implementation of the {@link AuthenticationRedirectStrategy} class that preserves the original behavior that existed prior to 3.3.0.
*
* @author Scott Battaglia
* @since 3.3.0
*/
public final class DefaultAuthenticationRedirectStrategy implements AuthenticationRedirectStrategy {
public void redirect(final HttpServletRequest request, final HttpServletResponse response, final String potentialRedirectUrl) throws IOException {
response.sendRedirect(potentialRedirectUrl);
}
}

View File

@ -0,0 +1,34 @@
package org.jasig.cas.client.authentication;
import org.jasig.cas.client.util.CommonUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* Implementation of the redirect strategy that can handle a Faces Ajax request in addition to the standard redirect style.
*
* @author Scott Battaglia
* @since 3.3.0
*/
public final class FacesCompatibleAuthenticationRedirectStrategy implements AuthenticationRedirectStrategy {
private static final String FACES_PARTIAL_AJAX_PARAMETER = "javax.faces.partial.ajax";
public void redirect(final HttpServletRequest request, final HttpServletResponse response, final String potentialRedirectUrl) throws IOException {
if (CommonUtils.isNotBlank(request.getParameter(FACES_PARTIAL_AJAX_PARAMETER))) {
// this is an ajax request - redirect ajaxly
response.setContentType("text/xml");
response.setStatus(200);
final PrintWriter writer = response.getWriter();
writer.write("<?xml version='1.0' encoding='UTF-8'?>");
writer.write(String.format("<partial-response><redirect url=\"%s\"></redirect></partial-response>", potentialRedirectUrl));
} else {
response.sendRedirect(potentialRedirectUrl);
}
}
}

View File

@ -0,0 +1,28 @@
package org.jasig.cas.client.authentication;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
public class DefaultAuthenticationRedirectStrategyTests {
private DefaultAuthenticationRedirectStrategy strategy;
@Before
public void setUp() throws Exception {
this.strategy = new DefaultAuthenticationRedirectStrategy();
}
@Test
public void didWeRedirect() throws Exception {
final String redirectUrl = "http://www.jasig.org";
final MockHttpServletRequest request = new MockHttpServletRequest();
final MockHttpServletResponse response = new MockHttpServletResponse();
this.strategy.redirect(request, response, redirectUrl);
assertEquals(redirectUrl, response.getRedirectedUrl());
}
}

View File

@ -0,0 +1,39 @@
package org.jasig.cas.client.authentication;
import org.junit.Before;
import org.junit.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import static org.junit.Assert.*;
public class FacesCompatibleAuthenticationRedirectStrategyTests {
private FacesCompatibleAuthenticationRedirectStrategy strategy;
@Before
public void setUp() throws Exception {
this.strategy = new FacesCompatibleAuthenticationRedirectStrategy();
}
@Test
public void didWeRedirect() throws Exception {
final String redirectUrl = "http://www.jasig.org";
final MockHttpServletRequest request = new MockHttpServletRequest();
final MockHttpServletResponse response = new MockHttpServletResponse();
this.strategy.redirect(request, response, redirectUrl);
assertEquals(redirectUrl, response.getRedirectedUrl());
}
@Test
public void facesPartialResponse() throws Exception {
final String redirectUrl = "http://www.jasig.org";
final MockHttpServletRequest request = new MockHttpServletRequest();
final MockHttpServletResponse response = new MockHttpServletResponse();
request.setParameter("javax.faces.partial.ajax", "true");
this.strategy.redirect(request, response, redirectUrl);
assertNull(response.getRedirectedUrl());
assertTrue(response.getContentAsString().contains(redirectUrl));
}
}