From 295e0ded188b77536ce56aa4ebb9162fcce34971 Mon Sep 17 00:00:00 2001 From: Luke Taylor Date: Fri, 21 May 2010 15:58:35 +0100 Subject: [PATCH] SEC-1483: Change User constructor to use a generic wildcard for authorities collection. --- .../security/core/userdetails/User.java | 122 +++++++++--------- 1 file changed, 62 insertions(+), 60 deletions(-) diff --git a/core/src/main/java/org/springframework/security/core/userdetails/User.java b/core/src/main/java/org/springframework/security/core/userdetails/User.java index 5932171ab2..a1216af40e 100644 --- a/core/src/main/java/org/springframework/security/core/userdetails/User.java +++ b/core/src/main/java/org/springframework/security/core/userdetails/User.java @@ -81,7 +81,7 @@ public class User implements UserDetails { * GrantedAuthority collection */ public User(String username, String password, boolean enabled, boolean accountNonExpired, - boolean credentialsNonExpired, boolean accountNonLocked, Collection authorities) { + boolean credentialsNonExpired, boolean accountNonLocked, Collection authorities) { if (((username == null) || "".equals(username)) || (password == null)) { throw new IllegalArgumentException("Cannot pass null or empty values to constructor"); @@ -98,6 +98,65 @@ public class User implements UserDetails { //~ Methods ======================================================================================================== + public Collection getAuthorities() { + return authorities; + } + + public String getPassword() { + return password; + } + + public String getUsername() { + return username; + } + + public boolean isAccountNonExpired() { + return accountNonExpired; + } + + public boolean isAccountNonLocked() { + return this.accountNonLocked; + } + + public boolean isCredentialsNonExpired() { + return credentialsNonExpired; + } + + public boolean isEnabled() { + return enabled; + } + + private static SortedSet sortAuthorities(Collection authorities) { + Assert.notNull(authorities, "Cannot pass a null GrantedAuthority collection"); + // Ensure array iteration order is predictable (as per UserDetails.getAuthorities() contract and SEC-717) + SortedSet sortedAuthorities = + new TreeSet(new AuthorityComparator()); + + for (GrantedAuthority grantedAuthority : authorities) { + Assert.notNull(grantedAuthority, "GrantedAuthority list cannot contain any null elements"); + sortedAuthorities.add(grantedAuthority); + } + + return sortedAuthorities; + } + + private static class AuthorityComparator implements Comparator, Serializable { + public int compare(GrantedAuthority g1, GrantedAuthority g2) { + // Neither should ever be null as each entry is checked before adding it to the set. + // If the authority is null, it is a custom authority and should precede others. + if (g2.getAuthority() == null) { + return -1; + } + + if (g1.getAuthority() == null) { + return 1; + } + + return g1.getAuthority().compareTo(g2.getAuthority()); + } + } + + @Override public boolean equals(Object rhs) { if (!(rhs instanceof User) || (rhs == null)) { return false; @@ -119,18 +178,7 @@ public class User implements UserDetails { && (this.isEnabled() == user.isEnabled())); } - public Collection getAuthorities() { - return authorities; - } - - public String getPassword() { - return password; - } - - public String getUsername() { - return username; - } - + @Override public int hashCode() { int code = 9792; @@ -165,53 +213,7 @@ public class User implements UserDetails { return code; } - public boolean isAccountNonExpired() { - return accountNonExpired; - } - - public boolean isAccountNonLocked() { - return this.accountNonLocked; - } - - public boolean isCredentialsNonExpired() { - return credentialsNonExpired; - } - - public boolean isEnabled() { - return enabled; - } - - private static SortedSet sortAuthorities(Collection authorities) { - Assert.notNull(authorities, "Cannot pass a null GrantedAuthority collection"); - // Ensure array iteration order is predictable (as per UserDetails.getAuthorities() contract and SEC-717) - SortedSet sortedAuthorities = - new TreeSet(new AuthorityComparator()); - - for (GrantedAuthority grantedAuthority : authorities) { - Assert.notNull(grantedAuthority, "GrantedAuthority list cannot contain any null elements"); - sortedAuthorities.add(grantedAuthority); - } - - return sortedAuthorities; - } - - private static class AuthorityComparator implements Comparator, Serializable { - public int compare(GrantedAuthority g1, GrantedAuthority g2) { - // Neither should ever be null as each entry is checked before adding it to the set. - // If the authority is null, it is a custom authority and should precede others. - if (g2.getAuthority() == null) { - return -1; - } - - if (g1.getAuthority() == null) { - return 1; - } - - return g1.getAuthority().compareTo(g2.getAuthority()); - } - } - - + @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append(super.toString()).append(": ");