Enhance AuthenticationProcessingFilterEntryPoint and related classes, to support a property forcing the login page to be access via https even if the original intercepted request came in as http.

This commit is contained in:
Colin Sampaleanu
2004-04-22 21:47:05 +00:00
parent 088563c363
commit e2de3c9dbc
16 changed files with 286 additions and 128 deletions

View File

@@ -42,6 +42,7 @@ import javax.servlet.http.HttpSession;
* </p>
*
* @author Ben Alex
* @author colin sampaleanu
* @version $Id$
*/
public class MockHttpServletRequest implements HttpServletRequest {
@@ -53,7 +54,11 @@ public class MockHttpServletRequest implements HttpServletRequest {
private Principal principal;
private String contextPath = "";
private String queryString = null;
private String requestURL;
private String scheme;
private String serverName;
private String servletPath;
private int serverPort;
//~ Constructors ===========================================================
@@ -235,8 +240,12 @@ public class MockHttpServletRequest implements HttpServletRequest {
throw new UnsupportedOperationException("mock method not implemented");
}
public void setRequestURL(String newRequestURL) {
requestURL = newRequestURL;
}
public StringBuffer getRequestURL() {
throw new UnsupportedOperationException("mock method not implemented");
return new StringBuffer(requestURL);
}
public String getRequestedSessionId() {
@@ -259,20 +268,32 @@ public class MockHttpServletRequest implements HttpServletRequest {
throw new UnsupportedOperationException("mock method not implemented");
}
public void setScheme(String newScheme) {
scheme = newScheme;
}
public String getScheme() {
throw new UnsupportedOperationException("mock method not implemented");
return scheme;
}
public boolean isSecure() {
throw new UnsupportedOperationException("mock method not implemented");
}
public void setServerName(String newServerName) {
serverName = newServerName;
}
public String getServerName() {
throw new UnsupportedOperationException("mock method not implemented");
return serverName;
}
public void setServerPort(int newPort) {
serverPort = newPort;
}
public int getServerPort() {
throw new UnsupportedOperationException("mock method not implemented");
return serverPort;
}
public void setServletPath(String servletPath) {

View File

@@ -41,6 +41,7 @@ import javax.servlet.ServletResponse;
* Tests {@link FilterInvocation}.
*
* @author Ben Alex
* @author colin sampaleanu
* @version $Id$
*/
public class FilterInvocationTests extends TestCase {
@@ -67,6 +68,7 @@ public class FilterInvocationTests extends TestCase {
public void testGettersAndStringMethods() {
MockHttpServletRequest request = new MockHttpServletRequest(null, null);
request.setServletPath("/HelloWorld");
request.setRequestURL("http://www.example.com/mycontext/HelloWorld");
MockHttpServletResponse response = new MockHttpServletResponse();
MockFilterChain chain = new MockFilterChain();
@@ -78,6 +80,8 @@ public class FilterInvocationTests extends TestCase {
assertEquals(chain, fi.getChain());
assertEquals("/HelloWorld", fi.getRequestUrl());
assertEquals("FilterInvocation: URL: /HelloWorld", fi.toString());
assertEquals("http://www.example.com/mycontext/HelloWorld",
fi.getFullRequestUrl());
}
public void testNoArgsConstructor() {
@@ -156,23 +160,29 @@ public class FilterInvocationTests extends TestCase {
public void testStringMethodsWithAQueryString() {
MockHttpServletRequest request = new MockHttpServletRequest("foo=bar");
request.setServletPath("/HelloWorld");
request.setRequestURL("http://www.example.com/mycontext/HelloWorld");
MockHttpServletResponse response = new MockHttpServletResponse();
MockFilterChain chain = new MockFilterChain();
FilterInvocation fi = new FilterInvocation(request, response, chain);
assertEquals("/HelloWorld?foo=bar", fi.getRequestUrl());
assertEquals("FilterInvocation: URL: /HelloWorld?foo=bar", fi.toString());
assertEquals("http://www.example.com/mycontext/HelloWorld?foo=bar",
fi.getFullRequestUrl());
}
public void testStringMethodsWithoutAnyQueryString() {
MockHttpServletRequest request = new MockHttpServletRequest(null, null);
request.setServletPath("/HelloWorld");
request.setRequestURL("http://www.example.com/mycontext/HelloWorld");
MockHttpServletResponse response = new MockHttpServletResponse();
MockFilterChain chain = new MockFilterChain();
FilterInvocation fi = new FilterInvocation(request, response, chain);
assertEquals("/HelloWorld", fi.getRequestUrl());
assertEquals("FilterInvocation: URL: /HelloWorld", fi.toString());
assertEquals("http://www.example.com/mycontext/HelloWorld",
fi.getFullRequestUrl());
}
//~ Inner Classes ==========================================================

View File

@@ -128,6 +128,8 @@ public class SecurityEnforcementFilterTests extends TestCase {
MockHttpServletRequest request = new MockHttpServletRequest(null,
new MockHttpSession());
request.setServletPath("/secure/page.html");
request.setRequestURL(
"http://www.example.com/mycontext/secure/page.html");
// Setup our expectation that the filter chain will not be invoked, as access is denied
MockFilterChain chain = new MockFilterChain(false);
@@ -146,7 +148,7 @@ public class SecurityEnforcementFilterTests extends TestCase {
MockHttpServletResponse response = new MockHttpServletResponse();
filter.doFilter(request, response, chain);
assertEquals("/login.jsp", response.getRedirect());
assertEquals("/secure/page.html",
assertEquals("http://www.example.com/mycontext/secure/page.html",
request.getSession().getAttribute(AuthenticationProcessingFilter.ACEGI_SECURITY_TARGET_URL_KEY));
}

View File

@@ -65,7 +65,7 @@ public class ServicePropertiesTests extends TestCase {
sp.setService("https://mycompany.com/service");
assertEquals("https://mycompany.com/service", sp.getService());
sp.afterPropertiesSet();
}
}

View File

@@ -15,22 +15,22 @@
package net.sf.acegisecurity.ui.webapp;
import java.util.HashMap;
import junit.framework.TestCase;
import net.sf.acegisecurity.MockHttpServletRequest;
import net.sf.acegisecurity.MockHttpServletResponse;
import java.util.HashMap;
/**
* Tests {@link AuthenticationProcessingFilterEntryPoint}.
*
* @author Ben Alex
* @author colin sampaleanu
* @version $Id$
*/
public class AuthenticationProcessingFilterEntryPointTests extends TestCase {
//~ Methods ================================================================
public final void setUp() throws Exception {
@@ -57,37 +57,48 @@ public class AuthenticationProcessingFilterEntryPointTests extends TestCase {
ep.setLoginFormUrl("/hello");
assertEquals("/hello", ep.getLoginFormUrl());
}
public void testSetSslPortMapping() {
public void testHttpsOperation() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest(
"/some_path");
request.setScheme("http");
request.setServerName("www.example.com");
request.setContextPath("/bigWebApp");
request.setServerPort(80);
MockHttpServletResponse response = new MockHttpServletResponse();
AuthenticationProcessingFilterEntryPoint ep = new AuthenticationProcessingFilterEntryPoint();
ep.setLoginFormUrl("/hello");
ep.setForceHttps(true);
ep.afterPropertiesSet();
ep.commence(request, response);
assertEquals("https://www.example.com:443/bigWebApp/hello",
response.getRedirect());
request.setServerPort(8080);
ep.commence(request, response);
assertEquals("https://www.example.com:8443/bigWebApp/hello",
response.getRedirect());
// check that unknown port leaves things as-is
request.setServerPort(8888);
ep.commence(request, response);
assertEquals("/bigWebApp/hello", response.getRedirect());
ep = new AuthenticationProcessingFilterEntryPoint();
ep.setLoginFormUrl("/hello");
ep.setForceHttps(true);
HashMap map = new HashMap();
try {
ep.setHttpsPortMapping(map);
} catch (IllegalArgumentException expected) {
assertEquals("must map at least one port", expected.getMessage());
}
map.put(new Integer(0).toString(), new Integer(443).toString());
try {
ep.setHttpsPortMapping(map);
} catch (IllegalArgumentException expected) {
assertTrue(expected.getMessage().startsWith("one or both ports out of legal range"));
}
map.clear();
map.put(new Integer(80).toString(), new Integer(100000).toString());
try {
ep.setHttpsPortMapping(map);
} catch (IllegalArgumentException expected) {
assertTrue(expected.getMessage().startsWith("one or both ports out of legal range"));
}
map.clear();
map.put(new Integer(80).toString(), new Integer(443).toString());
ep.setHttpsPortMapping(map);
map = ep.getTranslatedHttpsPortMapping();
assertTrue(map.size() == 1);
assertTrue(((Integer)map.get(new Integer(80))).equals(new Integer(443)));
map.put("8888", "9999");
ep.setHttpsPortMappings(map);
ep.afterPropertiesSet();
ep.commence(request, response);
assertEquals("https://www.example.com:9999/bigWebApp/hello",
response.getRedirect());
}
public void testNormalOperation() throws Exception {
@@ -104,11 +115,39 @@ public class AuthenticationProcessingFilterEntryPointTests extends TestCase {
ep.commence(request, response);
assertEquals("/bigWebApp/hello", response.getRedirect());
}
public void testHttpsOperation() throws Exception {
public void testSetSslPortMapping() {
AuthenticationProcessingFilterEntryPoint ep = new AuthenticationProcessingFilterEntryPoint();
//TODO: finish later today
HashMap map = new HashMap();
try {
ep.setHttpsPortMappings(map);
} catch (IllegalArgumentException expected) {
assertEquals("must map at least one port", expected.getMessage());
}
map.put(new Integer(0).toString(), new Integer(443).toString());
try {
ep.setHttpsPortMappings(map);
} catch (IllegalArgumentException expected) {
assertTrue(expected.getMessage().startsWith("one or both ports out of legal range"));
}
map.clear();
map.put(new Integer(80).toString(), new Integer(100000).toString());
try {
ep.setHttpsPortMappings(map);
} catch (IllegalArgumentException expected) {
assertTrue(expected.getMessage().startsWith("one or both ports out of legal range"));
}
map.clear();
map.put(new Integer(80).toString(), new Integer(443).toString());
ep.setHttpsPortMappings(map);
map = ep.getTranslatedHttpsPortMappings();
assertTrue(map.size() == 1);
assertTrue(((Integer) map.get(new Integer(80))).equals(new Integer(443)));
}
}