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,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)) {

View File

@@ -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);
}
}

View File

@@ -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 + "]" ;
}
}

View File

@@ -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;

View File

@@ -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 ===================================================================================================

View File

@@ -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;
}
}

View File

@@ -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));
}
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
});
}

View File

@@ -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));
}
}

View File

@@ -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);
}
});
}