SEC-1418: Deprecate GrantedAuthorityImpl in favour of final SimpleGrantedAuthority.

It should be noted that equality checks or lookups with Strings or other authority types will now fail where they would have succeeded before.
This commit is contained in:
Luke Taylor
2010-12-03 16:41:46 +00:00
parent 978b7d4707
commit 4a40d80da1
45 changed files with 380 additions and 414 deletions

View File

@@ -15,39 +15,32 @@
package org.springframework.security.access.intercept;
import junit.framework.TestCase;
import static org.junit.Assert.*;
import org.springframework.security.access.intercept.RunAsImplAuthenticationProvider;
import org.springframework.security.access.intercept.RunAsUserToken;
import org.junit.*;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.TestingAuthenticationToken;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
/**
* Tests {@link RunAsImplAuthenticationProvider}.
*/
public class RunAsImplAuthenticationProviderTests extends TestCase {
public class RunAsImplAuthenticationProviderTests {
@Test(expected = BadCredentialsException.class)
public void testAuthenticationFailDueToWrongKey() {
RunAsUserToken token = new RunAsUserToken("wrong_key", "Test", "Password",
AuthorityUtils.createAuthorityList("ROLE_ONE", "ROLE_TWO"), UsernamePasswordAuthenticationToken.class);
RunAsImplAuthenticationProvider provider = new RunAsImplAuthenticationProvider();
provider.setKey("hello_world");
try {
provider.authenticate(token);
fail("Should have thrown BadCredentialsException");
} catch (BadCredentialsException expected) {
assertTrue(true);
}
provider.authenticate(token);
}
@Test
public void testAuthenticationSuccess() {
RunAsUserToken token = new RunAsUserToken("my_password", "Test", "Password",
AuthorityUtils.createAuthorityList("ROLE_ONE", "ROLE_TWO"), UsernamePasswordAuthenticationToken.class);
@@ -56,33 +49,28 @@ public class RunAsImplAuthenticationProviderTests extends TestCase {
Authentication result = provider.authenticate(token);
if (!(result instanceof RunAsUserToken)) {
fail("Should have returned RunAsUserToken");
}
Assert.assertTrue("Should have returned RunAsUserToken", result instanceof RunAsUserToken);
RunAsUserToken resultCast = (RunAsUserToken) result;
assertEquals("my_password".hashCode(), resultCast.getKeyHash());
}
@Test(expected = IllegalArgumentException.class)
public void testStartupFailsIfNoKey() throws Exception {
RunAsImplAuthenticationProvider provider = new RunAsImplAuthenticationProvider();
try {
provider.afterPropertiesSet();
fail("Should have thrown IllegalArgumentException");
} catch (IllegalArgumentException expected) {
assertTrue(true);
}
provider.afterPropertiesSet();
}
@Test
public void testStartupSuccess() throws Exception {
RunAsImplAuthenticationProvider provider = new RunAsImplAuthenticationProvider();
provider.setKey("hello_world");
assertEquals("hello_world", provider.getKey());
provider.afterPropertiesSet();
assertTrue(true);
}
@Test
public void testSupports() {
RunAsImplAuthenticationProvider provider = new RunAsImplAuthenticationProvider();
assertTrue(provider.supports(RunAsUserToken.class));

View File

@@ -17,19 +17,14 @@ package org.springframework.security.access.vote;
import static org.junit.Assert.*;
import java.util.List;
import java.util.Vector;
import org.junit.Test;
import org.junit.*;
import org.springframework.security.access.AccessDecisionVoter;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.access.SecurityConfig;
import org.springframework.security.access.vote.ConsensusBased;
import org.springframework.security.access.vote.RoleVoter;
import org.springframework.security.authentication.TestingAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import java.util.*;
/**

View File

@@ -17,13 +17,12 @@ package org.springframework.security.authentication;
import static org.junit.Assert.*;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.*;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import java.util.*;
/**
@@ -49,7 +48,7 @@ public class AbstractAuthenticationTokenTests {
List<GrantedAuthority> gotAuthorities = (List<GrantedAuthority>) token.getAuthorities();
assertNotSame(authorities, gotAuthorities);
gotAuthorities.set(0, new GrantedAuthorityImpl("ROLE_SUPER_USER"));
gotAuthorities.set(0, new SimpleGrantedAuthority("ROLE_SUPER_USER"));
}
@Test

View File

@@ -15,17 +15,15 @@
package org.springframework.security.authentication.anonymous;
import junit.framework.TestCase;
import static org.junit.Assert.*;
import org.junit.*;
import org.springframework.security.authentication.AnonymousAuthenticationProvider;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.TestingAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
/**
@@ -33,10 +31,11 @@ import org.springframework.security.core.authority.GrantedAuthorityImpl;
*
* @author Ben Alex
*/
public class AnonymousAuthenticationProviderTests extends TestCase {
public class AnonymousAuthenticationProviderTests {
//~ Methods ========================================================================================================
@Test
public void testDetectsAnInvalidKey() throws Exception {
AnonymousAuthenticationProvider aap = new AnonymousAuthenticationProvider();
aap.setKey("qwerty");
@@ -51,6 +50,7 @@ public class AnonymousAuthenticationProviderTests extends TestCase {
}
}
@Test
public void testDetectsMissingKey() throws Exception {
AnonymousAuthenticationProvider aap = new AnonymousAuthenticationProvider();
@@ -62,6 +62,7 @@ public class AnonymousAuthenticationProviderTests extends TestCase {
}
}
@Test
public void testGettersSetters() throws Exception {
AnonymousAuthenticationProvider aap = new AnonymousAuthenticationProvider();
aap.setKey("qwerty");
@@ -69,6 +70,7 @@ public class AnonymousAuthenticationProviderTests extends TestCase {
assertEquals("qwerty", aap.getKey());
}
@Test
public void testIgnoresClassesItDoesNotSupport() throws Exception {
AnonymousAuthenticationProvider aap = new AnonymousAuthenticationProvider();
aap.setKey("qwerty");
@@ -80,6 +82,7 @@ public class AnonymousAuthenticationProviderTests extends TestCase {
assertNull(aap.authenticate(token));
}
@Test
public void testNormalOperation() throws Exception {
AnonymousAuthenticationProvider aap = new AnonymousAuthenticationProvider();
aap.setKey("qwerty");
@@ -92,6 +95,7 @@ public class AnonymousAuthenticationProviderTests extends TestCase {
assertEquals(result, token);
}
@Test
public void testSupports() {
AnonymousAuthenticationProvider aap = new AnonymousAuthenticationProvider();
assertTrue(aap.supports(AnonymousAuthenticationToken.class));

View File

@@ -23,8 +23,7 @@ import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.net.URL;
import java.security.Security;
import java.util.Collection;
import java.util.List;
import java.util.*;
import javax.security.auth.login.LoginContext;
import javax.security.auth.login.LoginException;
@@ -41,7 +40,7 @@ import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.security.core.session.SessionDestroyedEvent;
@@ -193,11 +192,12 @@ public class JaasAuthenticationProviderTests {
assertNotNull(jaasProvider.getLoginContextName());
Collection<? extends GrantedAuthority> list = auth.getAuthorities();
Set<String> set = AuthorityUtils.authorityListToSet(list);
assertTrue("GrantedAuthorities should contain ROLE_TEST1", list.contains(new GrantedAuthorityImpl("ROLE_TEST1")));
assertTrue("GrantedAuthorities should contain ROLE_TEST2", list.contains(new GrantedAuthorityImpl("ROLE_TEST2")));
assertTrue("GrantedAuthorities should contain ROLE_1", list.contains(defaultAuths.get(0)));
assertTrue("GrantedAuthorities should contain ROLE_2", list.contains(defaultAuths.get(1)));
assertTrue("GrantedAuthorities should contain ROLE_1", set.contains("ROLE_ONE"));
assertTrue("GrantedAuthorities should contain ROLE_2", set.contains("ROLE_TWO"));
assertTrue("GrantedAuthorities should contain ROLE_TEST1", set.contains("ROLE_TEST1"));
assertTrue("GrantedAuthorities should contain ROLE_TEST2", set.contains("ROLE_TEST2"));
boolean foundit = false;
@@ -213,7 +213,6 @@ public class JaasAuthenticationProviderTests {
assertNotNull("Success event should be fired", eventCheck.successEvent);
assertEquals("Auth objects should be equal", auth, eventCheck.successEvent.getAuthentication());
assertNull("Failure event should not be fired", eventCheck.failedEvent);
}

View File

@@ -1,77 +0,0 @@
/* Copyright 2004, 2005, 2006 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 org.springframework.security.core.authority;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
/**
* Tests {@link GrantedAuthorityImpl}.
*
* @author Ben Alex
*/
public class GrantedAuthorityImplTests {
@Test
public void equalsBehavesAsExpected() throws Exception {
GrantedAuthorityImpl auth1 = new GrantedAuthorityImpl("TEST");
GrantedAuthorityImpl auth2 = new GrantedAuthorityImpl("TEST");
assertEquals(auth1, auth2);
String authString1 = "TEST";
assertEquals(auth1, authString1);
String authString2 = "NOT_EQUAL";
assertTrue(!auth1.equals(authString2));
GrantedAuthorityImpl auth3 = new GrantedAuthorityImpl("NOT_EQUAL");
assertTrue(!auth1.equals(auth3));
MockGrantedAuthority mock1 = new MockGrantedAuthority("TEST");
assertEquals(auth1, mock1);
MockGrantedAuthority mock2 = new MockGrantedAuthority("NOT_EQUAL");
assertTrue(!auth1.equals(mock2));
Integer int1 = Integer.valueOf(222);
assertTrue(!auth1.equals(int1));
}
@Test
public void toStringReturnsAuthorityValue() {
GrantedAuthorityImpl auth = new GrantedAuthorityImpl("TEST");
assertEquals("TEST", auth.toString());
}
//~ Inner Classes ==================================================================================================
private class MockGrantedAuthority implements GrantedAuthority {
private String role;
public MockGrantedAuthority(String role) {
this.role = role;
}
public String getAuthority() {
return this.role;
}
}
}

View File

@@ -0,0 +1,54 @@
/* Copyright 2004, 2005, 2006 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 org.springframework.security.core.authority;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import org.junit.*;
import org.springframework.security.core.GrantedAuthority;
/**
* Tests {@link SimpleGrantedAuthority}.
*
* @author Ben Alex
*/
public class SimpleGrantedAuthorityTests {
@Test
public void equalsBehavesAsExpected() throws Exception {
SimpleGrantedAuthority auth1 = new SimpleGrantedAuthority("TEST");
assertEquals(auth1, auth1);
assertEquals(auth1, new SimpleGrantedAuthority("TEST"));
assertFalse(auth1.equals("TEST"));
SimpleGrantedAuthority auth3 = new SimpleGrantedAuthority("NOT_EQUAL");
assertTrue(!auth1.equals(auth3));
assertFalse(auth1.equals(mock(GrantedAuthority.class)));
assertFalse(auth1.equals(Integer.valueOf(222)));
}
@Test
public void toStringReturnsAuthorityValue() {
SimpleGrantedAuthority auth = new SimpleGrantedAuthority("TEST");
assertEquals("TEST", auth.toString());
}
}

View File

@@ -4,7 +4,7 @@ import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import java.util.*;
@@ -157,11 +157,11 @@ public class MapBasedAttributes2GrantedAuthoritiesMapperTest {
private HashMap getValidAttributes2GrantedAuthoritiesMap() {
HashMap m = new HashMap();
m.put("role1","ga1");
m.put("role2",new GrantedAuthorityImpl("ga2"));
m.put("role3",Arrays.asList("ga3",new GrantedAuthorityImpl("ga4")));
m.put("role2",new SimpleGrantedAuthority("ga2"));
m.put("role3",Arrays.asList("ga3",new SimpleGrantedAuthority("ga4")));
m.put("role4","ga5,ga6");
m.put("role5",Arrays.asList("ga7","ga8",new Object[]{new GrantedAuthorityImpl("ga9")}));
m.put("role6",new Object[]{"ga10","ga11",new Object[]{new GrantedAuthorityImpl("ga12")}});
m.put("role5",Arrays.asList("ga7","ga8",new Object[]{new SimpleGrantedAuthority("ga9")}));
m.put("role6",new Object[]{"ga10","ga11",new Object[]{new SimpleGrantedAuthority("ga12")}});
m.put("role7",new String[]{"ga13","ga14"});
m.put("role8",new String[]{"ga13","ga14",null});
m.put("role9",null);

View File

@@ -26,7 +26,7 @@ import java.util.Set;
import org.junit.Test;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
/**
@@ -98,7 +98,7 @@ public class UserTests {
try {
List<GrantedAuthority> auths = AuthorityUtils.createAuthorityList("ROLE_ONE");
auths.add(null);
auths.add(new GrantedAuthorityImpl("ROLE_THREE"));
auths.add(new SimpleGrantedAuthority("ROLE_THREE"));
new User(null, "koala", true, true, true, true, auths);
fail("Should have thrown IllegalArgumentException");
} catch (IllegalArgumentException expected) {

View File

@@ -23,7 +23,7 @@ import org.springframework.security.authentication.UsernamePasswordAuthenticatio
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserCache;
@@ -268,7 +268,7 @@ public class JdbcUserDetailsManagerTests {
@Test
public void addGroupAuthorityInsertsCorrectGroupAuthorityRow() throws Exception {
GrantedAuthority auth = new GrantedAuthorityImpl("ROLE_X");
GrantedAuthority auth = new SimpleGrantedAuthority("ROLE_X");
manager.addGroupAuthority("GROUP_0", auth);
template.queryForObject("select authority from group_authorities where authority = 'ROLE_X' and group_id = 0", String.class);
@@ -276,7 +276,7 @@ public class JdbcUserDetailsManagerTests {
@Test
public void deleteGroupAuthorityRemovesCorrectRows() throws Exception {
GrantedAuthority auth = new GrantedAuthorityImpl("ROLE_A");
GrantedAuthority auth = new SimpleGrantedAuthority("ROLE_A");
manager.removeGroupAuthority("GROUP_0", auth);
assertEquals(0, template.queryForList("select authority from group_authorities where group_id = 0").size());