Anonymous principal support. As requested by the community at various times, including in http://forum.springframework.org/viewtopic.php?t=1925.
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
/* Copyright 2004, 2005 Acegi Technology Pty Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package net.sf.acegisecurity;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import net.sf.acegisecurity.providers.TestingAuthenticationToken;
|
||||
import net.sf.acegisecurity.providers.anonymous.AnonymousAuthenticationToken;
|
||||
|
||||
|
||||
/**
|
||||
* Tests {@link net.sf.acegisecurity.AuthenticationTrustResolverImpl}.
|
||||
*
|
||||
* @author Ben Alex
|
||||
* @version $Id$
|
||||
*/
|
||||
public class AuthenticationTrustResolverImplTests extends TestCase {
|
||||
//~ Constructors ===========================================================
|
||||
|
||||
public AuthenticationTrustResolverImplTests() {
|
||||
super();
|
||||
}
|
||||
|
||||
public AuthenticationTrustResolverImplTests(String arg0) {
|
||||
super(arg0);
|
||||
}
|
||||
|
||||
//~ Methods ================================================================
|
||||
|
||||
public static void main(String[] args) {
|
||||
junit.textui.TestRunner.run(AuthenticationTrustResolverImplTests.class);
|
||||
}
|
||||
|
||||
public void testCorrectOperationIsAnonymous() {
|
||||
AuthenticationTrustResolverImpl trustResolver = new AuthenticationTrustResolverImpl();
|
||||
assertTrue(trustResolver.isAnonymous(
|
||||
new AnonymousAuthenticationToken("ignored", "ignored",
|
||||
new GrantedAuthority[] {new GrantedAuthorityImpl("ignored")})));
|
||||
assertFalse(trustResolver.isAnonymous(
|
||||
new TestingAuthenticationToken("ignored", "ignored",
|
||||
new GrantedAuthority[] {new GrantedAuthorityImpl("ignored")})));
|
||||
}
|
||||
|
||||
public void testGettersSetters() {
|
||||
AuthenticationTrustResolverImpl trustResolver = new AuthenticationTrustResolverImpl();
|
||||
|
||||
assertEquals(AnonymousAuthenticationToken.class,
|
||||
trustResolver.getAnonymousClass());
|
||||
trustResolver.setAnonymousClass(String.class);
|
||||
assertEquals(String.class, trustResolver.getAnonymousClass());
|
||||
|
||||
assertNull(trustResolver.getRememberMeClass());
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright 2004 Acegi Technology Pty Limited
|
||||
/* Copyright 2004, 2005 Acegi Technology Pty Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -19,11 +19,17 @@ import junit.framework.TestCase;
|
||||
|
||||
import net.sf.acegisecurity.AccessDeniedException;
|
||||
import net.sf.acegisecurity.BadCredentialsException;
|
||||
import net.sf.acegisecurity.GrantedAuthority;
|
||||
import net.sf.acegisecurity.GrantedAuthorityImpl;
|
||||
import net.sf.acegisecurity.MockAuthenticationEntryPoint;
|
||||
import net.sf.acegisecurity.MockHttpServletRequest;
|
||||
import net.sf.acegisecurity.MockHttpServletResponse;
|
||||
import net.sf.acegisecurity.MockHttpSession;
|
||||
import net.sf.acegisecurity.MockPortResolver;
|
||||
import net.sf.acegisecurity.context.ContextHolder;
|
||||
import net.sf.acegisecurity.context.security.SecureContext;
|
||||
import net.sf.acegisecurity.context.security.SecureContextImpl;
|
||||
import net.sf.acegisecurity.providers.anonymous.AnonymousAuthenticationToken;
|
||||
import net.sf.acegisecurity.ui.webapp.AuthenticationProcessingFilter;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -62,8 +68,47 @@ public class SecurityEnforcementFilterTests extends TestCase {
|
||||
junit.textui.TestRunner.run(SecurityEnforcementFilterTests.class);
|
||||
}
|
||||
|
||||
public void testAccessDeniedWhenAccessDeniedException()
|
||||
throws Exception {
|
||||
public void testAccessDeniedWhenAnonymous() throws Exception {
|
||||
// Setup our HTTP request
|
||||
HttpSession session = new MockHttpSession();
|
||||
MockHttpServletRequest request = new MockHttpServletRequest(null,
|
||||
session);
|
||||
request.setServletPath("/secure/page.html");
|
||||
request.setServerPort(80);
|
||||
request.setScheme("http");
|
||||
request.setServerName("www.example.com");
|
||||
request.setContextPath("/mycontext");
|
||||
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);
|
||||
|
||||
// Setup the FilterSecurityInterceptor thrown an access denied exception
|
||||
MockFilterSecurityInterceptor interceptor = new MockFilterSecurityInterceptor(true,
|
||||
false);
|
||||
|
||||
// Setup ContextHolder, as filter needs to check if user is anonymous
|
||||
SecureContext sc = new SecureContextImpl();
|
||||
sc.setAuthentication(new AnonymousAuthenticationToken("ignored",
|
||||
"ignored",
|
||||
new GrantedAuthority[] {new GrantedAuthorityImpl("IGNORED")}));
|
||||
ContextHolder.setContext(sc);
|
||||
|
||||
// Test
|
||||
SecurityEnforcementFilter filter = new SecurityEnforcementFilter();
|
||||
filter.setFilterSecurityInterceptor(interceptor);
|
||||
filter.setAuthenticationEntryPoint(new MockAuthenticationEntryPoint(
|
||||
"/login.jsp"));
|
||||
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
filter.doFilter(request, response, chain);
|
||||
assertEquals("/mycontext/login.jsp", response.getRedirect());
|
||||
assertEquals("http://www.example.com/mycontext/secure/page.html",
|
||||
request.getSession().getAttribute(AuthenticationProcessingFilter.ACEGI_SECURITY_TARGET_URL_KEY));
|
||||
}
|
||||
|
||||
public void testAccessDeniedWhenNonAnonymous() throws Exception {
|
||||
// Setup our HTTP request
|
||||
HttpSession session = new MockHttpSession();
|
||||
MockHttpServletRequest request = new MockHttpServletRequest(null,
|
||||
@@ -77,6 +122,11 @@ public class SecurityEnforcementFilterTests extends TestCase {
|
||||
MockFilterSecurityInterceptor interceptor = new MockFilterSecurityInterceptor(true,
|
||||
false);
|
||||
|
||||
// Setup ContextHolder, as filter needs to check if user is anonymous
|
||||
SecureContext sc = new SecureContextImpl();
|
||||
sc.setAuthentication(null);
|
||||
ContextHolder.setContext(sc);
|
||||
|
||||
// Test
|
||||
SecurityEnforcementFilter filter = new SecurityEnforcementFilter();
|
||||
filter.setFilterSecurityInterceptor(interceptor);
|
||||
@@ -281,6 +331,11 @@ public class SecurityEnforcementFilterTests extends TestCase {
|
||||
assertTrue(true);
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
super.tearDown();
|
||||
ContextHolder.setContext(null);
|
||||
}
|
||||
|
||||
//~ Inner Classes ==========================================================
|
||||
|
||||
private class MockFilterChain implements FilterChain {
|
||||
|
||||
@@ -0,0 +1,122 @@
|
||||
/* Copyright 2004, 2005 Acegi Technology Pty Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package net.sf.acegisecurity.providers.anonymous;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import net.sf.acegisecurity.Authentication;
|
||||
import net.sf.acegisecurity.BadCredentialsException;
|
||||
import net.sf.acegisecurity.GrantedAuthority;
|
||||
import net.sf.acegisecurity.GrantedAuthorityImpl;
|
||||
import net.sf.acegisecurity.providers.TestingAuthenticationToken;
|
||||
|
||||
|
||||
/**
|
||||
* Tests {@link AnonymousAuthenticationProvider}.
|
||||
*
|
||||
* @author Ben Alex
|
||||
* @version $Id$
|
||||
*/
|
||||
public class AnonymousAuthenticationProviderTests extends TestCase {
|
||||
//~ Constructors ===========================================================
|
||||
|
||||
public AnonymousAuthenticationProviderTests() {
|
||||
super();
|
||||
}
|
||||
|
||||
public AnonymousAuthenticationProviderTests(String arg0) {
|
||||
super(arg0);
|
||||
}
|
||||
|
||||
//~ Methods ================================================================
|
||||
|
||||
public final void setUp() throws Exception {
|
||||
super.setUp();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
junit.textui.TestRunner.run(AnonymousAuthenticationProviderTests.class);
|
||||
}
|
||||
|
||||
public void testDetectsAnInvalidKey() throws Exception {
|
||||
AnonymousAuthenticationProvider aap = new AnonymousAuthenticationProvider();
|
||||
aap.setKey("qwerty");
|
||||
|
||||
AnonymousAuthenticationToken token = new AnonymousAuthenticationToken("WRONG_KEY",
|
||||
"Test",
|
||||
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
|
||||
"ROLE_TWO")});
|
||||
|
||||
try {
|
||||
Authentication result = aap.authenticate(token);
|
||||
fail("Should have thrown BadCredentialsException");
|
||||
} catch (BadCredentialsException expected) {
|
||||
assertEquals("The presented AnonymousAuthenticationToken does not contain the expected key",
|
||||
expected.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void testDetectsMissingKey() throws Exception {
|
||||
AnonymousAuthenticationProvider aap = new AnonymousAuthenticationProvider();
|
||||
|
||||
try {
|
||||
aap.afterPropertiesSet();
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
} catch (IllegalArgumentException expected) {
|
||||
assertTrue(true);
|
||||
}
|
||||
}
|
||||
|
||||
public void testGettersSetters() throws Exception {
|
||||
AnonymousAuthenticationProvider aap = new AnonymousAuthenticationProvider();
|
||||
aap.setKey("qwerty");
|
||||
aap.afterPropertiesSet();
|
||||
assertEquals("qwerty", aap.getKey());
|
||||
}
|
||||
|
||||
public void testIgnoresClassesItDoesNotSupport() throws Exception {
|
||||
AnonymousAuthenticationProvider aap = new AnonymousAuthenticationProvider();
|
||||
aap.setKey("qwerty");
|
||||
|
||||
TestingAuthenticationToken token = new TestingAuthenticationToken("user",
|
||||
"password",
|
||||
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_A")});
|
||||
assertFalse(aap.supports(TestingAuthenticationToken.class));
|
||||
|
||||
// Try it anyway
|
||||
assertNull(aap.authenticate(token));
|
||||
}
|
||||
|
||||
public void testNormalOperation() throws Exception {
|
||||
AnonymousAuthenticationProvider aap = new AnonymousAuthenticationProvider();
|
||||
aap.setKey("qwerty");
|
||||
|
||||
AnonymousAuthenticationToken token = new AnonymousAuthenticationToken("qwerty",
|
||||
"Test",
|
||||
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
|
||||
"ROLE_TWO")});
|
||||
|
||||
Authentication result = aap.authenticate(token);
|
||||
|
||||
assertEquals(result, token);
|
||||
}
|
||||
|
||||
public void testSupports() {
|
||||
AnonymousAuthenticationProvider aap = new AnonymousAuthenticationProvider();
|
||||
assertTrue(aap.supports(AnonymousAuthenticationToken.class));
|
||||
assertFalse(aap.supports(TestingAuthenticationToken.class));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,190 @@
|
||||
/* Copyright 2004, 2005 Acegi Technology Pty Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package net.sf.acegisecurity.providers.anonymous;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import net.sf.acegisecurity.GrantedAuthority;
|
||||
import net.sf.acegisecurity.GrantedAuthorityImpl;
|
||||
import net.sf.acegisecurity.providers.UsernamePasswordAuthenticationToken;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Vector;
|
||||
|
||||
|
||||
/**
|
||||
* Tests {@link AnonymousAuthenticationToken}.
|
||||
*
|
||||
* @author Ben Alex
|
||||
* @version $Id$
|
||||
*/
|
||||
public class AnonymousAuthenticationTokenTests extends TestCase {
|
||||
//~ Constructors ===========================================================
|
||||
|
||||
public AnonymousAuthenticationTokenTests() {
|
||||
super();
|
||||
}
|
||||
|
||||
public AnonymousAuthenticationTokenTests(String arg0) {
|
||||
super(arg0);
|
||||
}
|
||||
|
||||
//~ Methods ================================================================
|
||||
|
||||
public final void setUp() throws Exception {
|
||||
super.setUp();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
junit.textui.TestRunner.run(AnonymousAuthenticationTokenTests.class);
|
||||
}
|
||||
|
||||
public void testConstructorRejectsNulls() {
|
||||
try {
|
||||
new AnonymousAuthenticationToken(null, "Test",
|
||||
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
|
||||
"ROLE_TWO")});
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
} catch (IllegalArgumentException expected) {
|
||||
assertTrue(true);
|
||||
}
|
||||
|
||||
try {
|
||||
new AnonymousAuthenticationToken("key", null,
|
||||
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
|
||||
"ROLE_TWO")});
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
} catch (IllegalArgumentException expected) {
|
||||
assertTrue(true);
|
||||
}
|
||||
|
||||
try {
|
||||
new AnonymousAuthenticationToken("key", "Test", null);
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
} catch (IllegalArgumentException expected) {
|
||||
assertTrue(true);
|
||||
}
|
||||
|
||||
try {
|
||||
new AnonymousAuthenticationToken("key", "Test",
|
||||
new GrantedAuthority[] {null});
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
} catch (IllegalArgumentException expected) {
|
||||
assertTrue(true);
|
||||
}
|
||||
|
||||
try {
|
||||
new AnonymousAuthenticationToken("key", "Test",
|
||||
new GrantedAuthority[] {});
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
} catch (IllegalArgumentException expected) {
|
||||
assertTrue(true);
|
||||
}
|
||||
}
|
||||
|
||||
public void testEqualsWhenEqual() {
|
||||
List proxyList1 = new Vector();
|
||||
proxyList1.add("https://localhost/newPortal/j_acegi_cas_security_check");
|
||||
|
||||
AnonymousAuthenticationToken token1 = new AnonymousAuthenticationToken("key",
|
||||
"Test",
|
||||
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
|
||||
"ROLE_TWO")});
|
||||
|
||||
AnonymousAuthenticationToken token2 = new AnonymousAuthenticationToken("key",
|
||||
"Test",
|
||||
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
|
||||
"ROLE_TWO")});
|
||||
|
||||
assertEquals(token1, token2);
|
||||
}
|
||||
|
||||
public void testGetters() {
|
||||
AnonymousAuthenticationToken token = new AnonymousAuthenticationToken("key",
|
||||
"Test",
|
||||
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
|
||||
"ROLE_TWO")});
|
||||
|
||||
assertEquals("key".hashCode(), token.getKeyHash());
|
||||
assertEquals("Test", token.getPrincipal());
|
||||
assertEquals("", token.getCredentials());
|
||||
assertEquals("ROLE_ONE", token.getAuthorities()[0].getAuthority());
|
||||
assertEquals("ROLE_TWO", token.getAuthorities()[1].getAuthority());
|
||||
assertTrue(token.isAuthenticated());
|
||||
}
|
||||
|
||||
public void testNoArgConstructor() {
|
||||
try {
|
||||
new AnonymousAuthenticationToken();
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
} catch (IllegalArgumentException expected) {
|
||||
assertTrue(true);
|
||||
}
|
||||
}
|
||||
|
||||
public void testNotEqualsDueToAbstractParentEqualsCheck() {
|
||||
AnonymousAuthenticationToken token1 = new AnonymousAuthenticationToken("key",
|
||||
"Test",
|
||||
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
|
||||
"ROLE_TWO")});
|
||||
|
||||
AnonymousAuthenticationToken token2 = new AnonymousAuthenticationToken("key",
|
||||
"DIFFERENT_PRINCIPAL",
|
||||
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
|
||||
"ROLE_TWO")});
|
||||
|
||||
assertFalse(token1.equals(token2));
|
||||
}
|
||||
|
||||
public void testNotEqualsDueToDifferentAuthenticationClass() {
|
||||
AnonymousAuthenticationToken token1 = new AnonymousAuthenticationToken("key",
|
||||
"Test",
|
||||
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
|
||||
"ROLE_TWO")});
|
||||
|
||||
UsernamePasswordAuthenticationToken token2 = new UsernamePasswordAuthenticationToken("Test",
|
||||
"Password",
|
||||
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
|
||||
"ROLE_TWO")});
|
||||
token2.setAuthenticated(true);
|
||||
|
||||
assertFalse(token1.equals(token2));
|
||||
}
|
||||
|
||||
public void testNotEqualsDueToKey() {
|
||||
AnonymousAuthenticationToken token1 = new AnonymousAuthenticationToken("key",
|
||||
"Test",
|
||||
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
|
||||
"ROLE_TWO")});
|
||||
|
||||
AnonymousAuthenticationToken token2 = new AnonymousAuthenticationToken("DIFFERENT_KEY",
|
||||
"Test",
|
||||
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
|
||||
"ROLE_TWO")});
|
||||
|
||||
assertFalse(token1.equals(token2));
|
||||
}
|
||||
|
||||
public void testSetAuthenticatedIgnored() {
|
||||
AnonymousAuthenticationToken token = new AnonymousAuthenticationToken("key",
|
||||
"Test",
|
||||
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
|
||||
"ROLE_TWO")});
|
||||
assertTrue(token.isAuthenticated());
|
||||
token.setAuthenticated(false); // ignored
|
||||
assertTrue(token.isAuthenticated());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,200 @@
|
||||
/* Copyright 2004, 2005 Acegi Technology Pty Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package net.sf.acegisecurity.providers.anonymous;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import net.sf.acegisecurity.Authentication;
|
||||
import net.sf.acegisecurity.GrantedAuthority;
|
||||
import net.sf.acegisecurity.GrantedAuthorityImpl;
|
||||
import net.sf.acegisecurity.MockFilterConfig;
|
||||
import net.sf.acegisecurity.MockHttpServletRequest;
|
||||
import net.sf.acegisecurity.MockHttpServletResponse;
|
||||
import net.sf.acegisecurity.context.ContextHolder;
|
||||
import net.sf.acegisecurity.context.security.SecureContext;
|
||||
import net.sf.acegisecurity.context.security.SecureContextImpl;
|
||||
import net.sf.acegisecurity.context.security.SecureContextUtils;
|
||||
import net.sf.acegisecurity.providers.TestingAuthenticationToken;
|
||||
import net.sf.acegisecurity.providers.dao.memory.UserAttribute;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.servlet.Filter;
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.FilterConfig;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRequest;
|
||||
import javax.servlet.ServletResponse;
|
||||
|
||||
|
||||
/**
|
||||
* Tests {@link AnonymousProcessingFilter}.
|
||||
*
|
||||
* @author Ben Alex
|
||||
* @version $Id$
|
||||
*/
|
||||
public class AnonymousProcessingFilterTests extends TestCase {
|
||||
//~ Constructors ===========================================================
|
||||
|
||||
public AnonymousProcessingFilterTests() {
|
||||
super();
|
||||
}
|
||||
|
||||
public AnonymousProcessingFilterTests(String arg0) {
|
||||
super(arg0);
|
||||
}
|
||||
|
||||
//~ Methods ================================================================
|
||||
|
||||
public static void main(String[] args) {
|
||||
junit.textui.TestRunner.run(AnonymousProcessingFilterTests.class);
|
||||
}
|
||||
|
||||
public void testDetectsMissingKey() throws Exception {
|
||||
UserAttribute user = new UserAttribute();
|
||||
user.setPassword("anonymousUsername");
|
||||
user.addAuthority(new GrantedAuthorityImpl("ROLE_ANONYMOUS"));
|
||||
|
||||
AnonymousProcessingFilter filter = new AnonymousProcessingFilter();
|
||||
filter.setUserAttribute(user);
|
||||
|
||||
try {
|
||||
filter.afterPropertiesSet();
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
} catch (IllegalArgumentException expected) {
|
||||
assertTrue(true);
|
||||
}
|
||||
}
|
||||
|
||||
public void testDetectsUserAttribute() throws Exception {
|
||||
AnonymousProcessingFilter filter = new AnonymousProcessingFilter();
|
||||
filter.setKey("qwerty");
|
||||
|
||||
try {
|
||||
filter.afterPropertiesSet();
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
} catch (IllegalArgumentException expected) {
|
||||
assertTrue(true);
|
||||
}
|
||||
}
|
||||
|
||||
public void testGettersSetters() throws Exception {
|
||||
UserAttribute user = new UserAttribute();
|
||||
user.setPassword("anonymousUsername");
|
||||
user.addAuthority(new GrantedAuthorityImpl("ROLE_ANONYMOUS"));
|
||||
|
||||
AnonymousProcessingFilter filter = new AnonymousProcessingFilter();
|
||||
filter.setKey("qwerty");
|
||||
filter.setUserAttribute(user);
|
||||
filter.afterPropertiesSet();
|
||||
|
||||
assertEquals("qwerty", filter.getKey());
|
||||
assertEquals(user, filter.getUserAttribute());
|
||||
}
|
||||
|
||||
public void testOperationWhenAuthenticationExistsInContextHolder()
|
||||
throws Exception {
|
||||
// Put an Authentication object into the ContextHolder
|
||||
SecureContext sc = SecureContextUtils.getSecureContext();
|
||||
Authentication originalAuth = new TestingAuthenticationToken("user",
|
||||
"password",
|
||||
new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_A")});
|
||||
sc.setAuthentication(originalAuth);
|
||||
ContextHolder.setContext(sc);
|
||||
|
||||
// Setup our filter correctly
|
||||
UserAttribute user = new UserAttribute();
|
||||
user.setPassword("anonymousUsername");
|
||||
user.addAuthority(new GrantedAuthorityImpl("ROLE_ANONYMOUS"));
|
||||
|
||||
AnonymousProcessingFilter filter = new AnonymousProcessingFilter();
|
||||
filter.setKey("qwerty");
|
||||
filter.setUserAttribute(user);
|
||||
filter.afterPropertiesSet();
|
||||
|
||||
// Test
|
||||
executeFilterInContainerSimulator(new MockFilterConfig(), filter,
|
||||
new MockHttpServletRequest("x"), new MockHttpServletResponse(),
|
||||
new MockFilterChain(true));
|
||||
|
||||
// Ensure filter didn't change our original object
|
||||
assertEquals(originalAuth,
|
||||
SecureContextUtils.getSecureContext().getAuthentication());
|
||||
}
|
||||
|
||||
public void testOperationWhenNoAuthenticationInContextHolder()
|
||||
throws Exception {
|
||||
UserAttribute user = new UserAttribute();
|
||||
user.setPassword("anonymousUsername");
|
||||
user.addAuthority(new GrantedAuthorityImpl("ROLE_ANONYMOUS"));
|
||||
|
||||
AnonymousProcessingFilter filter = new AnonymousProcessingFilter();
|
||||
filter.setKey("qwerty");
|
||||
filter.setUserAttribute(user);
|
||||
filter.afterPropertiesSet();
|
||||
|
||||
executeFilterInContainerSimulator(new MockFilterConfig(), filter,
|
||||
new MockHttpServletRequest("x"), new MockHttpServletResponse(),
|
||||
new MockFilterChain(true));
|
||||
|
||||
Authentication auth = SecureContextUtils.getSecureContext()
|
||||
.getAuthentication();
|
||||
assertEquals("anonymousUsername", auth.getPrincipal());
|
||||
assertEquals(new GrantedAuthorityImpl("ROLE_ANONYMOUS"),
|
||||
auth.getAuthorities()[0]);
|
||||
}
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
ContextHolder.setContext(new SecureContextImpl());
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
super.tearDown();
|
||||
ContextHolder.setContext(null);
|
||||
}
|
||||
|
||||
private void executeFilterInContainerSimulator(FilterConfig filterConfig,
|
||||
Filter filter, ServletRequest request, ServletResponse response,
|
||||
FilterChain filterChain) throws ServletException, IOException {
|
||||
filter.init(filterConfig);
|
||||
filter.doFilter(request, response, filterChain);
|
||||
filter.destroy();
|
||||
}
|
||||
|
||||
//~ Inner Classes ==========================================================
|
||||
|
||||
private class MockFilterChain implements FilterChain {
|
||||
private boolean expectToProceed;
|
||||
|
||||
public MockFilterChain(boolean expectToProceed) {
|
||||
this.expectToProceed = expectToProceed;
|
||||
}
|
||||
|
||||
private MockFilterChain() {
|
||||
super();
|
||||
}
|
||||
|
||||
public void doFilter(ServletRequest request, ServletResponse response)
|
||||
throws IOException, ServletException {
|
||||
if (expectToProceed) {
|
||||
assertTrue(true);
|
||||
} else {
|
||||
fail("Did not expect filter chain to proceed");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
/* Copyright 2004 Acegi Technology Pty Limited
|
||||
/* Copyright 2004, 2005 Acegi Technology Pty Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -19,8 +19,7 @@ import junit.framework.TestCase;
|
||||
|
||||
|
||||
/**
|
||||
* Tests {@link UserAttributeEditor} and associated {@link
|
||||
* UserAttributeDefinition}.
|
||||
* Tests {@link UserAttributeEditor} and associated {@link UserAttribute}.
|
||||
*
|
||||
* @author Ben Alex
|
||||
* @version $Id$
|
||||
@@ -50,8 +49,7 @@ public class UserAttributeEditorTests extends TestCase {
|
||||
UserAttributeEditor editor = new UserAttributeEditor();
|
||||
editor.setAsText("password,ROLE_ONE,ROLE_TWO");
|
||||
|
||||
UserAttributeDefinition user = (UserAttributeDefinition) editor
|
||||
.getValue();
|
||||
UserAttribute user = (UserAttribute) editor.getValue();
|
||||
assertTrue(user.isValid());
|
||||
assertTrue(user.isEnabled()); // default
|
||||
assertEquals("password", user.getPassword());
|
||||
@@ -64,8 +62,7 @@ public class UserAttributeEditorTests extends TestCase {
|
||||
UserAttributeEditor editor = new UserAttributeEditor();
|
||||
editor.setAsText("password,disabled,ROLE_ONE,ROLE_TWO");
|
||||
|
||||
UserAttributeDefinition user = (UserAttributeDefinition) editor
|
||||
.getValue();
|
||||
UserAttribute user = (UserAttribute) editor.getValue();
|
||||
assertTrue(user.isValid());
|
||||
assertTrue(!user.isEnabled());
|
||||
assertEquals("password", user.getPassword());
|
||||
@@ -78,8 +75,7 @@ public class UserAttributeEditorTests extends TestCase {
|
||||
UserAttributeEditor editor = new UserAttributeEditor();
|
||||
editor.setAsText("");
|
||||
|
||||
UserAttributeDefinition user = (UserAttributeDefinition) editor
|
||||
.getValue();
|
||||
UserAttribute user = (UserAttribute) editor.getValue();
|
||||
assertTrue(user == null);
|
||||
}
|
||||
|
||||
@@ -87,8 +83,7 @@ public class UserAttributeEditorTests extends TestCase {
|
||||
UserAttributeEditor editor = new UserAttributeEditor();
|
||||
editor.setAsText("password,ROLE_ONE,enabled,ROLE_TWO");
|
||||
|
||||
UserAttributeDefinition user = (UserAttributeDefinition) editor
|
||||
.getValue();
|
||||
UserAttribute user = (UserAttribute) editor.getValue();
|
||||
assertTrue(user.isValid());
|
||||
assertTrue(user.isEnabled());
|
||||
assertEquals("password", user.getPassword());
|
||||
@@ -101,8 +96,7 @@ public class UserAttributeEditorTests extends TestCase {
|
||||
UserAttributeEditor editor = new UserAttributeEditor();
|
||||
editor.setAsText("MALFORMED_STRING");
|
||||
|
||||
UserAttributeDefinition user = (UserAttributeDefinition) editor
|
||||
.getValue();
|
||||
UserAttribute user = (UserAttribute) editor.getValue();
|
||||
assertTrue(user == null);
|
||||
}
|
||||
|
||||
@@ -110,8 +104,7 @@ public class UserAttributeEditorTests extends TestCase {
|
||||
UserAttributeEditor editor = new UserAttributeEditor();
|
||||
editor.setAsText("disabled");
|
||||
|
||||
UserAttributeDefinition user = (UserAttributeDefinition) editor
|
||||
.getValue();
|
||||
UserAttribute user = (UserAttribute) editor.getValue();
|
||||
assertTrue(user == null);
|
||||
}
|
||||
|
||||
@@ -119,8 +112,7 @@ public class UserAttributeEditorTests extends TestCase {
|
||||
UserAttributeEditor editor = new UserAttributeEditor();
|
||||
editor.setAsText("password,enabled");
|
||||
|
||||
UserAttributeDefinition user = (UserAttributeDefinition) editor
|
||||
.getValue();
|
||||
UserAttribute user = (UserAttribute) editor.getValue();
|
||||
assertTrue(user == null);
|
||||
}
|
||||
|
||||
@@ -128,8 +120,7 @@ public class UserAttributeEditorTests extends TestCase {
|
||||
UserAttributeEditor editor = new UserAttributeEditor();
|
||||
editor.setAsText(null);
|
||||
|
||||
UserAttributeDefinition user = (UserAttributeDefinition) editor
|
||||
.getValue();
|
||||
UserAttribute user = (UserAttribute) editor.getValue();
|
||||
assertTrue(user == null);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user