1
0
mirror of synced 2026-08-04 17:27:13 +00:00

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
@@ -15,22 +15,15 @@
package org.springframework.security.access.hierarchicalroles;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* <p>
@@ -173,8 +166,8 @@ public class RoleHierarchyImpl implements RoleHierarchy {
rolesReachableInOneStepMap = new HashMap<GrantedAuthority, Set<GrantedAuthority>>();
while (roleHierarchyMatcher.find()) {
GrantedAuthority higherRole = new GrantedAuthorityImpl(roleHierarchyMatcher.group(2));
GrantedAuthority lowerRole = new GrantedAuthorityImpl(roleHierarchyMatcher.group(3));
GrantedAuthority higherRole = new SimpleGrantedAuthority(roleHierarchyMatcher.group(2));
GrantedAuthority lowerRole = new SimpleGrantedAuthority(roleHierarchyMatcher.group(3));
Set<GrantedAuthority> rolesReachableInOneStepSet;
if (!rolesReachableInOneStepMap.containsKey(higherRole)) {
@@ -210,7 +203,7 @@ public class RoleHierarchyImpl implements RoleHierarchy {
while (!rolesToVisitSet.isEmpty()) {
// take a role from the rolesToVisit set
GrantedAuthority aRole = (GrantedAuthority) rolesToVisitSet.iterator().next();
GrantedAuthority aRole = rolesToVisitSet.iterator().next();
rolesToVisitSet.remove(aRole);
addReachableRoles(visitedRolesSet, aRole);
if (rolesReachableInOneStepMap.containsKey(aRole)) {
@@ -23,7 +23,7 @@ import org.springframework.beans.factory.InitializingBean;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.util.Assert;
@@ -32,8 +32,8 @@ import org.springframework.util.Assert;
* <p>
* Is activated if any {@link ConfigAttribute#getAttribute()} is prefixed with <Code>RUN_AS_</code>.
* If found, it generates a new {@link RunAsUserToken} containing the same principal, credentials and granted
* authorities as the original {@link Authentication} object, along with {@link GrantedAuthorityImpl}s for each
* <code>RUN_AS_</code> indicated. The created <code>GrantedAuthorityImpl</code>s will be prefixed with a special
* authorities as the original {@link Authentication} object, along with {@link SimpleGrantedAuthority}s for each
* <code>RUN_AS_</code> indicated. The created <code>SimpleGrantedAuthority</code>s will be prefixed with a special
* prefix indicating that it is a role (default prefix value is <code>ROLE_</code>), and then the remainder of the
* <code>RUN_AS_</code> keyword. For example, <code>RUN_AS_FOO</code> will result in the creation of a granted
* authority of <code>ROLE_RUN_AS_FOO</code>.
@@ -66,7 +66,7 @@ public class RunAsManagerImpl implements RunAsManager, InitializingBean {
for (ConfigAttribute attribute : attributes) {
if (this.supports(attribute)) {
GrantedAuthority extraAuthority = new GrantedAuthorityImpl(getRolePrefix() + attribute.getAttribute());
GrantedAuthority extraAuthority = new SimpleGrantedAuthority(getRolePrefix() + attribute.getAttribute());
newAuthorities.add(extraAuthority);
}
}
@@ -15,29 +15,29 @@
package org.springframework.security.authentication.jaas;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.GrantedAuthority;
import java.security.Principal;
/**
* Extends GrantedAuthorityImpl to hold the principal that an AuthorityGranter justified as a reason to grant this
* Authority. <br>
* {@code GrantedAuthority} which, in addition to the assigned role, holds the principal that an
* {@link AuthorityGranter} used as a reason to grant this authority.
*
* @author Ray Krueger
*
* @see AuthorityGranter
*/
public class JaasGrantedAuthority extends GrantedAuthorityImpl {
public final class JaasGrantedAuthority implements GrantedAuthority {
//~ Instance fields ================================================================================================
private static final long serialVersionUID = 1L;
private final String role;
private final Principal principal;
//~ Constructors ===================================================================================================
public JaasGrantedAuthority(String role, Principal principal) {
super(role);
this.role = role;
this.principal = principal;
}
@@ -46,4 +46,29 @@ public class JaasGrantedAuthority extends GrantedAuthorityImpl {
public Principal getPrincipal() {
return principal;
}
public String getAuthority() {
return role;
}
public int hashCode() {
return 31 ^ principal.hashCode() ^ role.hashCode();
}
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof JaasGrantedAuthority) {
JaasGrantedAuthority jga = (JaasGrantedAuthority) obj;
return this.role.equals(jga.role) && this.principal.equals(jga.principal);
}
return false;
}
public String toString() {
return "Jaas Authority [" + role + "," + principal + "]" ;
}
}
@@ -49,7 +49,7 @@ public abstract class AuthorityUtils {
List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>(roles.length);
for (String role : roles) {
authorities.add(new GrantedAuthorityImpl(role));
authorities.add(new SimpleGrantedAuthority(role));
}
return authorities;
@@ -15,8 +15,6 @@
package org.springframework.security.core.authority;
import java.io.Serializable;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.util.Assert;
@@ -27,16 +25,14 @@ import org.springframework.util.Assert;
*
* <p>
* Stores a <code>String</code> representation of an authority granted to the {@link Authentication} object.
* <p>
* If compared to a custom authority which returns null from {@link #getAuthority}, the <tt>compareTo</tt>
* method will return -1, so the custom authority will take precedence.
*
* @author Ben Alex
* @deprecated Use the final class {@link SimpleGrantedAuthority} or implement your own.
*/
public class GrantedAuthorityImpl implements GrantedAuthority, Serializable {
@Deprecated
public class GrantedAuthorityImpl implements GrantedAuthority {
//~ Instance fields ================================================================================================
private static final long serialVersionUID = 1L;
private final String role;
//~ Constructors ===================================================================================================
@@ -0,0 +1,46 @@
package org.springframework.security.core.authority;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.util.Assert;
/**
* Basic concrete implementation of a {@link GrantedAuthority}.
*
* <p>
* Stores a {@code String} representation of an authority granted to the
* {@link org.springframework.security.core.Authentication Authentication} object.
*
* @author Luke Taylor
*/
public final class SimpleGrantedAuthority implements GrantedAuthority {
private final String role;
public SimpleGrantedAuthority(String role) {
Assert.hasText(role, "A granted authority textual representation is required");
this.role = role;
}
public String getAuthority() {
return role;
}
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof SimpleGrantedAuthority) {
return role.equals(((SimpleGrantedAuthority) obj).role);
}
return false;
}
public int hashCode() {
return this.role.hashCode();
}
public String toString() {
return this.role;
}
}
@@ -4,7 +4,7 @@ import java.util.*;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
@@ -130,7 +130,7 @@ public class MapBasedAttributes2GrantedAuthoritiesMapper implements Attributes2G
while ( st.hasMoreTokens() ) {
String nextToken = st.nextToken();
if ( StringUtils.hasText(nextToken) ) {
result.add(new GrantedAuthorityImpl(nextToken));
result.add(new SimpleGrantedAuthority(nextToken));
}
}
}
@@ -1,7 +1,7 @@
package org.springframework.security.core.authority.mapping;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import java.util.ArrayList;
import java.util.Collection;
@@ -67,9 +67,9 @@ public class SimpleAttributes2GrantedAuthoritiesMapper implements Attributes2Gra
attribute = attribute.toUpperCase(Locale.getDefault());
}
if (isAddPrefixIfAlreadyExisting() || !attribute.startsWith(getAttributePrefix())) {
return new GrantedAuthorityImpl(getAttributePrefix() + attribute);
return new SimpleGrantedAuthority(getAttributePrefix() + attribute);
} else {
return new GrantedAuthorityImpl(attribute);
return new SimpleGrantedAuthority(attribute);
}
}
@@ -29,7 +29,7 @@ import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.SpringSecurityMessageSource;
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.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
@@ -210,7 +210,7 @@ public class JdbcDaoImpl extends JdbcDaoSupport implements UserDetailsService {
public GrantedAuthority mapRow(ResultSet rs, int rowNum) throws SQLException {
String roleName = rolePrefix + rs.getString(2);
return new GrantedAuthorityImpl(roleName);
return new SimpleGrantedAuthority(roleName);
}
});
}
@@ -225,7 +225,7 @@ public class JdbcDaoImpl extends JdbcDaoSupport implements UserDetailsService {
public GrantedAuthority mapRow(ResultSet rs, int rowNum) throws SQLException {
String roleName = getRolePrefix() + rs.getString(3);
return new GrantedAuthorityImpl(roleName);
return new SimpleGrantedAuthority(roleName);
}
});
}
@@ -20,7 +20,7 @@ import java.util.List;
import java.util.Vector;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
/**
@@ -65,7 +65,7 @@ public class UserAttribute {
public void setAuthoritiesAsString(List<String> authoritiesAsStrings) {
setAuthorities(new ArrayList<GrantedAuthority>(authoritiesAsStrings.size()));
for(String authority : authoritiesAsStrings) {
addAuthority(new GrantedAuthorityImpl(authority));
addAuthority(new SimpleGrantedAuthority(authority));
}
}
@@ -7,7 +7,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.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserCache;
import org.springframework.security.core.userdetails.UserDetails;
@@ -334,7 +334,7 @@ public class JdbcUserDetailsManager extends JdbcDaoImpl implements UserDetailsMa
public GrantedAuthority mapRow(ResultSet rs, int rowNum) throws SQLException {
String roleName = getRolePrefix() + rs.getString(3);
return new GrantedAuthorityImpl(roleName);
return new SimpleGrantedAuthority(roleName);
}
});
}
@@ -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));
@@ -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.*;
/**
@@ -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
@@ -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));
@@ -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);
}
@@ -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;
}
}
}
@@ -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());
}
}
@@ -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);
@@ -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) {
@@ -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());